-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
AuthorizationCodeCredential.cs
199 lines (183 loc) · 11.2 KB
/
AuthorizationCodeCredential.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Microsoft.Identity.Client;
namespace Azure.Identity
{
/// <summary>
/// Authenticates by redeeming an authorization code previously obtained from Microsoft Entra ID. See
/// <seealso href="https://learn.microsoft.com/entra/identity-platform/v2-oauth2-auth-code-flow" /> for more information
/// about the authorization code authentication flow.
/// </summary>
public class AuthorizationCodeCredential : TokenCredential
{
private readonly string _authCode;
private readonly string _clientId;
private readonly CredentialPipeline _pipeline;
private AuthenticationRecord _record;
internal MsalConfidentialClient Client { get; }
private readonly string _redirectUri;
private readonly string _tenantId;
internal readonly string[] AdditionallyAllowedTenantIds;
internal TenantIdResolverBase TenantIdResolver { get; }
/// <summary>
/// Protected constructor for mocking.
/// </summary>
protected AuthorizationCodeCredential()
{
}
/// <summary>
/// Creates an instance of the ClientSecretCredential with the details needed to authenticate against Microsoft Entra ID with a prefetched authorization code.
/// </summary>
/// <param name="tenantId">The Microsoft Entra tenant (directory) ID of the service principal.</param>
/// <param name="clientId">The client (application) ID of the service principal</param>
/// <param name="clientSecret">A client secret that was generated for the App Registration used to authenticate the client.</param>
/// <param name="authorizationCode">The authorization code obtained from a call to authorize. The code should be obtained with all required scopes.
/// See https://learn.microsoft.com/entra/identity-platform/v2-oauth2-auth-code-flow for more information.</param>
public AuthorizationCodeCredential(string tenantId, string clientId, string clientSecret, string authorizationCode)
: this(tenantId, clientId, clientSecret, authorizationCode, null)
{
}
/// <summary>
/// Creates an instance of the ClientSecretCredential with the details needed to authenticate against Microsoft Entra ID with a prefetched authorization code.
/// </summary>
/// <param name="tenantId">The Microsoft Entra tenant (directory) ID of the service principal.</param>
/// <param name="clientId">The client (application) ID of the service principal</param>
/// <param name="clientSecret">A client secret that was generated for the App Registration used to authenticate the client.</param>
/// <param name="authorizationCode">The authorization code obtained from a call to authorize. The code should be obtained with all required scopes.
/// See <see href="https://learn.microsoft.com/entra/identity-platform/v2-oauth2-auth-code-flow" /> for more information.</param>
/// <param name="options">Options that allow to configure the management of the requests sent to Microsoft Entra ID.</param>
public AuthorizationCodeCredential(
string tenantId,
string clientId,
string clientSecret,
string authorizationCode,
AuthorizationCodeCredentialOptions options) : this(tenantId, clientId, clientSecret, authorizationCode, options, null)
{ }
/// <summary>
/// Creates an instance of the ClientSecretCredential with the details needed to authenticate against Microsoft Entra ID with a prefetched authorization code.
/// </summary>
/// <param name="tenantId">The Microsoft Entra tenant (directory) ID of the service principal.</param>
/// <param name="clientId">The client (application) ID of the service principal</param>
/// <param name="clientSecret">A client secret that was generated for the App Registration used to authenticate the client.</param>
/// <param name="authorizationCode">The authorization code obtained from a call to authorize. The code should be obtained with all required scopes.
/// See <see href="https://learn.microsoft.com/entra/identity-platform/v2-oauth2-auth-code-flow" /> for more information.</param>
/// <param name="options">Options that allow to configure the management of the requests sent to Microsoft Entra ID.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public AuthorizationCodeCredential(string tenantId, string clientId, string clientSecret, string authorizationCode, TokenCredentialOptions options)
: this(tenantId, clientId, clientSecret, authorizationCode, options, null)
{ }
internal AuthorizationCodeCredential(string tenantId, string clientId, string clientSecret, string authorizationCode, TokenCredentialOptions options, MsalConfidentialClient client, CredentialPipeline pipeline = null)
{
Validations.ValidateTenantId(tenantId, nameof(tenantId));
_tenantId = tenantId;
Argument.AssertNotNull(clientSecret, nameof(clientSecret));
Argument.AssertNotNull(clientId, nameof(clientId));
Argument.AssertNotNull(authorizationCode, nameof(authorizationCode));
_clientId = clientId;
_authCode = authorizationCode;
_pipeline = pipeline ?? CredentialPipeline.GetInstance(options ?? new TokenCredentialOptions());
_redirectUri = options switch
{
AuthorizationCodeCredentialOptions o => o.RedirectUri?.AbsoluteUri,
_ => null
};
Client = client ??
new MsalConfidentialClient(
_pipeline,
tenantId,
clientId,
clientSecret,
_redirectUri,
options);
TenantIdResolver = options?.TenantIdResolver ?? TenantIdResolverBase.Default;
AdditionallyAllowedTenantIds = TenantIdResolver.ResolveAddionallyAllowedTenantIds((options as ISupportsAdditionallyAllowedTenants)?.AdditionallyAllowedTenants);
}
/// <summary>
/// Obtains a token from Microsoft Entra ID, using the specified authorization code to authenticate. Acquired tokens
/// are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential
/// instances to optimize cache effectiveness.
/// </summary>
/// <param name="requestContext">The details of the authentication request.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls.</returns>
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken = default)
{
return GetTokenImplAsync(false, requestContext, cancellationToken).EnsureCompleted();
}
/// <summary>
/// Obtains a token from Microsoft Entra ID, using the specified authorization code to authenticate. Acquired tokens
/// are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential
/// instances to optimize cache effectiveness.
/// </summary>
/// <param name="requestContext">The details of the authentication request.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls.</returns>
public override async ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken = default)
{
return await GetTokenImplAsync(true, requestContext, cancellationToken).ConfigureAwait(false);
}
private async ValueTask<AccessToken> GetTokenImplAsync(bool async, TokenRequestContext requestContext, CancellationToken cancellationToken = default)
{
using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope($"{nameof(AuthorizationCodeCredential)}.{nameof(GetToken)}", requestContext);
AccessToken token;
string tenantId = null;
try
{
tenantId = TenantIdResolver.Resolve(_tenantId, requestContext, AdditionallyAllowedTenantIds);
if (_record is null)
{
token = await AcquireTokenWithCode(async, requestContext, token, tenantId, cancellationToken).ConfigureAwait(false);
}
else
{
AuthenticationResult result = await Client
.AcquireTokenSilentAsync(requestContext.Scopes, (AuthenticationAccount)_record, tenantId, _redirectUri, requestContext.Claims, requestContext.IsCaeEnabled, async, cancellationToken)
.ConfigureAwait(false);
token = new AccessToken(result.AccessToken, result.ExpiresOn);
}
return scope.Succeeded(token);
}
catch (MsalUiRequiredException)
{
// This occurs when we have an auth record but the cae or ncae cache entry is missing
// fall through to the acquire call below
}
catch (Exception e)
{
throw scope.FailWrapAndThrow(e);
}
try
{
token = await AcquireTokenWithCode(async, requestContext, token, tenantId, cancellationToken).ConfigureAwait(false);
return scope.Succeeded(token);
}
catch (Exception e)
{
throw scope.FailWrapAndThrow(e);
}
}
private async Task<AccessToken> AcquireTokenWithCode(bool async, TokenRequestContext requestContext, AccessToken token, string tenantId, CancellationToken cancellationToken)
{
AuthenticationResult result = await Client
.AcquireTokenByAuthorizationCodeAsync(
scopes: requestContext.Scopes,
code: _authCode,
tenantId: tenantId,
redirectUri: _redirectUri,
claims: requestContext.Claims,
enableCae: requestContext.IsCaeEnabled,
async,
cancellationToken)
.ConfigureAwait(false);
_record = new AuthenticationRecord(result, _clientId);
token = new AccessToken(result.AccessToken, result.ExpiresOn);
return token;
}
}
}