| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using Azure.Core; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Text; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using Azure.Core.Pipeline; |
| | 11 | |
|
| | 12 | | namespace Azure.Identity |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Provides a default <see cref="TokenCredential"/> authentication flow for applications that will be deployed to A |
| | 16 | | /// types if enabled will be tried, in order: |
| | 17 | | /// <list type="bullet"> |
| | 18 | | /// <item><description><see cref="EnvironmentCredential"/></description></item> |
| | 19 | | /// <item><description><see cref="ManagedIdentityCredential"/></description></item> |
| | 20 | | /// <item><description><see cref="SharedTokenCacheCredential"/></description></item> |
| | 21 | | /// <item><description><see cref="VisualStudioCredential"/></description></item> |
| | 22 | | /// <item><description><see cref="VisualStudioCodeCredential"/></description></item> |
| | 23 | | /// <item><description><see cref="AzureCliCredential"/></description></item> |
| | 24 | | /// <item><description><see cref="InteractiveBrowserCredential"/></description></item> |
| | 25 | | /// </list> |
| | 26 | | /// Consult the documentation of these credential types for more information on how they attempt authentication. |
| | 27 | | /// </summary> |
| | 28 | | /// <remarks> |
| | 29 | | /// Note that credentials requiring user interaction, such as the <see cref="InteractiveBrowserCredential"/>, are no |
| | 30 | | /// constructing the <see cref="DefaultAzureCredential"/> either by setting the includeInteractiveCredentials parame |
| | 31 | | /// <see cref="DefaultAzureCredentialOptions.ExcludeInteractiveBrowserCredential"/> property to false when passing < |
| | 32 | | /// </remarks> |
| | 33 | | public class DefaultAzureCredential : TokenCredential |
| | 34 | | { |
| | 35 | | private const string DefaultExceptionMessage = "DefaultAzureCredential failed to retrieve a token from the inclu |
| | 36 | | private const string UnhandledExceptionMessage = "DefaultAzureCredential authentication failed."; |
| 2 | 37 | | private static readonly TokenCredential[] s_defaultCredentialChain = GetDefaultAzureCredentialChain(new DefaultA |
| | 38 | |
|
| | 39 | | private readonly CredentialPipeline _pipeline; |
| | 40 | | private readonly AsyncLockWithValue<TokenCredential> _credentialLock; |
| | 41 | |
|
| | 42 | | private TokenCredential[] _sources; |
| | 43 | |
|
| 64 | 44 | | internal DefaultAzureCredential() : this(false) { } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Creates an instance of the DefaultAzureCredential class. |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="includeInteractiveCredentials">Specifies whether credentials requiring user interaction will be |
| | 50 | | public DefaultAzureCredential(bool includeInteractiveCredentials = false) |
| 40 | 51 | | : this(includeInteractiveCredentials ? new DefaultAzureCredentialOptions { ExcludeInteractiveBrowserCredenti |
| | 52 | | { |
| 40 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Creates an instance of the <see cref="DefaultAzureCredential"/> class. |
| | 57 | | /// </summary> |
| | 58 | | /// <param name="options">Options that configure the management of the requests sent to Azure Active Directory s |
| | 59 | | public DefaultAzureCredential(DefaultAzureCredentialOptions options) |
| 44 | 60 | | : this(new DefaultAzureCredentialFactory(options), options) |
| | 61 | | { |
| 40 | 62 | | } |
| | 63 | |
|
| 1180 | 64 | | internal DefaultAzureCredential(DefaultAzureCredentialFactory factory, DefaultAzureCredentialOptions options) |
| | 65 | | { |
| 1180 | 66 | | _pipeline = factory.Pipeline; |
| 1180 | 67 | | _sources = GetDefaultAzureCredentialChain(factory, options); |
| 1176 | 68 | | _credentialLock = new AsyncLockWithValue<TokenCredential>(); |
| 1176 | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Sequentially calls <see cref="TokenCredential.GetToken"/> on all the included credentials in the order <see |
| | 73 | | /// and <see cref="InteractiveBrowserCredential"/> returning the first successfully obtained <see cref="AccessTo |
| | 74 | | /// </summary> |
| | 75 | | /// <remarks> |
| | 76 | | /// Note that credentials requiring user interaction, such as the <see cref="InteractiveBrowserCredential"/>, ar |
| | 77 | | /// </remarks> |
| | 78 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | 79 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 80 | | /// <returns>The first <see cref="AccessToken"/> returned by the specified sources. Any credential which raises |
| | 81 | | public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken = d |
| | 82 | | { |
| 50 | 83 | | return GetTokenImplAsync(false, requestContext, cancellationToken).EnsureCompleted(); |
| | 84 | | } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Sequentially calls <see cref="TokenCredential.GetToken"/> on all the included credentials in the order <see |
| | 88 | | /// and <see cref="InteractiveBrowserCredential"/> returning the first successfully obtained <see cref="AccessTo |
| | 89 | | /// </summary> |
| | 90 | | /// <remarks> |
| | 91 | | /// Note that credentials requiring user interaction, such as the <see cref="InteractiveBrowserCredential"/>, ar |
| | 92 | | /// </remarks> |
| | 93 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | 94 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 95 | | /// <returns>The first <see cref="AccessToken"/> returned by the specified sources. Any credential which raises |
| | 96 | | public override async ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken |
| | 97 | | { |
| 642 | 98 | | return await GetTokenImplAsync(true, requestContext, cancellationToken).ConfigureAwait(false); |
| 102 | 99 | | } |
| | 100 | |
|
| | 101 | | private async ValueTask<AccessToken> GetTokenImplAsync(bool async, TokenRequestContext requestContext, Cancellat |
| | 102 | | { |
| 692 | 103 | | using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScopeGroup("DefaultAzureCredential.GetToken", |
| | 104 | |
|
| | 105 | | try |
| | 106 | | { |
| 692 | 107 | | using var asyncLock = await _credentialLock.GetLockOrValueAsync(async, cancellationToken).ConfigureAwait |
| | 108 | |
|
| | 109 | | AccessToken token; |
| 692 | 110 | | if (asyncLock.HasValue) |
| | 111 | | { |
| 98 | 112 | | token = await GetTokenFromCredentialAsync(asyncLock.Value, requestContext, async, cancellationToken) |
| | 113 | | } |
| | 114 | | else |
| | 115 | | { |
| | 116 | | TokenCredential credential; |
| 594 | 117 | | (token, credential) = await GetTokenFromSourcesAsync(_sources, requestContext, async, cancellationTo |
| 48 | 118 | | _sources = default; |
| 48 | 119 | | asyncLock.SetValue(credential); |
| | 120 | | } |
| | 121 | |
|
| 142 | 122 | | return scope.Succeeded(token); |
| | 123 | | } |
| 550 | 124 | | catch (Exception e) |
| | 125 | | { |
| 550 | 126 | | throw scope.FailWrapAndThrow(e); |
| | 127 | | } |
| 142 | 128 | | } |
| | 129 | |
|
| | 130 | | private static async ValueTask<AccessToken> GetTokenFromCredentialAsync(TokenCredential credential, TokenRequest |
| | 131 | | { |
| | 132 | | try |
| | 133 | | { |
| 98 | 134 | | return async |
| 98 | 135 | | ? await credential.GetTokenAsync(requestContext, cancellationToken).ConfigureAwait(false) |
| 98 | 136 | | : credential.GetToken(requestContext, cancellationToken); |
| | 137 | | } |
| 4 | 138 | | catch (Exception e) when (!(e is CredentialUnavailableException)) |
| | 139 | | { |
| 4 | 140 | | throw new AuthenticationFailedException(UnhandledExceptionMessage, e); |
| | 141 | | } |
| 94 | 142 | | } |
| | 143 | |
|
| | 144 | | private static async ValueTask<(AccessToken, TokenCredential)> GetTokenFromSourcesAsync(TokenCredential[] source |
| | 145 | | { |
| 594 | 146 | | List<AuthenticationFailedException> exceptions = new List<AuthenticationFailedException>(); |
| | 147 | |
|
| 5268 | 148 | | for (var i = 0; i < sources.Length && sources[i] != null; i++) |
| | 149 | | { |
| | 150 | | try |
| | 151 | | { |
| 2116 | 152 | | AccessToken token = async |
| 2116 | 153 | | ? await sources[i].GetTokenAsync(requestContext, cancellationToken).ConfigureAwait(false) |
| 2116 | 154 | | : sources[i].GetToken(requestContext, cancellationToken); |
| | 155 | |
|
| 48 | 156 | | return (token, sources[i]); |
| | 157 | | } |
| 2040 | 158 | | catch (AuthenticationFailedException e) |
| | 159 | | { |
| 2040 | 160 | | exceptions.Add(e); |
| 2040 | 161 | | } |
| | 162 | | } |
| | 163 | |
|
| | 164 | | // Build the credential unavailable message, this code is only reachable if all credentials throw Authentica |
| 518 | 165 | | StringBuilder errorMsg = new StringBuilder(DefaultExceptionMessage); |
| | 166 | |
|
| 518 | 167 | | bool allCredentialUnavailableException = true; |
| 4692 | 168 | | foreach (AuthenticationFailedException ex in exceptions) |
| | 169 | | { |
| 1828 | 170 | | allCredentialUnavailableException &= ex is CredentialUnavailableException; |
| 1828 | 171 | | errorMsg.Append(Environment.NewLine).Append("- ").Append(ex.Message); |
| | 172 | | } |
| | 173 | |
|
| | 174 | | // If all credentials have thrown CredentialUnavailableException, throw CredentialUnavailableException, |
| | 175 | | // otherwise throw AuthenticationFailedException |
| 518 | 176 | | throw allCredentialUnavailableException |
| 518 | 177 | | ? new CredentialUnavailableException(errorMsg.ToString()) |
| 518 | 178 | | : new AuthenticationFailedException(errorMsg.ToString()); |
| 48 | 179 | | } |
| | 180 | |
|
| | 181 | | private static TokenCredential[] GetDefaultAzureCredentialChain(DefaultAzureCredentialFactory factory, DefaultAz |
| | 182 | | { |
| 1182 | 183 | | if (options is null) |
| | 184 | | { |
| 36 | 185 | | return s_defaultCredentialChain; |
| | 186 | | } |
| | 187 | |
|
| 1146 | 188 | | int i = 0; |
| 1146 | 189 | | TokenCredential[] chain = new TokenCredential[7]; |
| | 190 | |
|
| 1146 | 191 | | if (!options.ExcludeEnvironmentCredential) |
| | 192 | | { |
| 578 | 193 | | chain[i++] = factory.CreateEnvironmentCredential(); |
| | 194 | | } |
| | 195 | |
|
| 1146 | 196 | | if (!options.ExcludeManagedIdentityCredential) |
| | 197 | | { |
| 634 | 198 | | chain[i++] = factory.CreateManagedIdentityCredential(options.ManagedIdentityClientId); |
| | 199 | | } |
| | 200 | |
|
| 1146 | 201 | | if (!options.ExcludeSharedTokenCacheCredential) |
| | 202 | | { |
| 610 | 203 | | chain[i++] = factory.CreateSharedTokenCacheCredential(options.SharedTokenCacheTenantId, options.SharedTo |
| | 204 | | } |
| | 205 | |
|
| 1146 | 206 | | if (!options.ExcludeVisualStudioCredential) |
| | 207 | | { |
| 638 | 208 | | chain[i++] = factory.CreateVisualStudioCredential(options.VisualStudioTenantId); |
| | 209 | | } |
| | 210 | |
|
| 1146 | 211 | | if (!options.ExcludeVisualStudioCodeCredential) |
| | 212 | | { |
| 638 | 213 | | chain[i++] = factory.CreateVisualStudioCodeCredential(options.VisualStudioCodeTenantId); |
| | 214 | | } |
| | 215 | |
|
| 1146 | 216 | | if (!options.ExcludeAzureCliCredential) |
| | 217 | | { |
| 606 | 218 | | chain[i++] = factory.CreateAzureCliCredential(); |
| | 219 | | } |
| | 220 | |
|
| 1146 | 221 | | if (!options.ExcludeInteractiveBrowserCredential) |
| | 222 | | { |
| 608 | 223 | | chain[i++] = factory.CreateInteractiveBrowserCredential(options.InteractiveBrowserTenantId); |
| | 224 | | } |
| | 225 | |
|
| 1146 | 226 | | if (i == 0) |
| | 227 | | { |
| 4 | 228 | | throw new ArgumentException("At least one credential type must be included in the authentication flow.", |
| | 229 | | } |
| | 230 | |
|
| 1142 | 231 | | return chain; |
| | 232 | | } |
| | 233 | | } |
| | 234 | | } |