-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathJsonWebTokenHandler.ValidateToken.cs
More file actions
635 lines (569 loc) · 33.1 KB
/
Copy pathJsonWebTokenHandler.ValidateToken.cs
File metadata and controls
635 lines (569 loc) · 33.1 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Abstractions;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Telemetry;
using Microsoft.IdentityModel.Tokens;
using TokenLogMessages = Microsoft.IdentityModel.Tokens.LogMessages;
namespace Microsoft.IdentityModel.JsonWebTokens
{
/// <remarks>This partial class contains methods and logic related to the validation of tokens.</remarks>
public partial class JsonWebTokenHandler : TokenHandler
{
internal Telemetry.ITelemetryClient _telemetryClient = new TelemetryClient();
/// <summary>
/// Returns a value that indicates if this handler can validate a <see cref="SecurityToken"/>.
/// </summary>
/// <returns><see langword="true"/> if this instance can validate a <see cref="JsonWebToken"/>.</returns>
public virtual bool CanValidateToken
{
get { return true; }
}
internal async ValueTask<TokenValidationResult> ValidateJWEAsync(
JsonWebToken jwtToken,
TokenValidationParameters validationParameters,
BaseConfiguration configuration)
{
try
{
TokenValidationResult tokenValidationResult = ReadToken(DecryptToken(jwtToken, validationParameters, configuration), validationParameters);
if (!tokenValidationResult.IsValid)
return tokenValidationResult;
tokenValidationResult = await ValidateJWSAsync(
tokenValidationResult.SecurityToken as JsonWebToken,
validationParameters,
configuration).ConfigureAwait(false);
if (!tokenValidationResult.IsValid)
return tokenValidationResult;
jwtToken.InnerToken = tokenValidationResult.SecurityToken as JsonWebToken;
jwtToken.Payload = (tokenValidationResult.SecurityToken as JsonWebToken).Payload;
return new TokenValidationResult
{
SecurityToken = jwtToken,
ClaimsIdentityNoLocking = tokenValidationResult.ClaimsIdentityNoLocking,
IsValid = true,
TokenType = tokenValidationResult.TokenType
};
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
return new TokenValidationResult
{
Exception = ex,
IsValid = false,
TokenOnFailedValidation = validationParameters.IncludeTokenOnFailedValidation ? jwtToken : null
};
}
}
internal async ValueTask<TokenValidationResult> ValidateJWSAsync(
JsonWebToken jsonWebToken,
TokenValidationParameters validationParameters,
BaseConfiguration configuration)
{
try
{
TokenValidationResult tokenValidationResult;
if (validationParameters.TransformBeforeSignatureValidation != null)
jsonWebToken = validationParameters.TransformBeforeSignatureValidation(jsonWebToken, validationParameters) as JsonWebToken;
if (validationParameters.SignatureValidator != null || validationParameters.SignatureValidatorUsingConfiguration != null)
{
var validatedToken = ValidateSignatureUsingDelegates(jsonWebToken, validationParameters);
tokenValidationResult = await ValidateTokenPayloadAsync(
validatedToken,
validationParameters,
configuration).ConfigureAwait(false);
Validators.ValidateIssuerSecurityKey(validatedToken.SigningKey, validatedToken, validationParameters);
}
else
{
if (validationParameters.ValidateSignatureLast)
{
tokenValidationResult = await ValidateTokenPayloadAsync(
jsonWebToken,
validationParameters,
configuration).ConfigureAwait(false);
if (tokenValidationResult.IsValid)
tokenValidationResult.SecurityToken = ValidateSignatureAndIssuerSecurityKey(jsonWebToken, validationParameters, configuration);
}
else
{
var validatedToken = ValidateSignatureAndIssuerSecurityKey(jsonWebToken, validationParameters, configuration);
tokenValidationResult = await ValidateTokenPayloadAsync(
validatedToken,
validationParameters,
configuration).ConfigureAwait(false);
}
}
return tokenValidationResult;
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
return new TokenValidationResult
{
Exception = ex,
IsValid = false,
TokenOnFailedValidation = validationParameters.IncludeTokenOnFailedValidation ? jsonWebToken : null
};
}
}
private static JsonWebToken ValidateSignatureAndIssuerSecurityKey(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
{
JsonWebToken validatedToken = ValidateSignature(jsonWebToken, validationParameters, configuration);
Validators.ValidateIssuerSecurityKey(validatedToken.SigningKey, jsonWebToken, validationParameters, configuration);
return validatedToken;
}
/// <summary>
/// Validates the JWT signature.
/// </summary>
private static JsonWebToken ValidateSignature(JsonWebToken jwtToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
{
bool kidMatched = false;
IEnumerable<SecurityKey> keys = null;
if (!jwtToken.IsSigned)
{
if (validationParameters.RequireSignedTokens)
throw LogHelper.LogExceptionMessage(
new SecurityTokenInvalidSignatureException(
LogHelper.FormatInvariant(
TokenLogMessages.IDX10504,
LogHelper.MarkAsSecurityArtifact(jwtToken, JwtTokenUtilities.SafeLogJwtToken))));
else
return jwtToken;
}
if (validationParameters.IssuerSigningKeyResolverUsingConfiguration != null)
{
keys = validationParameters.IssuerSigningKeyResolverUsingConfiguration(jwtToken.EncodedToken, jwtToken, jwtToken.Kid, validationParameters, configuration);
}
else if (validationParameters.IssuerSigningKeyResolver != null)
{
keys = validationParameters.IssuerSigningKeyResolver(jwtToken.EncodedToken, jwtToken, jwtToken.Kid, validationParameters);
}
else
{
var key = JwtTokenUtilities.ResolveTokenSigningKey(jwtToken.Kid, jwtToken.X5t, validationParameters, configuration);
if (key != null)
{
kidMatched = true;
keys = [key];
}
}
if (validationParameters.TryAllIssuerSigningKeys && keys.IsNullOrEmpty())
{
// control gets here if:
// 1. User specified delegate: IssuerSigningKeyResolver returned null
// 2. ResolveIssuerSigningKey returned null
// Try all the keys. This is the degenerate case, not concerned about perf.
keys = TokenUtilities.GetAllSigningKeys(configuration, validationParameters);
}
// keep track of exceptions thrown, keys that were tried
StringBuilder exceptionStrings = null;
StringBuilder keysAttempted = null;
var kidExists = !string.IsNullOrEmpty(jwtToken.Kid);
if (keys != null)
{
foreach (var key in keys)
{
#pragma warning disable CA1031 // Do not catch general exception types
try
{
if (ValidateSignature(jwtToken, key, validationParameters))
{
if (LogHelper.IsEnabled(EventLogLevel.Informational))
LogHelper.LogInformation(TokenLogMessages.IDX10242, jwtToken);
jwtToken.SigningKey = key;
return jwtToken;
}
}
catch (Exception ex)
{
(exceptionStrings ??= new StringBuilder()).AppendLine(ex.ToString());
}
#pragma warning restore CA1031 // Do not catch general exception types
if (key != null)
{
(keysAttempted ??= new StringBuilder()).Append(key.ToString()).Append(" , KeyId: ").AppendLine(key.KeyId);
if (kidExists && !kidMatched && key.KeyId != null)
kidMatched = jwtToken.Kid.Equals(key.KeyId, key is X509SecurityKey ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
}
}
}
// Get information on where keys used during token validation came from for debugging purposes.
var keysInTokenValidationParameters = TokenUtilities.GetAllSigningKeys(validationParameters: validationParameters);
var keysInConfiguration = TokenUtilities.GetAllSigningKeys(configuration);
var numKeysInTokenValidationParameters = keysInTokenValidationParameters.Count();
var numKeysInConfiguration = keysInConfiguration.Count();
if (kidExists)
{
if (kidMatched)
{
JsonWebToken localJwtToken = jwtToken; // avoid closure on non-exceptional path
var isKidInTVP = keysInTokenValidationParameters.Any(x => x.KeyId.Equals(localJwtToken.Kid));
var keyLocation = isKidInTVP ? "TokenValidationParameters" : "Configuration";
throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidSignatureException(LogHelper.FormatInvariant(TokenLogMessages.IDX10511,
LogHelper.MarkAsNonPII((object)keysAttempted ?? ""),
LogHelper.MarkAsNonPII(numKeysInTokenValidationParameters),
LogHelper.MarkAsNonPII(numKeysInConfiguration),
LogHelper.MarkAsNonPII(keyLocation),
LogHelper.MarkAsNonPII(jwtToken.Kid),
(object)exceptionStrings ?? "",
jwtToken)));
}
if (!validationParameters.ValidateSignatureLast)
{
InternalValidators.ValidateAfterSignatureFailed(
jwtToken,
jwtToken.ValidFromNullable,
jwtToken.ValidToNullable,
jwtToken.Audiences,
validationParameters,
configuration);
}
}
if (keysAttempted is not null)
{
if (kidExists)
{
throw LogHelper.LogExceptionMessage(new SecurityTokenSignatureKeyNotFoundException(LogHelper.FormatInvariant(TokenLogMessages.IDX10503,
LogHelper.MarkAsNonPII(jwtToken.Kid),
LogHelper.MarkAsNonPII((object)keysAttempted ?? ""),
LogHelper.MarkAsNonPII(numKeysInTokenValidationParameters),
LogHelper.MarkAsNonPII(numKeysInConfiguration),
(object)exceptionStrings ?? "",
jwtToken)));
}
else
{
throw LogHelper.LogExceptionMessage(new SecurityTokenSignatureKeyNotFoundException(LogHelper.FormatInvariant(TokenLogMessages.IDX10517,
LogHelper.MarkAsNonPII((object)keysAttempted ?? ""),
LogHelper.MarkAsNonPII(numKeysInTokenValidationParameters),
LogHelper.MarkAsNonPII(numKeysInConfiguration),
(object)exceptionStrings ?? "",
jwtToken)));
}
}
throw LogHelper.LogExceptionMessage(new SecurityTokenSignatureKeyNotFoundException(TokenLogMessages.IDX10500));
}
internal static bool IsSignatureValid(byte[] signatureBytes, int signatureBytesLength, SignatureProvider signatureProvider, byte[] dataToVerify, int dataToVerifyLength)
{
if (signatureProvider is SymmetricSignatureProvider)
{
return signatureProvider.Verify(dataToVerify, 0, dataToVerifyLength, signatureBytes, 0, signatureBytesLength);
}
else
{
if (signatureBytes.Length == signatureBytesLength)
{
return signatureProvider.Verify(dataToVerify, 0, dataToVerifyLength, signatureBytes, 0, signatureBytesLength);
}
else
{
byte[] sigBytes = new byte[signatureBytesLength];
Array.Copy(signatureBytes, 0, sigBytes, 0, signatureBytesLength);
return signatureProvider.Verify(dataToVerify, 0, dataToVerifyLength, sigBytes, 0, signatureBytesLength);
}
}
}
internal static bool ValidateSignature(byte[] bytes, int len, string stringWithSignature, int signatureStartIndex, SignatureProvider signatureProvider)
{
return Base64UrlEncoding.Decode<bool, SignatureProvider, byte[], int>(
stringWithSignature,
signatureStartIndex + 1,
stringWithSignature.Length - signatureStartIndex - 1,
signatureProvider,
bytes,
len,
IsSignatureValid);
}
internal static bool ValidateSignature(JsonWebToken jsonWebToken, SecurityKey key, TokenValidationParameters validationParameters)
{
var cryptoProviderFactory = validationParameters.CryptoProviderFactory ?? key.CryptoProviderFactory;
if (!cryptoProviderFactory.IsSupportedAlgorithm(jsonWebToken.Alg, key))
{
if (LogHelper.IsEnabled(EventLogLevel.Informational))
LogHelper.LogInformation(LogMessages.IDX14000, LogHelper.MarkAsNonPII(jsonWebToken.Alg), LogHelper.MarkAsNonPII(key.KeyId));
return false;
}
Validators.ValidateAlgorithm(jsonWebToken.Alg, key, jsonWebToken, validationParameters);
var signatureProvider = cryptoProviderFactory.CreateForVerifying(key, jsonWebToken.Alg);
try
{
if (signatureProvider == null)
throw LogHelper.LogExceptionMessage
(new InvalidOperationException(
LogHelper.FormatInvariant(
TokenLogMessages.IDX10636,
LogHelper.MarkAsNonPII(key == null ? "Null" : key.KeyId),
LogHelper.MarkAsNonPII(jsonWebToken.Alg))));
return EncodingUtils.PerformEncodingDependentOperation<bool, string, int, SignatureProvider>(
jsonWebToken.EncodedToken,
0,
jsonWebToken.Dot2,
Encoding.UTF8,
jsonWebToken.EncodedToken,
jsonWebToken.Dot2,
signatureProvider,
ValidateSignature);
}
finally
{
cryptoProviderFactory.ReleaseSignatureProvider(signatureProvider);
}
}
private static JsonWebToken ValidateSignatureUsingDelegates(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters)
{
if (validationParameters.SignatureValidatorUsingConfiguration != null)
{
// TODO - get configuration from validationParameters
BaseConfiguration configuration = null;
var validatedToken = validationParameters.SignatureValidatorUsingConfiguration(jsonWebToken.EncodedToken, validationParameters, configuration);
if (validatedToken == null)
throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidSignatureException(LogHelper.FormatInvariant(TokenLogMessages.IDX10505, jsonWebToken)));
if (!(validatedToken is JsonWebToken validatedJsonWebToken))
throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidSignatureException(LogHelper.FormatInvariant(TokenLogMessages.IDX10506, LogHelper.MarkAsNonPII(typeof(JsonWebToken)), LogHelper.MarkAsNonPII(validatedToken.GetType()), jsonWebToken)));
return validatedJsonWebToken;
}
else if (validationParameters.SignatureValidator != null)
{
var validatedToken = validationParameters.SignatureValidator(jsonWebToken.EncodedToken, validationParameters);
if (validatedToken == null)
throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidSignatureException(LogHelper.FormatInvariant(TokenLogMessages.IDX10505, jsonWebToken)));
if (!(validatedToken is JsonWebToken validatedJsonWebToken))
throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidSignatureException(LogHelper.FormatInvariant(TokenLogMessages.IDX10506, LogHelper.MarkAsNonPII(typeof(JsonWebToken)), LogHelper.MarkAsNonPII(validatedToken.GetType()), jsonWebToken)));
return validatedJsonWebToken;
}
throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidSignatureException(LogHelper.FormatInvariant(TokenLogMessages.IDX10505, jsonWebToken)));
}
/// <summary>
/// Validates a JWS or a JWE.
/// </summary>
/// <param name="token">A JSON Web Token (JWT) in JWS or JWE Compact Serialization format.</param>
/// <param name="validationParameters">The <see cref="TokenValidationParameters"/> to be used for validating the token.</param>
/// <returns>A <see cref="TokenValidationResult"/>.</returns>
[Obsolete("`JsonWebTokens.ValidateToken(string, TokenValidationParameters)` has been deprecated and will be removed in a future release. Use `JsonWebTokens.ValidateTokenAsync(string, TokenValidationParameters)` instead. For more information, see https://aka.ms/IdentityModel/7-breaking-changes", false)]
public virtual TokenValidationResult ValidateToken(string token, TokenValidationParameters validationParameters)
{
return ValidateTokenAsync(token, validationParameters).ConfigureAwait(false).GetAwaiter().GetResult();
}
/// <summary>
/// Validates a token.
/// On a validation failure, no exception will be thrown; instead, the exception will be set in the returned TokenValidationResult.Exception property.
/// Callers should always check the TokenValidationResult.IsValid property to verify the validity of the result.
/// </summary>
/// <param name="token">The token to be validated.</param>
/// <param name="validationParameters">The <see cref="TokenValidationParameters"/> to be used for validating the token.</param>
/// <returns>A <see cref="TokenValidationResult"/>.</returns>
/// <remarks>
/// <para>TokenValidationResult.Exception will be set to one of the following exceptions if the <paramref name="token"/> is invalid.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="token"/> is null or empty.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="validationParameters"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown if 'token.Length' is greater than <see cref="TokenHandler.MaximumTokenSizeInBytes"/>.</exception>
/// <exception cref="SecurityTokenMalformedException">Thrown if <paramref name="token"/> is not a valid <see cref="JsonWebToken"/>, <see cref="ReadToken(string, TokenValidationParameters)"/></exception>
/// <exception cref="SecurityTokenMalformedException">Thrown if the validationParameters.TokenReader delegate is not able to parse/read the token as a valid <see cref="JsonWebToken"/>, <see cref="ReadToken(string, TokenValidationParameters)"/></exception>
public override async Task<TokenValidationResult> ValidateTokenAsync(string token, TokenValidationParameters validationParameters)
{
if (string.IsNullOrEmpty(token))
return new TokenValidationResult { Exception = LogHelper.LogArgumentNullException(nameof(token)), IsValid = false };
if (validationParameters == null)
return new TokenValidationResult { Exception = LogHelper.LogArgumentNullException(nameof(validationParameters)), IsValid = false };
if (token.Length > MaximumTokenSizeInBytes)
return new TokenValidationResult { Exception = LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(TokenLogMessages.IDX10209, LogHelper.MarkAsNonPII(token.Length), LogHelper.MarkAsNonPII(MaximumTokenSizeInBytes)))), IsValid = false };
try
{
TokenValidationResult result = ReadToken(token, validationParameters);
if (result.IsValid)
return await ValidateTokenAsync(result.SecurityToken, validationParameters).ConfigureAwait(false);
return result;
}
catch (Exception ex)
{
return new TokenValidationResult
{
Exception = ex,
IsValid = false
};
}
}
/// <inheritdoc/>
public override async Task<TokenValidationResult> ValidateTokenAsync(SecurityToken token, TokenValidationParameters validationParameters)
{
if (token == null)
{
return new TokenValidationResult
{
Exception = LogHelper.LogArgumentNullException(nameof(token)),
IsValid = false
};
}
if (validationParameters == null)
return new TokenValidationResult
{
Exception = LogHelper.LogArgumentNullException(nameof(validationParameters)),
IsValid = false
};
var jwt = token as JsonWebToken;
if (jwt == null)
{
return new TokenValidationResult
{
Exception = LogHelper.LogExceptionMessage(
new ArgumentException(
$"{nameof(token)} must be a {nameof(JsonWebToken)}.",
nameof(token)
),
TokenValidationParametersExtensions.GetCallContext(validationParameters)
),
IsValid = false
};
}
try
{
return await ValidateTokenAsync(jwt, validationParameters).ConfigureAwait(false);
}
catch (Exception ex)
{
return new TokenValidationResult
{
Exception = ex,
IsValid = false
};
}
}
/// <summary>
/// Internal method for token validation, responsible for:
/// (1) Obtaining a configuration from the <see cref="TokenValidationParameters.ConfigurationManager"/>.
/// (2) Revalidating using the Last Known Good Configuration (if present), and obtaining a refreshed configuration (if necessary) and revalidating using it.
/// </summary>
/// <param name="jsonWebToken">The JWT token.</param>
/// <param name="validationParameters">The <see cref="TokenValidationParameters"/> to be used for validating the token.</param>
/// <returns></returns>
internal async ValueTask<TokenValidationResult> ValidateTokenAsync(
JsonWebToken jsonWebToken,
TokenValidationParameters validationParameters)
{
BaseConfiguration currentConfiguration = null;
if (validationParameters.ConfigurationManager != null)
{
try
{
currentConfiguration = await validationParameters.ConfigurationManager.GetBaseConfigurationAsync(CancellationToken.None).ConfigureAwait(false);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
// The exception is not re-thrown as the TokenValidationParameters may have the issuer and signing key set
// directly on them, allowing the library to continue with token validation.
if (LogHelper.IsEnabled(EventLogLevel.Warning))
LogHelper.LogWarning(
LogHelper.FormatInvariant(
TokenLogMessages.IDX10261,
validationParameters.ConfigurationManager.MetadataAddress,
ex.ToString()));
}
}
TokenValidationResult tokenValidationResult = jsonWebToken.IsEncrypted ?
await ValidateJWEAsync(jsonWebToken, validationParameters, currentConfiguration).ConfigureAwait(false) :
await ValidateJWSAsync(jsonWebToken, validationParameters, currentConfiguration).ConfigureAwait(false);
if (validationParameters.ConfigurationManager != null)
{
if (tokenValidationResult.IsValid)
{
// Set current configuration as LKG if it exists.
if (currentConfiguration != null)
validationParameters.ConfigurationManager.LastKnownGoodConfiguration = currentConfiguration;
return tokenValidationResult;
}
else if (TokenUtilities.IsRecoverableException(tokenValidationResult.Exception, (currentConfiguration != null && currentConfiguration.TokenDecryptionKeys.Count > 0)))
{
// If we were still unable to validate, attempt to refresh the configuration and validate using it
// but ONLY if the currentConfiguration is not null. We want to avoid refreshing the configuration on
// retrieval error as this case should have already been hit before. This refresh handles the case
// where a new valid configuration was somehow published during validation time.
if (currentConfiguration != null)
{
_telemetryClient.IncrementConfigurationRefreshRequestCounter(
validationParameters.ConfigurationManager.MetadataAddress,
TelemetryConstants.Protocols.Lkg,
TelemetryConstants.Protocols.ConfigurationSourceUnknown);
validationParameters.ConfigurationManager.RequestRefresh();
validationParameters.RefreshBeforeValidation = true;
var lastConfig = currentConfiguration;
currentConfiguration = await validationParameters.ConfigurationManager.GetBaseConfigurationAsync(CancellationToken.None).ConfigureAwait(false);
// Only try to re-validate using the newly obtained config if it doesn't reference equal the previously used configuration.
if (lastConfig != currentConfiguration)
{
tokenValidationResult = jsonWebToken.IsEncrypted ?
await ValidateJWEAsync(jsonWebToken, validationParameters, currentConfiguration).ConfigureAwait(false) :
await ValidateJWSAsync(jsonWebToken, validationParameters, currentConfiguration).ConfigureAwait(false);
if (tokenValidationResult.IsValid)
{
validationParameters.ConfigurationManager.LastKnownGoodConfiguration = currentConfiguration;
return tokenValidationResult;
}
}
}
if (validationParameters.ConfigurationManager.UseLastKnownGoodConfiguration)
{
validationParameters.RefreshBeforeValidation = false;
validationParameters.ValidateWithLKG = true;
var recoverableException = tokenValidationResult.Exception;
foreach (BaseConfiguration lkgConfiguration in validationParameters.ConfigurationManager.GetValidLkgConfigurations())
{
if (!lkgConfiguration.Equals(currentConfiguration) && TokenUtilities.IsRecoverableConfiguration(jsonWebToken.Kid, currentConfiguration, lkgConfiguration, recoverableException))
{
tokenValidationResult = jsonWebToken.IsEncrypted ?
await ValidateJWEAsync(jsonWebToken, validationParameters, lkgConfiguration).ConfigureAwait(false) :
await ValidateJWSAsync(jsonWebToken, validationParameters, lkgConfiguration).ConfigureAwait(false);
if (tokenValidationResult.IsValid)
return tokenValidationResult;
}
}
}
}
}
return tokenValidationResult;
}
internal async ValueTask<TokenValidationResult> ValidateTokenPayloadAsync(
JsonWebToken jsonWebToken,
TokenValidationParameters validationParameters,
BaseConfiguration configuration)
{
var expires = jsonWebToken.HasPayloadClaim(JwtRegisteredClaimNames.Exp) ? (DateTime?)jsonWebToken.ValidTo : null;
var notBefore = jsonWebToken.HasPayloadClaim(JwtRegisteredClaimNames.Nbf) ? (DateTime?)jsonWebToken.ValidFrom : null;
Validators.ValidateLifetime(notBefore, expires, jsonWebToken, validationParameters);
Validators.ValidateAudience(jsonWebToken.Audiences, jsonWebToken, validationParameters);
string issuer = await Validators.ValidateIssuerAsync(jsonWebToken.Issuer, jsonWebToken, validationParameters, configuration).ConfigureAwait(false);
Validators.ValidateTokenReplay(expires, jsonWebToken.EncodedToken, validationParameters);
if (validationParameters.ValidateActor && !string.IsNullOrWhiteSpace(jsonWebToken.Actor))
{
// Infinite recursion should not occur here, as the JsonWebToken passed into this method is (1) constructed from a string
// AND (2) the signature is successfully validated on it. (1) implies that even if there are nested actor tokens,
// they must end at some point since they cannot reference one another. (2) means that the token has a valid signature
// and (since issuer validation occurs first) came from a trusted authority.
// NOTE: More than one nested actor token should not be considered a valid token, but if we somehow encounter one,
// this code will still work properly.
TokenValidationResult tokenValidationResult =
await ValidateTokenAsync(jsonWebToken.Actor, validationParameters.ActorValidationParameters ?? validationParameters).ConfigureAwait(false);
if (!tokenValidationResult.IsValid)
return tokenValidationResult;
}
string tokenType = Validators.ValidateTokenType(jsonWebToken.Typ, jsonWebToken, validationParameters);
return new TokenValidationResult(jsonWebToken, this, validationParameters.Clone(), issuer)
{
IsValid = true,
TokenType = tokenType
};
}
}
}