Skip to content

Commit f6b4b7c

Browse files
committed
Address PR comments
1 parent 694c3c3 commit f6b4b7c

11 files changed

Lines changed: 83 additions & 109 deletions

File tree

src/Microsoft.IdentityModel.JsonWebTokens/Experimental/JsonWebTokenHandler.ValidateSignature.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,11 @@ private static ValidationResult<SecurityKey, ValidationError> ValidateSignatureU
177177
}
178178
}
179179

180+
// This method tries a number of different keys, for each failure we add a stack frame to the stackFrames collection.
181+
// We want to add the current stack frame to the end of the list, to keep the order of the stack frames.
182+
// If for some reason stackFrames is null or empty, we add the current stack frame as the first and only entry.
180183
StackFrame currentStackFrame = ValidationError.GetCurrentStackFrame();
181-
StackFrame firstStackFrame = (stackFrames == null) ? currentStackFrame! : stackFrames[0];
184+
StackFrame firstStackFrame = (stackFrames == null || stackFrames.Count == 0) ? currentStackFrame : stackFrames[0];
182185
SignatureValidationError signatureValidationError;
183186

184187
if (keysTried)

src/Microsoft.IdentityModel.JsonWebTokens/Experimental/JsonWebTokenHandler.ValidateToken.Internal.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal override async Task<ValidationResult<ValidatedToken, ValidationError>>
2626
StackFrame? stackFrame;
2727
if (string.IsNullOrEmpty(token))
2828
{
29-
string key = ValidationError.GetStackFrameKey(memberName: TokenHandler.ValidateTokenAsyncStr);
29+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateTokenAsync));
3030
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
3131
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
3232

@@ -37,7 +37,7 @@ internal override async Task<ValidationResult<ValidatedToken, ValidationError>>
3737

3838
if (validationParameters is null)
3939
{
40-
string key = ValidationError.GetStackFrameKey(memberName: TokenHandler.ValidateTokenAsyncStr);
40+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateTokenAsync));
4141
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
4242
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
4343

@@ -48,7 +48,7 @@ internal override async Task<ValidationResult<ValidatedToken, ValidationError>>
4848

4949
if (token.Length > MaximumTokenSizeInBytes)
5050
{
51-
string key = ValidationError.GetStackFrameKey(memberName: TokenHandler.ValidateTokenAsyncStr);
51+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateTokenAsync));
5252
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
5353
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
5454

@@ -64,7 +64,7 @@ internal override async Task<ValidationResult<ValidatedToken, ValidationError>>
6464
ValidationResult<SecurityToken, ValidationError> readResult = ReadToken(token, callContext);
6565
if (!readResult.Succeeded)
6666
{
67-
string key = ValidationError.GetStackFrameKey(memberName: TokenHandler.ValidateTokenAsyncStr);
67+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateTokenAsync));
6868
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
6969
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
7070

@@ -80,7 +80,7 @@ internal override async Task<ValidationResult<ValidatedToken, ValidationError>>
8080

8181
if (!validationResult.Succeeded)
8282
{
83-
string key = ValidationError.GetStackFrameKey(memberName: TokenHandler.ValidateTokenAsyncStr);
83+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateTokenAsync));
8484
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
8585
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
8686

@@ -101,7 +101,7 @@ internal override async Task<ValidationResult<ValidatedToken, ValidationError>>
101101
string key = string.Empty;
102102
if (securityToken is null)
103103
{
104-
key = ValidationError.GetStackFrameKey(memberName: TokenHandler.ValidateTokenAsyncStr);
104+
key = ValidationError.GetStackFrameKey(memberName: nameof(TokenHandler.ValidateTokenAsync));
105105
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
106106
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
107107

@@ -112,7 +112,7 @@ internal override async Task<ValidationResult<ValidatedToken, ValidationError>>
112112

113113
if (validationParameters is null)
114114
{
115-
key = ValidationError.GetStackFrameKey(memberName: TokenHandler.ValidateTokenAsyncStr);
115+
key = ValidationError.GetStackFrameKey(memberName: nameof(TokenHandler.ValidateTokenAsync));
116116
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
117117
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
118118

@@ -123,7 +123,7 @@ internal override async Task<ValidationResult<ValidatedToken, ValidationError>>
123123

124124
if (securityToken is not JsonWebToken jsonWebToken)
125125
{
126-
key = ValidationError.GetStackFrameKey(memberName: TokenHandler.ValidateTokenAsyncStr);
126+
key = ValidationError.GetStackFrameKey(memberName: nameof(TokenHandler.ValidateTokenAsync));
127127
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
128128
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
129129

@@ -145,7 +145,7 @@ await ValidateJWEAsync(jsonWebToken, validationParameters, currentConfiguration,
145145
if (result.Succeeded)
146146
return result;
147147

148-
key = ValidationError.GetStackFrameKey(memberName: TokenHandler.ValidateTokenAsyncStr);
148+
key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateTokenAsync));
149149
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
150150
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
151151

@@ -214,7 +214,7 @@ await ValidateJWEAsync(jsonWebToken, validationParameters, lkgConfiguration, cal
214214
}
215215

216216
// If we reach this point, the token validation failed and we should return the error.
217-
key = ValidationError.GetStackFrameKey(memberName: TokenHandler.ValidateTokenAsyncStr);
217+
key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateTokenAsync));
218218
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
219219
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
220220

@@ -238,7 +238,7 @@ private async ValueTask<ValidationResult<ValidatedToken, ValidationError>> Valid
238238
StackFrame? stackFrame;
239239
if (!decryptionResult.Succeeded)
240240
{
241-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWEAsync");
241+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWEAsync));
242242
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
243243
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
244244

@@ -248,7 +248,7 @@ private async ValueTask<ValidationResult<ValidatedToken, ValidationError>> Valid
248248
ValidationResult<SecurityToken, ValidationError> readResult = ReadToken(decryptionResult.Result!, callContext);
249249
if (!readResult.Succeeded)
250250
{
251-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWEAsync");
251+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWEAsync));
252252
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
253253
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
254254

@@ -262,7 +262,7 @@ await ValidateJWSAsync(decryptedToken!, validationParameters, configuration, cal
262262

263263
if (!validationResult.Succeeded)
264264
{
265-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWSAsync");
265+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWSAsync));
266266
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
267267
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
268268

@@ -298,7 +298,7 @@ private async ValueTask<ValidationResult<ValidatedToken, ValidationError>> Valid
298298

299299
if (!lifetimeResult.Succeeded)
300300
{
301-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWSAsync");
301+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWSAsync));
302302
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
303303
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
304304

@@ -317,7 +317,7 @@ private async ValueTask<ValidationResult<ValidatedToken, ValidationError>> Valid
317317

318318
if (!audienceResult.Succeeded)
319319
{
320-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWSAsync");
320+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWSAsync));
321321
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
322322
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
323323

@@ -334,7 +334,7 @@ await Validators.ValidateIssuerInternalAsync(
334334

335335
if (!issuerResult.Succeeded)
336336
{
337-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWSAsync");
337+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWSAsync));
338338
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
339339
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
340340

@@ -350,7 +350,7 @@ await Validators.ValidateIssuerInternalAsync(
350350

351351
if (!tokenReplayResult.Succeeded)
352352
{
353-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWSAsync");
353+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWSAsync));
354354
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
355355
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
356356

@@ -366,7 +366,7 @@ await Validators.ValidateIssuerInternalAsync(
366366

367367
if (!tokenTypeResult.Succeeded)
368368
{
369-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWSAsync");
369+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWSAsync));
370370
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
371371
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
372372

@@ -382,7 +382,7 @@ await Validators.ValidateIssuerInternalAsync(
382382

383383
if (!algorithmResult.Succeeded)
384384
{
385-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWSAsync");
385+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWSAsync));
386386
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
387387
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
388388

@@ -399,7 +399,7 @@ await Validators.ValidateIssuerInternalAsync(
399399

400400
if (!signatureResult.Succeeded)
401401
{
402-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWSAsync");
402+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWSAsync));
403403
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
404404
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
405405

@@ -415,7 +415,7 @@ await Validators.ValidateIssuerInternalAsync(
415415

416416
if (!signatureKeyResult.Succeeded)
417417
{
418-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWSAsync");
418+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWSAsync));
419419
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
420420
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
421421

@@ -429,7 +429,7 @@ await Validators.ValidateIssuerInternalAsync(
429429
ValidationResult<SecurityToken, ValidationError> readResult = ReadToken(jsonWebToken.Actor, callContext);
430430
if (!readResult.Succeeded)
431431
{
432-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWSAsync");
432+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWSAsync));
433433
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
434434
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
435435

@@ -445,7 +445,7 @@ await Validators.ValidateIssuerInternalAsync(
445445

446446
if (!actorResult.Value.Succeeded)
447447
{
448-
string key = ValidationError.GetStackFrameKey(memberName: "ValidateJWSAsync");
448+
string key = ValidationError.GetStackFrameKey(memberName: nameof(ValidateJWSAsync));
449449
if (!ValidationError.TryGetStackFrame(key, out stackFrame))
450450
stackFrame = ValidationError.GetAsyncStackFrame(key, new StackFrame(0, true));
451451

0 commit comments

Comments
 (0)