Skip to content

Commit 48b9e88

Browse files
committed
JsonWebTokenHandler IssuerExtensibility
1 parent aa98635 commit 48b9e88

File tree

14 files changed

+680
-23
lines changed

14 files changed

+680
-23
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
static Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.EncryptToken(byte[] innerTokenUtf8Bytes, Microsoft.IdentityModel.Tokens.EncryptingCredentials encryptingCredentials, string compressionAlgorithm, System.Collections.Generic.IDictionary<string, object> additionalHeaderClaims, string tokenType, bool includeKeyIdInHeader) -> string
22
static Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.WriteJweHeader(Microsoft.IdentityModel.Tokens.EncryptingCredentials encryptingCredentials, string compressionAlgorithm, string tokenType, System.Collections.Generic.IDictionary<string, object> jweHeaderClaims, bool includeKeyIdInHeader) -> byte[]
3-
static Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.WriteJwsHeader(ref System.Text.Json.Utf8JsonWriter writer, Microsoft.IdentityModel.Tokens.SigningCredentials signingCredentials, Microsoft.IdentityModel.Tokens.EncryptingCredentials encryptingCredentials, System.Collections.Generic.IDictionary<string, object> jweHeaderClaims, System.Collections.Generic.IDictionary<string, object> jwsHeaderClaims, string tokenType, bool includeKeyIdInHeader) -> void
3+
static Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.WriteJwsHeader(ref System.Text.Json.Utf8JsonWriter writer, Microsoft.IdentityModel.Tokens.SigningCredentials signingCredentials, Microsoft.IdentityModel.Tokens.EncryptingCredentials encryptingCredentials, System.Collections.Generic.IDictionary<string, object> jweHeaderClaims, System.Collections.Generic.IDictionary<string, object> jwsHeaderClaims, string tokenType, bool includeKeyIdInHeader) -> void
4+
static Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.StackFrames.IssuerValidatorThrew -> System.Diagnostics.StackFrame

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ await ValidateJWEAsync(jsonWebToken, validationParameters, currentConfiguration,
135135
if (result.IsValid)
136136
return result;
137137

138-
StackFrame tokenValidationStackFrame = StackFrames.TokenValidationFailedNullConfigurationManager ??= new StackFrame(true);
139-
return result.UnwrapError().AddStackFrame(tokenValidationStackFrame);
138+
return result.UnwrapError().AddStackFrame(ValidationError.GetCurrentStackFrame());
140139
}
141140

142141
if (result.IsValid)
@@ -277,14 +276,29 @@ private async ValueTask<ValidationResult<ValidatedToken>> ValidateJWSAsync(
277276
return audienceValidationResult.UnwrapError().AddStackFrame(audienceValidationFailureStackFrame);
278277
}
279278

280-
ValidationResult<ValidatedIssuer> issuerValidationResult = await validationParameters.IssuerValidatorAsync(
281-
jsonWebToken.Issuer, jsonWebToken, validationParameters, callContext, cancellationToken)
282-
.ConfigureAwait(false);
279+
ValidationResult<ValidatedIssuer> issuerValidationResult;
280+
try
281+
{
282+
issuerValidationResult = await validationParameters.IssuerValidatorAsync(
283+
jsonWebToken.Issuer, jsonWebToken, validationParameters, callContext, cancellationToken)
284+
.ConfigureAwait(false);
283285

284-
if (!issuerValidationResult.IsValid)
286+
if (!issuerValidationResult.IsValid)
287+
{
288+
return issuerValidationResult.UnwrapError().AddCurrentStackFrame();
289+
}
290+
}
291+
#pragma warning disable CA1031 // Do not catch general exception types
292+
catch (Exception ex)
293+
#pragma warning restore CA1031 // Do not catch general exception types
285294
{
286-
StackFrame issuerValidationFailureStackFrame = StackFrames.IssuerValidationFailed ??= new StackFrame(true);
287-
return issuerValidationResult.UnwrapError().AddStackFrame(issuerValidationFailureStackFrame);
295+
return new IssuerValidationError(
296+
new MessageDetail(TokenLogMessages.IDX10269),
297+
ValidationFailureType.IssuerValidatorThrew,
298+
typeof(SecurityTokenInvalidIssuerException),
299+
ValidationError.GetCurrentStackFrame(),
300+
jsonWebToken.Issuer,
301+
ex);
288302
}
289303

290304
ValidationResult<DateTime?> replayValidationResult = validationParameters.TokenReplayValidator(

src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.ValidateToken.StackFrames.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ internal static class StackFrames
2222
internal static StackFrame? TokenNull;
2323
internal static StackFrame? TokenValidationParametersNull;
2424
internal static StackFrame? TokenNotJWT;
25-
internal static StackFrame? TokenValidationFailedNullConfigurationManager;
2625
internal static StackFrame? TokenValidationFailed;
2726
// ValidateJWEAsync
2827
internal static StackFrame? DecryptionFailed;

src/Microsoft.IdentityModel.Tokens/InternalAPI.Unshipped.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
const Microsoft.IdentityModel.Tokens.LogMessages.IDX10002 = "IDX10002: Unknown exception type returned. Type: '{0}'. Message: '{1}'." -> string
22
const Microsoft.IdentityModel.Tokens.LogMessages.IDX10268 = "IDX10268: Unable to validate audience, validationParameters.ValidAudiences.Count == 0." -> string
3+
const Microsoft.IdentityModel.Tokens.LogMessages.IDX10269 = "IDX10269: IssuerValidationDelegate threw an exception, see inner exception." -> string
34
Microsoft.IdentityModel.Tokens.AlgorithmValidationError
45
Microsoft.IdentityModel.Tokens.AlgorithmValidationError.AlgorithmValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, string invalidAlgorithm) -> void
56
Microsoft.IdentityModel.Tokens.AlgorithmValidationError.InvalidAlgorithm.get -> string
67
Microsoft.IdentityModel.Tokens.AlgorithmValidationError._invalidAlgorithm -> string
78
Microsoft.IdentityModel.Tokens.AudienceValidationError.AudienceValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, Microsoft.IdentityModel.Tokens.ValidationFailureType failureType, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, System.Collections.Generic.IList<string> tokenAudiences, System.Collections.Generic.IList<string> validAudiences) -> void
89
Microsoft.IdentityModel.Tokens.AudienceValidationError.TokenAudiences.get -> System.Collections.Generic.IList<string>
10+
Microsoft.IdentityModel.Tokens.IssuerValidationError.InvalidIssuer.get -> string
11+
Microsoft.IdentityModel.Tokens.IssuerValidationError.IssuerValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, Microsoft.IdentityModel.Tokens.ValidationFailureType validationFailureType, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, string invalidIssuer, System.Exception innerException) -> void
12+
Microsoft.IdentityModel.Tokens.IssuerValidationError.IssuerValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, string invalidIssuer, System.Exception innerException) -> void
913
Microsoft.IdentityModel.Tokens.IssuerValidationSource.IssuerMatchedConfiguration = 1 -> Microsoft.IdentityModel.Tokens.IssuerValidationSource
1014
Microsoft.IdentityModel.Tokens.IssuerValidationSource.IssuerMatchedValidationParameters = 2 -> Microsoft.IdentityModel.Tokens.IssuerValidationSource
1115
Microsoft.IdentityModel.Tokens.LifetimeValidationError._expires -> System.DateTime
@@ -31,6 +35,7 @@ static Microsoft.IdentityModel.Tokens.AudienceValidationError.ValidationParamete
3135
static Microsoft.IdentityModel.Tokens.AudienceValidationError.ValidationParametersNull -> System.Diagnostics.StackFrame
3236
static Microsoft.IdentityModel.Tokens.Utility.SerializeAsSingleCommaDelimitedString(System.Collections.Generic.IList<string> strings) -> string
3337
static Microsoft.IdentityModel.Tokens.ValidationError.GetCurrentStackFrame(string filePath = "", int lineNumber = 0, int skipFrames = 1) -> System.Diagnostics.StackFrame
38+
static readonly Microsoft.IdentityModel.Tokens.ValidationFailureType.IssuerValidatorThrew -> Microsoft.IdentityModel.Tokens.ValidationFailureType
3439
static readonly Microsoft.IdentityModel.Tokens.ValidationFailureType.NoTokenAudiencesProvided -> Microsoft.IdentityModel.Tokens.ValidationFailureType
3540
static readonly Microsoft.IdentityModel.Tokens.ValidationFailureType.NoValidationParameterAudiencesProvided -> Microsoft.IdentityModel.Tokens.ValidationFailureType
3641
static readonly Microsoft.IdentityModel.Tokens.ValidationFailureType.SignatureAlgorithmValidationFailed -> Microsoft.IdentityModel.Tokens.ValidationFailureType

src/Microsoft.IdentityModel.Tokens/LogMessages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ internal static class LogMessages
8787
//public const string IDX10266 = "IDX10266: Unable to validate issuer. validationParameters.ValidIssuer is null or whitespace, validationParameters.ValidIssuers is null or empty and ConfigurationManager is null.";
8888
public const string IDX10267 = "IDX10267: '{0}' has been called by a derived class '{1}' which has not implemented this method. For this call graph to succeed, '{1}' will need to implement '{0}'.";
8989
public const string IDX10268 = "IDX10268: Unable to validate audience, validationParameters.ValidAudiences.Count == 0.";
90+
public const string IDX10269 = "IDX10269: IssuerValidationDelegate threw an exception, see inner exception.";
9091

9192

9293
// 10500 - SignatureValidation

src/Microsoft.IdentityModel.Tokens/Validation/Results/Details/IssuerValidationError.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,36 @@ namespace Microsoft.IdentityModel.Tokens
99
{
1010
internal class IssuerValidationError : ValidationError
1111
{
12-
private string? _invalidIssuer;
13-
1412
internal IssuerValidationError(
1513
MessageDetail messageDetail,
1614
Type exceptionType,
1715
StackFrame stackFrame,
1816
string? invalidIssuer)
19-
: base(messageDetail, ValidationFailureType.IssuerValidationFailed, exceptionType, stackFrame)
17+
: this(messageDetail, ValidationFailureType.IssuerValidationFailed, exceptionType, stackFrame, invalidIssuer, null)
2018
{
21-
_invalidIssuer = invalidIssuer;
2219
}
2320

21+
internal IssuerValidationError(
22+
MessageDetail messageDetail,
23+
ValidationFailureType validationFailureType,
24+
Type exceptionType,
25+
StackFrame stackFrame,
26+
string? invalidIssuer,
27+
Exception? innerException)
28+
: base(messageDetail, validationFailureType, exceptionType, stackFrame, innerException)
29+
{
30+
InvalidIssuer = invalidIssuer;
31+
}
32+
33+
internal string? InvalidIssuer { get; }
34+
2435
internal override Exception GetException()
2536
{
2637
if (ExceptionType == typeof(SecurityTokenInvalidIssuerException))
2738
{
2839
SecurityTokenInvalidIssuerException exception = new(MessageDetail.Message, InnerException)
2940
{
30-
InvalidIssuer = _invalidIssuer
41+
InvalidIssuer = InvalidIssuer
3142
};
3243

3344
return exception;

src/Microsoft.IdentityModel.Tokens/Validation/ValidationFailureType.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,11 @@ private class InvalidSecurityTokenFailure : ValidationFailureType { internal Inv
116116
/// </summary>
117117
public static readonly ValidationFailureType XmlValidationFailed = new XmlValidationFailure("XmlValidationFailed");
118118
private class XmlValidationFailure : ValidationFailureType { internal XmlValidationFailure(string name) : base(name) { } }
119+
120+
/// <summary>
121+
/// Defines a type that represents that a token is invalid.
122+
/// </summary>
123+
public static readonly ValidationFailureType IssuerValidatorThrew = new IssuerValidatorFailure("IssuerValidatorThrew");
124+
private class IssuerValidatorFailure : ValidationFailureType { internal IssuerValidatorFailure(string name) : base(name) { } }
119125
}
120126
}

src/Microsoft.IdentityModel.Tokens/Validation/ValidationParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public IssuerSigningKeyValidationDelegate IssuerSigningKeyValidator
311311
/// Allows overriding the delegate that will be used to validate the issuer of the token.
312312
/// </summary>
313313
/// <exception cref="ArgumentNullException">Thrown when the value is set as null.</exception>
314-
/// <returns>The <see cref="IssuerValidationDelegateAsync"/> used to validate the issuer of a token</returns>
314+
/// <returns>The <see cref="Tokens.IssuerValidationDelegateAsync"/> used to validate the issuer of a token</returns>
315315
public IssuerValidationDelegateAsync IssuerValidatorAsync
316316
{
317317
get { return _issuerValidatorAsync; }

src/Microsoft.IdentityModel.Tokens/Validation/Validators.Issuer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public static partial class Validators
5252
/// <returns>An <see cref="ValidationResult{TResult}"/> that contains either the issuer that was validated or an error.</returns>
5353
/// <remarks>An EXACT match is required.</remarks>
5454
internal static async Task<ValidationResult<ValidatedIssuer>> ValidateIssuerAsync(
55-
string issuer,
56-
SecurityToken securityToken,
55+
string? issuer,
56+
SecurityToken? securityToken,
5757
ValidationParameters validationParameters,
5858
#pragma warning disable CA1801 // Review unused parameters
5959
CallContext? callContext,
@@ -103,7 +103,7 @@ internal static async Task<ValidationResult<ValidatedIssuer>> ValidateIssuerAsyn
103103
// LogHelper.LogInformation(LogMessages.IDX10236, LogHelper.MarkAsNonPII(issuer), callContext);
104104

105105

106-
return new ValidatedIssuer(issuer, IssuerValidationSource.IssuerMatchedConfiguration);
106+
return new ValidatedIssuer(issuer!, IssuerValidationSource.IssuerMatchedConfiguration);
107107
}
108108
}
109109

@@ -126,7 +126,7 @@ internal static async Task<ValidationResult<ValidatedIssuer>> ValidateIssuerAsyn
126126
//if (LogHelper.IsEnabled(EventLogLevel.Informational))
127127
// LogHelper.LogInformation(LogMessages.IDX10236, LogHelper.MarkAsNonPII(issuer));
128128

129-
return new ValidatedIssuer(issuer, IssuerValidationSource.IssuerMatchedValidationParameters);
129+
return new ValidatedIssuer(issuer!, IssuerValidationSource.IssuerMatchedValidationParameters);
130130
}
131131
}
132132
}

0 commit comments

Comments
 (0)