/*
* Copyright 2011-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using Amazon.Runtime.Internal.Util;
using Amazon.Util;
using System;
namespace Amazon.Runtime
{
///
/// Immutable representation of AWS credentials.
///
public class ImmutableCredentials
{
#region Properties
///
/// Gets the AccessKey property for the current credentials.
///
public string AccessKey { get; private set; }
///
/// Gets the SecretKey property for the current credentials.
///
public string SecretKey { get; private set; }
///
/// Gets the Token property for the current credentials.
///
public string Token { get; private set; }
///
/// Gets the UseToken property for the current credentials.
/// Specifies if Token property is non-emtpy.
///
public bool UseToken { get { return !string.IsNullOrEmpty(Token); } }
#endregion
#region Constructors
///
/// Constructs an ImmutableCredentials object with supplied accessKey, secretKey.
///
///
///
/// Optional. Can be set to null or empty for non-session credentials.
public ImmutableCredentials(string awsAccessKeyId, string awsSecretAccessKey, string token)
{
if (string.IsNullOrEmpty(awsAccessKeyId)) throw new ArgumentNullException("awsAccessKeyId");
if (string.IsNullOrEmpty(awsSecretAccessKey)) throw new ArgumentNullException("awsSecretAccessKey");
AccessKey = awsAccessKeyId;
SecretKey = awsSecretAccessKey;
Token = token ?? string.Empty;
}
private ImmutableCredentials() { }
#endregion
#region Public methods
///
/// Returns a copy of the current credentials.
///
///
public virtual ImmutableCredentials Copy()
{
ImmutableCredentials credentials = new ImmutableCredentials
{
AccessKey = this.AccessKey,
SecretKey = this.SecretKey,
Token = this.Token,
};
return credentials;
}
#endregion
#region Overrides
public override int GetHashCode()
{
return Hashing.Hash(AccessKey, SecretKey, Token);
}
public override bool Equals(object obj)
{
if (object.ReferenceEquals(this, obj))
return true;
ImmutableCredentials ic = obj as ImmutableCredentials;
if (ic == null)
return false;
return AWSSDKUtils.AreEqual(
new object[] { AccessKey, SecretKey, Token },
new object[] { ic.AccessKey, ic.SecretKey, ic.Token });
}
#endregion
}
}