Skip to content

Commit 442baa3

Browse files
brentschmaltzHP712
andauthored
Added internal virtual on TokenHandler (#3084)
Add AadIssuer with ValidationParameters Added tests to AadIssuerValidator for ValidationParameters Co-authored-by: id4s <[email protected]>
1 parent 658604a commit 442baa3

25 files changed

+867
-99
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateTokenAsync(Microsoft.IdentityModel.Tokens.SecurityToken token, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
2-
Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateTokenAsync(string token, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
31
Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler._telemetryClient -> Microsoft.IdentityModel.Telemetry.ITelemetryClient
2+
override Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateTokenAsync(Microsoft.IdentityModel.Tokens.SecurityToken token, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
3+
override Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateTokenAsync(string token, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
44
static Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.CreateToken(string payload, Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor tokenDescriptor) -> string
55
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
66
static Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.EncryptToken(byte[] innerTokenUtf8Bytes, Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor tokenDescriptor) -> string

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public partial class JsonWebTokenHandler : TokenHandler
2424
/// <param name="callContext">A <see cref="CallContext"/> that contains call information.</param>
2525
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to request cancellation of the asynchronous operation.</param>
2626
/// <returns>A <see cref="ValidationResult{TResult}"/> with either a <see cref="ValidatedToken"/> if the token was validated or an <see cref="ValidationError"/> with the failure information and exception otherwise.</returns>
27-
internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
27+
internal override async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
2828
string token,
2929
ValidationParameters validationParameters,
3030
CallContext callContext,
@@ -76,7 +76,7 @@ internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
7676
}
7777

7878
/// <inheritdoc/>
79-
internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
79+
internal override async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
8080
SecurityToken token,
8181
ValidationParameters validationParameters,
8282
CallContext callContext,
@@ -391,7 +391,7 @@ await ValidateJWSAsync(actorToken, actorParameters, configuration, callContext,
391391
try
392392
{
393393
issuerSigningKeyValidationResult = validationParameters.IssuerSigningKeyValidator(
394-
jsonWebToken.SigningKey, jsonWebToken, validationParameters, configuration, callContext);
394+
jsonWebToken.SigningKey, jsonWebToken, validationParameters, callContext);
395395

396396
if (!issuerSigningKeyValidationResult.IsValid)
397397
return issuerSigningKeyValidationResult.UnwrapError().AddCurrentStackFrame();

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ Microsoft.IdentityModel.Tokens.Saml.SamlValidationError.SamlValidationError(Micr
1515
Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.CreateClaimsIdentity(Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityToken samlToken, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, string issuer) -> System.Security.Claims.ClaimsIdentity
1616
Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.ValidateTokenAsync(Microsoft.IdentityModel.Tokens.SecurityToken securityToken, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
1717
Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.ValidateTokenAsync(string token, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
18+
Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.ValidateTokenAsync(string token, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
1819
Microsoft.IdentityModel.Tokens.Saml2.Saml2ValidationError
1920
Microsoft.IdentityModel.Tokens.Saml2.Saml2ValidationError.Saml2ValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, Microsoft.IdentityModel.Tokens.ValidationFailureType validationFailureType, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, System.Exception innerException = null) -> void
20-
override Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.CreateClaimsIdentityInternal(Microsoft.IdentityModel.Tokens.SecurityToken securityToken, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, string issuer) -> System.Security.Claims.ClaimsIdentity
21-
override Microsoft.IdentityModel.Tokens.Saml.SamlValidationError.CreateException() -> System.Exception
22-
override Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.CreateClaimsIdentityInternal(Microsoft.IdentityModel.Tokens.SecurityToken securityToken, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, string issuer) -> System.Security.Claims.ClaimsIdentity
21+
override Microsoft.IdentityModel.Tokens.Saml.SamlValidationError.GetException() -> System.Exception
22+
override Microsoft.IdentityModel.Tokens.Saml.SamlValidationError.GetException() -> System.Exception
2323
override Microsoft.IdentityModel.Tokens.Saml2.Saml2ValidationError.CreateException() -> System.Exception
24+
static Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.StackFrames.SignatureValidationFailed -> System.Diagnostics.StackFrame
2425
static Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidateSignature(Microsoft.IdentityModel.Tokens.Saml.SamlSecurityToken samlToken, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext) -> Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.SecurityKey>
2526
static Microsoft.IdentityModel.Tokens.Saml.SamlTokenUtilities.PopulateValidationParametersWithCurrentConfigurationAsync(Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationParameters>
2627
static Microsoft.IdentityModel.Tokens.Saml.SamlTokenUtilities.ResolveTokenSigningKey(Microsoft.IdentityModel.Xml.KeyInfo tokenKeyInfo, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters) -> Microsoft.IdentityModel.Tokens.SecurityKey

src/Microsoft.IdentityModel.Tokens.Saml/Saml/SamlSecurityTokenHandler.ValidateToken.Internal.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public partial class SamlSecurityTokenHandler : SecurityTokenHandler
2424
/// <param name="callContext">A <see cref="CallContext"/> that contains call information.</param>
2525
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to request cancellation of the asynchronous operation.</param>
2626
/// <returns>A <see cref="ValidationResult{TResult}"/> with either a <see cref="ValidatedToken"/> if the token was validated or an <see cref="ValidationError"/> with the failure information and exception otherwise.</returns>
27-
internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
27+
internal async override Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
2828
string token,
2929
ValidationParameters validationParameters,
3030
CallContext callContext,
@@ -43,7 +43,7 @@ internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
4343
return await ValidateTokenAsync(tokenReadingResult.UnwrapResult(), validationParameters, callContext, cancellationToken).ConfigureAwait(false);
4444
}
4545

46-
internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
46+
internal override async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
4747
SecurityToken securityToken,
4848
ValidationParameters validationParameters,
4949
CallContext callContext,
@@ -150,7 +150,6 @@ internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
150150
samlToken.SigningKey,
151151
samlToken,
152152
validationParameters,
153-
null,
154153
callContext);
155154

156155
if (!issuerSigningKeyValidationResult.IsValid)

src/Microsoft.IdentityModel.Tokens.Saml/Saml2/Saml2SecurityTokenHandler.ValidateToken.Internal.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public partial class Saml2SecurityTokenHandler : SecurityTokenHandler
2525
/// <param name="callContext">A <see cref="CallContext"/> that contains call information.</param>
2626
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to request cancellation of the asynchronous operation.</param>
2727
/// <returns>A <see cref="ValidationResult{TResult}"/> with either a <see cref="ValidatedToken"/> if the token was validated or an <see cref="ValidationError"/> with the failure information and exception otherwise.</returns>
28-
internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
28+
internal override async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
2929
string token,
3030
ValidationParameters validationParameters,
3131
CallContext callContext,
@@ -44,7 +44,7 @@ internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
4444
return await ValidateTokenAsync(tokenReadingResult.UnwrapResult(), validationParameters, callContext, cancellationToken).ConfigureAwait(false);
4545
}
4646

47-
internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
47+
internal override async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
4848
SecurityToken securityToken,
4949
ValidationParameters validationParameters,
5050
CallContext callContext,
@@ -155,7 +155,6 @@ internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
155155
samlToken.SigningKey,
156156
samlToken,
157157
validationParameters,
158-
null,
159158
callContext);
160159

161160
if (!issuerSigningKeyValidationResult.IsValid)

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

+2
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,5 @@ static readonly Microsoft.IdentityModel.Tokens.ValidationFailureType.TokenIsNotS
163163
static readonly Microsoft.IdentityModel.Tokens.ValidationFailureType.TokenReplayValidatorThrew -> Microsoft.IdentityModel.Tokens.ValidationFailureType
164164
static readonly Microsoft.IdentityModel.Tokens.ValidationFailureType.TokenTypeValidatorThrew -> Microsoft.IdentityModel.Tokens.ValidationFailureType
165165
static readonly Microsoft.IdentityModel.Tokens.ValidationFailureType.XmlValidationFailed -> Microsoft.IdentityModel.Tokens.ValidationFailureType
166+
virtual Microsoft.IdentityModel.Tokens.TokenHandler.ValidateTokenAsync(Microsoft.IdentityModel.Tokens.SecurityToken token, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
167+
virtual Microsoft.IdentityModel.Tokens.TokenHandler.ValidateTokenAsync(string token, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using static Microsoft.IdentityModel.Logging.LogHelper;
8+
9+
namespace Microsoft.IdentityModel.Tokens
10+
{
11+
/// <summary>
12+
/// Defines properties shared across all security token handlers.
13+
/// </summary>
14+
public abstract partial class TokenHandler
15+
{
16+
internal virtual Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
17+
string token,
18+
ValidationParameters validationParameters,
19+
CallContext callContext,
20+
CancellationToken cancellationToken)
21+
{
22+
throw LogExceptionMessage(
23+
new NotImplementedException(
24+
FormatInvariant(
25+
LogMessages.IDX10267,
26+
MarkAsNonPII("internal virtual Task<ValidationResult<ValidatedToken>> " +
27+
"ValidateTokenAsync(string token, ValidationParameters validationParameters, CallContext callContext, CancellationToken cancellationToken)"),
28+
MarkAsNonPII(GetType().FullName))));
29+
}
30+
31+
internal virtual Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
32+
SecurityToken token,
33+
ValidationParameters validationParameters,
34+
CallContext callContext,
35+
CancellationToken cancellationToken)
36+
{
37+
throw LogExceptionMessage(
38+
new NotImplementedException(
39+
FormatInvariant(
40+
LogMessages.IDX10267,
41+
MarkAsNonPII("internal virtual Task<ValidationResult<ValidatedToken>> " +
42+
"ValidateTokenAsync(SecurityToken token, ValidationParameters validationParameters, CallContext callContext, CancellationToken cancellationToken)"),
43+
MarkAsNonPII(GetType().FullName))));
44+
}
45+
}
46+
}

src/Microsoft.IdentityModel.Tokens/TokenHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.IdentityModel.Tokens
1212
/// <summary>
1313
/// Defines properties shared across all security token handlers.
1414
/// </summary>
15-
public abstract class TokenHandler
15+
public abstract partial class TokenHandler
1616
{
1717
private int _defaultTokenLifetimeInMinutes = DefaultTokenLifetimeInMinutes;
1818
private int _maximumTokenSizeInBytes = TokenValidationParameters.DefaultMaximumTokenSizeInBytes;

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal class ValidationParameters
2222
private string _roleClaimType = ClaimsIdentity.DefaultRoleClaimType;
2323
private Dictionary<string, object>? _instancePropertyBag;
2424
private IList<SecurityKey>? _issuerSigningKeys;
25-
private IDictionary<string, object>? _propertyBag;
25+
private Dictionary<string, object>? _propertyBag;
2626
private IList<SecurityKey>? _tokenDecryptionKeys;
2727
private IList<string>? _validIssuers;
2828
private IList<string>? _validTokenTypes;
@@ -83,7 +83,9 @@ protected ValidationParameters(ValidationParameters other)
8383
LogTokenId = other.LogTokenId;
8484
NameClaimType = other.NameClaimType;
8585
NameClaimTypeRetriever = other.NameClaimTypeRetriever;
86-
_propertyBag = other.PropertyBag;
86+
foreach (var keyValue in other.PropertyBag)
87+
PropertyBag[keyValue.Key] = keyValue.Value;
88+
8789
RefreshBeforeValidation = other.RefreshBeforeValidation;
8890
RoleClaimType = other.RoleClaimType;
8991
RoleClaimTypeRetriever = other.RoleClaimTypeRetriever;
@@ -290,7 +292,9 @@ public IssuerSigningKeyValidationDelegate IssuerSigningKeyValidator
290292
/// Calling <see cref="Clone"/> will result in a new instance of this IDictionary.
291293
/// </summary>
292294
public IDictionary<string, object> InstancePropertyBag =>
293-
_instancePropertyBag ??= new Dictionary<string, object>();
295+
_instancePropertyBag ??
296+
Interlocked.CompareExchange(ref _instancePropertyBag, [], null) ??
297+
_instancePropertyBag;
294298

295299
/// <summary>
296300
/// Gets a value indicating if <see cref="Clone"/> was called to obtain this instance.
@@ -389,7 +393,9 @@ public string NameClaimType
389393
/// Gets or sets the <see cref="IDictionary{TKey, TValue}"/> that contains a collection of custom key/value pairs.
390394
/// This allows addition of parameters that could be used in custom token validation scenarios.
391395
/// </summary>
392-
public IDictionary<string, object> PropertyBag => _propertyBag ??= new Dictionary<string, object>();
396+
public IDictionary<string, object> PropertyBag => _propertyBag ??
397+
Interlocked.CompareExchange(ref _propertyBag, [], null) ??
398+
_propertyBag;
393399

394400
/// <summary>
395401
/// A boolean to control whether configuration should be refreshed before validating a token.

0 commit comments

Comments
 (0)