AsperaCredential
using Relativity.Transfer.Resources;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace Relativity.Transfer
{
public sealed class AsperaCredential : IEquatable<AsperaCredential>
{
public const int ManualCredentialArtifactId = 1;
public const string AsperaTransferCredentialType = "Aspera";
public const string AsperaNodeCredentialType = "AsperaNode";
public const string AsperaUrlKey = "url";
public const string AsperaAccountNameKey = "accountName";
public const string AsperaAccountPasswordKey = "accountPassword";
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly List<byte> accountUserNameData = new List<byte>();
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly List<byte> accountPasswordData = new List<byte>();
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public IReadOnlyList<byte> AccountUserName {
get {
return accountUserNameData;
}
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public IReadOnlyList<byte> AccountPassword {
get {
return accountPasswordData;
}
}
public int ArtifactId { get; set; }
public string CredentialType { get; set; }
public Uri Host { get; set; }
public HttpConnectionInfo HttpConnectionInfo => new HttpConnectionInfo(Host, new BasicAuthenticationCredential(AccountUserName.UnprotectData(), AccountPassword.UnprotectData()));
public string Name { get; }
public AsperaCredential()
{
ArtifactId = 0;
CredentialType = null;
Host = null;
Name = null;
}
public AsperaCredential(Uri host, string accountUserName, string accountPassword, string credentialType)
: this(host, accountUserName, accountPassword, credentialType, 1)
{
}
public AsperaCredential(Uri host, string accountUserName, string accountPassword, string credentialType, int artifactId)
{
Validate((host != (Uri)null) ? host.ToString() : null, accountUserName, accountPassword, credentialType);
ArtifactId = artifactId;
CredentialType = credentialType;
Host = host;
Name = "Default";
ChangeAccountUserName(accountUserName);
ChangeAccountPassword(accountPassword);
}
public AsperaCredential(Credential credential)
: this(credential, true)
{
}
internal AsperaCredential(Credential credential, bool validate)
{
if (credential == null)
throw new ArgumentNullException("credential");
if (validate)
Validate(credential);
accountPasswordData.AddRange(credential.GetSecretValue("accountPassword").ProtectData());
accountUserNameData.AddRange(credential.GetSecretValue("accountName").ProtectData());
ArtifactId = credential.ArtifactId;
CredentialType = credential.CredentialType;
Host = new Uri(credential.GetSecretValue("url"), UriKind.RelativeOrAbsolute);
Name = credential.Name;
}
public static void Validate(Credential credential)
{
if (credential == null)
throw new ArgumentNullException("credential");
Validate(credential.GetSecretValue("url"), credential.GetSecretValue("accountName"), credential.GetSecretValue("accountPassword"), credential.CredentialType);
}
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#", Justification = "The parameter name and type is appropriate for the use-case.")]
public static void Validate(string url, string accountUserName, string accountPasssword, string credentialType)
{
if (string.IsNullOrEmpty(accountUserName))
throw new ArgumentException(CoreStrings.AsperaAccountUserNameArgumentExceptionMessage, "accountUserName");
if (string.IsNullOrEmpty(accountPasssword))
throw new ArgumentException(CoreStrings.AsperaAccountPasswordArgumentExceptionMessage, "accountPasssword");
if (string.IsNullOrEmpty(url) || !Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out Uri result))
throw new ArgumentException(CoreStrings.AsperaAccountHostArgumentExceptionMessage, "url");
if (!result.IsWellFormedOriginalString())
throw new ArgumentException(CoreStrings.AsperaAccountHostNotWellFormedExceptionMessage, "url");
if (string.IsNullOrEmpty(credentialType))
throw new ArgumentException(CoreStrings.AsperaAccountCredentialTypeArgumentExceptionMessage, "url");
}
public static bool operator ==(AsperaCredential x, AsperaCredential y)
{
if ((object)x == y)
return true;
if ((object)x == null)
return false;
if ((object)y == null)
return false;
if (x.Host == y.Host && string.Compare(x.AccountUserName.UnprotectData(), y.AccountUserName.UnprotectData(), StringComparison.OrdinalIgnoreCase) == 0)
return string.Compare(x.AccountPassword.UnprotectData(), y.AccountPassword.UnprotectData(), StringComparison.OrdinalIgnoreCase) == 0;
return false;
}
public static bool operator !=(AsperaCredential x, AsperaCredential y)
{
return !(x == y);
}
public Credential CreateCredential()
{
Dictionary<string, string> secrets = new Dictionary<string, string> {
{
"accountName",
AccountUserName.UnprotectData()
},
{
"accountPassword",
AccountPassword.UnprotectData()
},
{
"url",
(Host != (Uri)null) ? Host.ToString() : null
}
};
return new Credential(ArtifactId, Name, CredentialType, secrets);
}
public void ChangeAccountPassword(string value)
{
accountPasswordData.Clear();
if (!string.IsNullOrEmpty(value))
accountPasswordData.AddRange(value.ProtectData());
}
public void ChangeAccountUserName(string value)
{
accountUserNameData.Clear();
if (!string.IsNullOrEmpty(value))
accountUserNameData.AddRange(value.ProtectData());
}
public override int GetHashCode()
{
return (((((AccountUserName != null && AccountUserName.Count > 0) ? AccountUserName.UnprotectData().GetHashCode() : 0) * 397) ^ ((AccountPassword != null && AccountPassword.Count > 0) ? AccountPassword.UnprotectData().GetHashCode() : 0)) * 397) ^ ((Host != (Uri)null) ? Host.GetHashCode() : 0);
}
public override bool Equals(object obj)
{
return Equals(obj as AsperaCredential);
}
public bool Equals(AsperaCredential other)
{
if ((object)other == null)
return false;
if ((object)this == other)
return true;
if (Host == other.Host && string.Compare(AccountUserName.UnprotectData(), other.AccountUserName.UnprotectData(), StringComparison.OrdinalIgnoreCase) == 0)
return string.Compare(AccountPassword.UnprotectData(), other.AccountPassword.UnprotectData(), StringComparison.OrdinalIgnoreCase) == 0;
return false;
}
public bool Equals(Credential other)
{
if (other == null)
return false;
if (string.Compare(other.CredentialType, "Aspera", StringComparison.OrdinalIgnoreCase) != 0 && string.Compare(other.CredentialType, "AsperaNode", StringComparison.OrdinalIgnoreCase) != 0)
return false;
string secretValue = other.GetSecretValue("accountName");
string secretValue2 = other.GetSecretValue("accountPassword");
string secretValue3 = other.GetSecretValue("url");
if (Host == new Uri(secretValue3, UriKind.RelativeOrAbsolute) && string.Compare(AccountUserName.UnprotectData(), secretValue, StringComparison.OrdinalIgnoreCase) == 0)
return string.Compare(AccountPassword.UnprotectData(), secretValue2, StringComparison.OrdinalIgnoreCase) == 0;
return false;
}
}
}