<PackageReference Include="Relativity.Transfer.Client" Version="7.2.7" />

AsperaCredential

using Relativity.Transfer.Resources; using System; using System.Collections.Generic; namespace Relativity.Transfer { public sealed class AsperaCredential : IEquatable<AsperaCredential> { public const int ManualCredentialArtifactId = 1; public const string AsperaTransferCredentialType = "Aspera"; public const string AsperaUrlKey = "url"; public const string AsperaAccountNameKey = "accountName"; public const string AsperaAccountPasswordKey = "accountPassword"; private readonly List<byte> accountUserNameData = new List<byte>(); private readonly List<byte> accountPasswordData = new List<byte>(); public IReadOnlyList<byte> AccountUserName => accountUserNameData; public IReadOnlyList<byte> AccountPassword => 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); } 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"); Uri result = default(Uri); if (string.IsNullOrEmpty(url) || !Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out 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) { if ((object)x != null) { if ((object)y != null) return x.Host == y.Host && string.Compare(x.AccountUserName.UnprotectData(), y.AccountUserName.UnprotectData(), StringComparison.OrdinalIgnoreCase) == 0 && string.Compare(x.AccountPassword.UnprotectData(), y.AccountPassword.UnprotectData(), StringComparison.OrdinalIgnoreCase) == 0; return false; } return false; } return true; } 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() { int num = (AccountUserName != null && AccountUserName.Count > 0) ? AccountUserName.UnprotectData().GetHashCode() : 0; num = ((num * 397) ^ ((AccountPassword != null && AccountPassword.Count > 0) ? AccountPassword.UnprotectData().GetHashCode() : 0)); return (num * 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) { if ((object)this != other) return Host == other.Host && string.Compare(AccountUserName.UnprotectData(), other.AccountUserName.UnprotectData(), StringComparison.OrdinalIgnoreCase) == 0 && string.Compare(AccountPassword.UnprotectData(), other.AccountPassword.UnprotectData(), StringComparison.OrdinalIgnoreCase) == 0; return true; } return false; } public bool Equals(Credential other) { if (other != null) { if (string.Compare(other.CredentialType, "Aspera", StringComparison.OrdinalIgnoreCase) == 0) { string secretValue = other.GetSecretValue("accountName"); string secretValue2 = other.GetSecretValue("accountPassword"); string secretValue3 = other.GetSecretValue("url"); return Host == new Uri(secretValue3, UriKind.RelativeOrAbsolute) && string.Compare(AccountUserName.UnprotectData(), secretValue, StringComparison.OrdinalIgnoreCase) == 0 && string.Compare(AccountPassword.UnprotectData(), secretValue2, StringComparison.OrdinalIgnoreCase) == 0; } return false; } return false; } } }