Skip to content

Commit

Permalink
Implement refactor for CredentialType
Browse files Browse the repository at this point in the history
  • Loading branch information
thohng committed Jan 19, 2024
1 parent 0e5794c commit cacdc6c
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions EchoServiceApi/Verifiers/TokenCredentialFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,38 @@ private async Task<TokenCredential> GetDefaultTokenCredentialAsync()

public async Task<TokenCredential?> GetTokenCredentialAsync(AzureCredentialInfo? options)
{
return options != null && options.ClientId is { } clientId
? options.TenantId is { } tenantId
&& options.ClientSecret is { } clientSecret
? await GetClientSecretCredentialAsync(tenantId, clientId, clientSecret)
: await GetManagedIdentityClientIdAsync(clientId)
: null;
if (options != null)
{
if (options.CredentialType is { } credentialType && !string.IsNullOrWhiteSpace(credentialType))
{
return await GetByCredentialTypeAsync(credentialType, options);
}
else
{
if (options.ClientId is { } clientId)
{
return options.TenantId is { } tenantId && options.ClientSecret is { } clientSecret
? await GetClientSecretCredentialAsync(tenantId, clientId, clientSecret)
: await GetManagedIdentityClientIdAsync(clientId);
}
}
}

return null;
}

private Task<TokenCredential?> GetByCredentialTypeAsync(string credentialType, AzureCredentialInfo? options)
{
if ("AzureDeveloperCliCredential".Equals(credentialType, StringComparison.InvariantCultureIgnoreCase))
{
return Task.FromResult<TokenCredential?>(new AzureDeveloperCliCredential());
}
else if ("AzureCliCredential".Equals(credentialType, StringComparison.InvariantCultureIgnoreCase))
{
return Task.FromResult<TokenCredential?>(new AzureCliCredential());
}

return Task.FromResult<TokenCredential?>(null);
}

public string? Redact(string? secret)
Expand Down Expand Up @@ -126,6 +152,7 @@ public class AzureCredentialInfo
public string? TenantId { get; set; }
public string? ClientId { get; set; }
public string? ClientSecret { get; set; }
public string? CredentialType { get; set; }
}

public class TokenCredentialWrapper
Expand Down

0 comments on commit cacdc6c

Please sign in to comment.