-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathClientCredentialRequest.cs
More file actions
506 lines (442 loc) · 23.7 KB
/
ClientCredentialRequest.cs
File metadata and controls
506 lines (442 loc) · 23.7 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Identity.Client.ApiConfig.Parameters;
using Microsoft.Identity.Client.Cache.Items;
using Microsoft.Identity.Client.Core;
using Microsoft.Identity.Client.Extensibility;
using Microsoft.Identity.Client.Instance;
using Microsoft.Identity.Client.OAuth2;
using Microsoft.Identity.Client.PlatformsCommon.Interfaces;
using Microsoft.Identity.Client.Utils;
namespace Microsoft.Identity.Client.Internal.Requests
{
internal class ClientCredentialRequest : RequestBase
{
private readonly AcquireTokenForClientParameters _clientParameters;
private static readonly SemaphoreSlim s_semaphoreSlim = new SemaphoreSlim(1, 1);
private readonly ICryptographyManager _cryptoManager;
public ClientCredentialRequest(
IServiceBundle serviceBundle,
AuthenticationRequestParameters authenticationRequestParameters,
AcquireTokenForClientParameters clientParameters)
: base(serviceBundle, authenticationRequestParameters, clientParameters)
{
_clientParameters = clientParameters;
_cryptoManager = serviceBundle.PlatformProxy.CryptographyManager;
}
protected override async Task<AuthenticationResult> ExecuteAsync(CancellationToken cancellationToken)
{
if (AuthenticationRequestParameters.Scope == null || AuthenticationRequestParameters.Scope.Count == 0)
{
throw new MsalClientException(
MsalError.ScopesRequired,
MsalErrorMessage.ScopesRequired);
}
ILoggerAdapter logger = AuthenticationRequestParameters.RequestContext.Logger;
if (AuthenticationRequestParameters.Authority is AadAuthority aadAuthority &&
aadAuthority.IsCommonOrOrganizationsTenant())
{
logger.Error(MsalErrorMessage.ClientCredentialWrongAuthority);
}
AuthenticationResult authResult;
if (IsInternalCacheDisabled)
{
AuthenticationRequestParameters.RequestContext.ApiEvent.CacheInfo = CacheRefreshReason.CacheDisabled;
return await GetAccessTokenAsync(cancellationToken, logger).ConfigureAwait(false);
}
// Skip cache if either:
// 1) ForceRefresh is set, or
// 2) Claims are specified and there is no AccessTokenHashToRefresh.
// This ensures that when both claims and AccessTokenHashToRefresh are set,
// we do NOT skip the cache, allowing MSAL to attempt retrieving a matching
// cached token by the provided hash before requesting a new token.
bool skipCache = _clientParameters.ForceRefresh ||
(!string.IsNullOrEmpty(AuthenticationRequestParameters.Claims) &&
string.IsNullOrEmpty(_clientParameters.AccessTokenHashToRefresh));
if (skipCache)
{
AuthenticationRequestParameters.RequestContext.ApiEvent.CacheInfo = CacheRefreshReason.ForceRefreshOrClaims;
logger.Info("[ClientCredentialRequest] Skipped looking for a cached access token because ForceRefresh was requested, or there are Claims but no AccessTokenHashToRefresh.");
authResult = await GetAccessTokenAsync(cancellationToken, logger).ConfigureAwait(false);
return authResult;
}
MsalAccessTokenCacheItem cachedAccessTokenItem = await GetCachedAccessTokenAsync().ConfigureAwait(false);
cachedAccessTokenItem = await ValidateCachedAccessTokenAsync(
AuthenticationRequestParameters, cachedAccessTokenItem, nameof(ClientCredentialRequest)).ConfigureAwait(false);
// No access token or cached access token needs to be refreshed
if (cachedAccessTokenItem != null)
{
authResult = await CreateAuthenticationResultFromCacheAsync(cachedAccessTokenItem).ConfigureAwait(false);
try
{
var proactivelyRefresh = SilentRequestHelper.NeedsRefresh(cachedAccessTokenItem);
// If needed, refreshes token in the background
if (proactivelyRefresh)
{
AuthenticationRequestParameters.RequestContext.ApiEvent.CacheInfo = CacheRefreshReason.ProactivelyRefreshed;
SilentRequestHelper.ProcessFetchInBackground(
cachedAccessTokenItem,
() =>
{
// Use a linked token source, in case the original cancellation token source is disposed before this background task completes.
using var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
return GetAccessTokenAsync(tokenSource.Token, logger);
}, logger, ServiceBundle, AuthenticationRequestParameters.RequestContext.ApiEvent,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkApiId,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkVersion);
}
}
catch (MsalServiceException e)
{
return await HandleTokenRefreshErrorAsync(e, cachedAccessTokenItem, cancellationToken).ConfigureAwait(false);
}
}
else
{
// No access token in the cache
if (AuthenticationRequestParameters.RequestContext.ApiEvent.CacheInfo != CacheRefreshReason.Expired)
{
AuthenticationRequestParameters.RequestContext.ApiEvent.CacheInfo = CacheRefreshReason.NoCachedAccessToken;
}
authResult = await GetAccessTokenAsync(cancellationToken, logger).ConfigureAwait(false);
}
return authResult;
}
private async Task<AuthenticationResult> GetAccessTokenAsync(
CancellationToken cancellationToken,
ILoggerAdapter logger)
{
await ResolveAuthorityAsync().ConfigureAwait(false);
AuthenticationResult authResult = null;
int retryCount = 0;
// Retry loop using the retry callback if configured
while (true)
{
try
{
// Get a token from AAD
if (ServiceBundle.Config.AppTokenProvider == null)
{
logger.Verbose(() => "[ClientCredentialRequest] Sending token request to AAD.");
MsalTokenResponse msalTokenResponse = await SendTokenRequestAsync(GetBodyParameters(), cancellationToken).ConfigureAwait(false);
authResult = await CacheTokenResponseAndCreateAuthenticationResultAsync(msalTokenResponse, cancellationToken).ConfigureAwait(false);
}
else
{
// Get a token from the app provider delegate
authResult = await GetAccessTokenFromAppProviderAsync(cancellationToken, logger)
.ConfigureAwait(false);
}
// Success - invoke OnCompletion callback if configured
await InvokeOnCompletionCallbackAsync(authResult, exception: null, logger).ConfigureAwait(false);
return authResult;
}
catch (MsalServiceException serviceEx)
{
// Check if OnMsalServiceFailure is configured
if (AuthenticationRequestParameters.AppConfig.OnMsalServiceFailure != null)
{
logger.Info("[ClientCredentialRequest] MsalServiceException caught. Invoking OnMsalServiceFailure.");
bool shouldRetry = await InvokeOnMsalServiceFailureCallbackAsync(serviceEx, logger)
.ConfigureAwait(false);
if (shouldRetry)
{
retryCount++;
logger.Info($"[ClientCredentialRequest] OnMsalServiceFailure returned true. Retrying token request (Retry #{retryCount}).");
continue; // Retry the loop
}
logger.Info("[ClientCredentialRequest] OnMsalServiceFailure returned false. Propagating exception.");
}
// Invoke OnCompletion callback with failure result
await InvokeOnCompletionCallbackAsync(authResult: null, exception: serviceEx, logger).ConfigureAwait(false);
// Re-throw if no callback or callback returned false
throw;
}
catch (MsalException ex)
{
// For non-service exceptions (MsalClientException, etc.), invoke OnCompletion and re-throw
await InvokeOnCompletionCallbackAsync(authResult: null, exception: ex, logger).ConfigureAwait(false);
throw;
}
}
}
private async Task<AuthenticationResult> GetAccessTokenFromAppProviderAsync(
CancellationToken cancellationToken,
ILoggerAdapter logger)
{
// Get a token from the app provider delegate
AuthenticationResult authResult;
MsalAccessTokenCacheItem cachedAccessTokenItem;
// Allow only one call to the provider
logger.Verbose(() => "[ClientCredentialRequest] Entering client credential request semaphore.");
await s_semaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false);
logger.Verbose(() => "[ClientCredentialRequest] Entered client credential request semaphore.");
try
{
// Bypass cache and send request to token endpoint, when
// 1. Force refresh is requested, or
// 2. Claims are passed, or
// 3. If the access token needs to be refreshed proactively.
if (_clientParameters.ForceRefresh ||
AuthenticationRequestParameters.RequestContext.ApiEvent.CacheInfo == CacheRefreshReason.ProactivelyRefreshed ||
!string.IsNullOrEmpty(AuthenticationRequestParameters.Claims))
{
authResult = await SendTokenRequestToAppTokenProviderAsync(logger, cancellationToken).ConfigureAwait(false);
}
else
{
// Check the cache again after acquiring the semaphore in case the previous request cached a new token.
cachedAccessTokenItem = await GetCachedAccessTokenAsync().ConfigureAwait(false);
if (cachedAccessTokenItem == null)
{
authResult = await SendTokenRequestToAppTokenProviderAsync(logger, cancellationToken).ConfigureAwait(false);
}
else
{
logger.Verbose(() => "[ClientCredentialRequest] Checking for a cached access token.");
authResult = await CreateAuthenticationResultFromCacheAsync(cachedAccessTokenItem).ConfigureAwait(false);
}
}
return authResult;
}
finally
{
s_semaphoreSlim.Release();
logger.Verbose(() => "[ClientCredentialRequest] Released client credential request semaphore.");
}
}
/// <summary>
/// Invokes the OnMsalServiceFailure if configured.
/// Returns true if the request should be retried, false otherwise.
/// </summary>
private async Task<bool> InvokeOnMsalServiceFailureCallbackAsync(
MsalServiceException serviceException,
ILoggerAdapter logger)
{
try
{
var tokenEndpoint = await AuthenticationRequestParameters.Authority.GetTokenEndpointAsync(AuthenticationRequestParameters.RequestContext).ConfigureAwait(false);
var options = new AssertionRequestOptions(
AuthenticationRequestParameters.AppConfig,
tokenEndpoint,
AuthenticationRequestParameters.AuthorityManager.Authority.TenantId,
AuthenticationRequestParameters.RequestContext.CorrelationId);
var executionResult = new ExecutionResult
{
Successful = false,
Result = null,
Exception = serviceException,
ClientCertificate = AuthenticationRequestParameters.ResolvedCertificate
};
bool shouldRetry = await AuthenticationRequestParameters.AppConfig
.OnMsalServiceFailure(options, executionResult)
.ConfigureAwait(false);
logger.Verbose(() => $"[ClientCredentialRequest] OnMsalServiceFailure returned: {shouldRetry}");
return shouldRetry;
}
catch (Exception ex)
{
// If the callback throws, log and don't retry
logger.Error($"[ClientCredentialRequest] OnMsalServiceFailure threw an exception: {ex.Message}");
logger.ErrorPii(ex);
return false;
}
}
/// <summary>
/// Invokes the OnCompletion if configured.
/// Exceptions from the callback are caught and logged to prevent disrupting the authentication flow.
/// </summary>
private async Task InvokeOnCompletionCallbackAsync(
AuthenticationResult authResult,
MsalException exception,
ILoggerAdapter logger)
{
if (AuthenticationRequestParameters.AppConfig.OnCompletion == null)
{
return;
}
try
{
logger.Verbose(() => "[ClientCredentialRequest] Invoking OnCompletion callback.");
var tokenEndpoint = await AuthenticationRequestParameters.Authority.GetTokenEndpointAsync(AuthenticationRequestParameters.RequestContext).ConfigureAwait(false);
var options = new AssertionRequestOptions(
AuthenticationRequestParameters.AppConfig,
tokenEndpoint,
AuthenticationRequestParameters.AuthorityManager.Authority.TenantId,
AuthenticationRequestParameters.RequestContext.CorrelationId);
var executionResult = new ExecutionResult
{
Successful = authResult != null,
Result = authResult,
Exception = exception,
ClientCertificate = AuthenticationRequestParameters.ResolvedCertificate
};
await AuthenticationRequestParameters.AppConfig
.OnCompletion(options, executionResult)
.ConfigureAwait(false);
logger.Verbose(() => "[ClientCredentialRequest] OnCompletion callback completed successfully.");
}
catch (Exception ex)
{
// Catch and log any exceptions from the observer callback
// Do not propagate - observer should not disrupt authentication flow
logger.Error($"[ClientCredentialRequest] OnCompletion callback threw an exception: {ex.Message}");
logger.ErrorPii(ex);
}
}
private async Task<AuthenticationResult> SendTokenRequestToAppTokenProviderAsync(ILoggerAdapter logger,
CancellationToken cancellationToken)
{
logger.Info("[ClientCredentialRequest] Acquiring a token from the token provider.");
AppTokenProviderParameters appTokenProviderParameters = new AppTokenProviderParameters
{
Scopes = GetOverriddenScopes(AuthenticationRequestParameters.Scope),
CorrelationId = AuthenticationRequestParameters.RequestContext.CorrelationId.ToString(),
Claims = AuthenticationRequestParameters.Claims,
TenantId = AuthenticationRequestParameters.Authority.TenantId,
CancellationToken = cancellationToken,
};
AppTokenProviderResult externalToken = await ServiceBundle.Config.AppTokenProvider(appTokenProviderParameters).ConfigureAwait(false);
var tokenResponse = MsalTokenResponse.CreateFromAppProviderResponse(externalToken);
tokenResponse.Scope = appTokenProviderParameters.Scopes.AsSingleString();
tokenResponse.CorrelationId = appTokenProviderParameters.CorrelationId;
AuthenticationResult authResult = await CacheTokenResponseAndCreateAuthenticationResultAsync(tokenResponse, cancellationToken)
.ConfigureAwait(false);
return authResult;
}
/// <summary>
/// Checks if the token should be used from the cache and returns the cached access token if applicable.
/// </summary>
/// <returns></returns>
private async Task<MsalAccessTokenCacheItem> GetCachedAccessTokenAsync()
{
// Fetch the cache item (could be null if none found).
MsalAccessTokenCacheItem cacheItem =
await CacheManager.FindAccessTokenAsync().ConfigureAwait(false);
// If the item fails any checks (null, or hash mismatch),
if (!ShouldUseCachedToken(cacheItem))
{
return null;
}
// Otherwise, record a successful cache hit and return the token.
MarkAccessTokenAsCacheHit();
return cacheItem;
}
/// <summary>
/// Checks if the token should be used from the cache.
/// </summary>
/// <param name="cacheItem"></param>
/// <returns></returns>
private bool ShouldUseCachedToken(MsalAccessTokenCacheItem cacheItem)
{
// 1) No cached item
if (cacheItem == null)
{
return false;
}
// 2) If an mTLS cert is supplied for THIS request, reuse cache only if
// the cached token's KeyId matches the one provided in the request.
X509Certificate2 requestCert = AuthenticationRequestParameters.MtlsCertificate;
if (requestCert != null && AuthenticationRequestParameters.IsMtlsPopRequested)
{
string expectedKid = CoreHelpers.ComputeX5tS256KeyId(requestCert);
// If the certificate cannot produce a valid KeyId (SPKI-SHA256), expectedKid will be null or empty.
// In this case, the cache will be bypassed, as we cannot safely match the cached token to the certificate.
if (!string.Equals(cacheItem.KeyId, expectedKid, StringComparison.Ordinal))
{
AuthenticationRequestParameters.RequestContext.Logger.Verbose(() =>
"[ClientCredentialRequest] Cached token KeyId does not match request certificate (SPKI-SHA256 mismatch). Bypassing cache.");
return false;
}
AuthenticationRequestParameters.RequestContext.Logger.Verbose(() =>
"[ClientCredentialRequest] Cached token KeyId matches request certificate (SPKI-SHA256). Using cached token.");
}
// 3) If the token's hash matches AccessTokenHashToRefresh, ignore it
if (!string.IsNullOrEmpty(_clientParameters.AccessTokenHashToRefresh) &&
IsMatchingTokenHash(cacheItem.Secret, _clientParameters.AccessTokenHashToRefresh))
{
AuthenticationRequestParameters.RequestContext.Logger.Info(
"[ClientCredentialRequest] A cached token was found and its hash matches AccessTokenHashToRefresh, so it is ignored.");
return false;
}
return true;
}
/// <summary>
/// Checks if the token hash matches the hash provided in AccessTokenHashToRefresh.
/// </summary>
/// <param name="tokenSecret"></param>
/// <param name="accessTokenHashToRefresh"></param>
/// <returns></returns>
private bool IsMatchingTokenHash(string tokenSecret, string accessTokenHashToRefresh)
{
string cachedTokenHash = _cryptoManager.CreateSha256HashHex(tokenSecret);
return string.Equals(cachedTokenHash, accessTokenHashToRefresh, StringComparison.Ordinal);
}
/// <summary>
/// Marks the request as a cache hit and increments the cache hit count.
/// </summary>
private void MarkAccessTokenAsCacheHit()
{
AuthenticationRequestParameters.RequestContext.ApiEvent.IsAccessTokenCacheHit = true;
Metrics.IncrementTotalAccessTokensFromCache();
}
/// <summary>
/// returns the cached access token item
/// </summary>
/// <param name="cachedAccessTokenItem"></param>
/// <returns></returns>
private Task<AuthenticationResult> CreateAuthenticationResultFromCacheAsync(MsalAccessTokenCacheItem cachedAccessTokenItem)
{
return AuthenticationResult.CreateAsync(
cachedAccessTokenItem,
null,
AuthenticationRequestParameters.AuthenticationScheme,
AuthenticationRequestParameters.RequestContext.CorrelationId,
TokenSource.Cache,
AuthenticationRequestParameters.RequestContext.ApiEvent,
account: null,
spaAuthCode: null,
additionalResponseParameters: null);
}
/// <summary>
/// Gets overridden scopes for client credentials flow
/// </summary>
/// <param name="inputScopes"></param>
/// <returns></returns>
protected override SortedSet<string> GetOverriddenScopes(ISet<string> inputScopes)
{
// Client credentials should not add the reserved scopes
// ("openid", "profile", and "offline_access")
// because the access token is on behalf of an app (no profile, no ID token, no refresh token)
return new SortedSet<string>(inputScopes);
}
/// <summary>
/// Gets the body parameters for the client credentials flow
/// </summary>
/// <returns></returns>
private Dictionary<string, string> GetBodyParameters()
{
var dict = new Dictionary<string, string>
{
[OAuth2Parameter.GrantType] = OAuth2GrantType.ClientCredentials,
[OAuth2Parameter.Scope] = AuthenticationRequestParameters.Scope.AsSingleString(),
[OAuth2Parameter.ClientInfo] = "2"
};
return dict;
}
/// <summary>
/// Gets the CCS header for the client credentials flow
/// </summary>
/// <param name="additionalBodyParameters"></param>
/// <returns></returns>
protected override KeyValuePair<string, string>? GetCcsHeader(IDictionary<string, string> additionalBodyParameters)
{
return null;
}
}
}