-
Notifications
You must be signed in to change notification settings - Fork 438
Remove Delegate Checks in Multiple Validators and Prevents Null Setting of Delegates #2725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d0297da
Removed delegate checks for token type validation.
d12b60a
Fix un-referenced message check
d9a1c9d
Added internal delegate for token type validation
5e47250
Prevent delegates from being null.
b258ed1
Simplify Valid types getter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| using System.Collections.Generic; | ||
| using System.ComponentModel; | ||
| using System.Security.Claims; | ||
| using System.Threading; | ||
| using Microsoft.IdentityModel.Abstractions; | ||
| using Microsoft.IdentityModel.Logging; | ||
|
|
||
|
|
@@ -20,8 +21,13 @@ internal class ValidationParameters | |
| private string _nameClaimType = ClaimsIdentity.DefaultNameClaimType; | ||
| private string _roleClaimType = ClaimsIdentity.DefaultRoleClaimType; | ||
| private Dictionary<string, object> _instancePropertyBag; | ||
| private IList<string> _validTokenTypes = []; | ||
|
|
||
| private IssuerValidationDelegateAsync _issuerValidationDelegate = Validators.ValidateIssuerAsync; | ||
| private AudienceValidatorDelegate _audienceValidator = Validators.ValidateAudience; | ||
| private IssuerValidationDelegateAsync _issuerValidatorAsync = Validators.ValidateIssuerAsync; | ||
| private LifetimeValidatorDelegate _lifetimeValidator = Validators.ValidateLifetime; | ||
| private TypeValidatorDelegate _typeValidator = Validators.ValidateTokenType; | ||
| private TokenReplayValidatorDelegate _tokenReplayValidator = Validators.ValidateTokenReplay; | ||
|
|
||
| /// <summary> | ||
| /// This is the default value of <see cref="ClaimsIdentity.AuthenticationType"/> when creating a <see cref="ClaimsIdentity"/>. | ||
|
|
@@ -112,14 +118,25 @@ public ValidationParameters() | |
| public AlgorithmValidator AlgorithmValidator { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a delegate that will be used to validate the audience. | ||
| /// Allows overriding the delegate that will be used to validate the audience. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// If set, this delegate will be called to validate the 'audience', instead of default processing. | ||
| /// If set, this delegate will be responsible for validating the 'audience', instead of default processing. | ||
| /// This means that no default 'audience' validation will occur. | ||
| /// Even if <see cref="ValidateAudience"/> is false, this delegate will still be called. | ||
| /// </remarks> | ||
| public AudienceValidator AudienceValidator { get; set; } | ||
| /// <exception cref="ArgumentNullException">Thrown when the value is set as null.</exception> | ||
FuPingFranco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// <returns>The <see cref="AudienceValidatorDelegate"/> used to validate the issuer of a token</returns> | ||
| public AudienceValidatorDelegate AudienceValidator | ||
| { | ||
| get | ||
| { | ||
| return _audienceValidator; | ||
| } | ||
| set | ||
| { | ||
| _audienceValidator = value ?? throw new ArgumentNullException(nameof(value), "AudienceValidator cannot be set as null."); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider to use nameof in exception message |
||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the AuthenticationType when creating a <see cref="ClaimsIdentity"/>. | ||
|
|
@@ -278,17 +295,19 @@ public virtual ClaimsIdentity CreateClaimsIdentity(SecurityToken securityToken, | |
| public IList<SecurityKey> IssuerSigningKeys { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a delegate that will be used to validate the issuer of the token. | ||
| /// Allows overriding the delegate that will be used to validate the issuer of the token. | ||
| /// </summary> | ||
| /// <exception cref="ArgumentNullException">Thrown when the value is set as null.</exception> | ||
| /// <returns>The <see cref="IssuerValidationDelegateAsync"/> used to validate the issuer of a token</returns> | ||
| public IssuerValidationDelegateAsync IssuerValidatorAsync | ||
| { | ||
| get | ||
| { | ||
| return _issuerValidationDelegate; | ||
| return _issuerValidatorAsync; | ||
| } | ||
| set | ||
| { | ||
| _issuerValidationDelegate = value ?? throw LogHelper.LogArgumentNullException(nameof(value)); | ||
| _issuerValidatorAsync = value ?? throw new ArgumentNullException(nameof(value), "IssuerValidatorAsync cannot be set as null."); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -298,9 +317,21 @@ public IssuerValidationDelegateAsync IssuerValidatorAsync | |
| public TransformBeforeSignatureValidation TransformBeforeSignatureValidation { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a delegate that will be used to validate the lifetime of the token | ||
| /// Allows overriding the delegate that will be used to validate the lifetime of the token | ||
| /// </summary> | ||
| public LifetimeValidator LifetimeValidator { get; set; } | ||
| /// <exception cref="ArgumentNullException">Thrown when the value is set as null.</exception> | ||
| /// <returns>The <see cref="LifetimeValidatorDelegate"/> used to validate the lifetime of a token</returns> | ||
| public LifetimeValidatorDelegate LifetimeValidator | ||
| { | ||
| get | ||
| { | ||
| return _lifetimeValidator; | ||
| } | ||
| set | ||
| { | ||
| _lifetimeValidator = value ?? throw new ArgumentNullException(nameof(value), "LifetimeValidator cannot be set as null."); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a <see cref="bool"/> that will decide if the token identifier claim needs to be logged. | ||
|
|
@@ -430,26 +461,51 @@ public string RoleClaimType | |
| public ITokenReplayCache TokenReplayCache { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a delegate that will be used to validate the token replay of the token | ||
| /// Allows overriding the delegate that will be used to validate the token replay of the token. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// If set, this delegate will be called to validate the token replay of the token, instead of default processing. | ||
| /// If no delegate is set, the default implementation will be used. | ||
| /// This means no default token replay validation will occur. | ||
| /// Even if <see cref="ValidateTokenReplay"/> is false, this delegate will still be called. | ||
| /// </remarks> | ||
| public TokenReplayValidator TokenReplayValidator { get; set; } | ||
| /// <exception cref="ArgumentNullException">Thrown when the value is set as null.</exception> | ||
| /// <returns>The <see cref="TokenReplayValidatorDelegate"/> used to validate the token replay of the token.</returns> | ||
| public TokenReplayValidatorDelegate TokenReplayValidator | ||
| { | ||
| get | ||
| { | ||
| return _tokenReplayValidator; | ||
| } | ||
|
|
||
| set | ||
| { | ||
| _tokenReplayValidator = value ?? throw new ArgumentNullException(nameof(value), "TokenReplayValidator cannot be set as null."); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a delegate that will be used to validate the type of the token. | ||
| /// If the token type cannot be validated, an exception MUST be thrown by the delegate. | ||
| /// Allows overriding the delegate that will be used to validate the type of the token. | ||
| /// If the token type cannot be validated, a <see cref="TokenTypeValidationResult"/> MUST be returned by the delegate. | ||
| /// Note: the 'type' parameter may be null if it couldn't be extracted from its usual location. | ||
| /// Implementations that need to resolve it from a different location can use the 'token' parameter. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// If set, this delegate will be called to validate the 'type' of the token, instead of default processing. | ||
| /// This means that no default 'type' validation will occur. | ||
| /// If no delegate is set, the default implementation will be used. The default checks the type | ||
| /// against the <see cref="ValidTypes"/> property, if the type is present then, it will succeed. | ||
| /// </remarks> | ||
| public TypeValidator TypeValidator { get; set; } | ||
| /// <exception cref="ArgumentNullException">Thrown when the value is set as null.</exception> | ||
| /// <returns>The <see cref="TypeValidatorDelegate"/> used to validate the token type of a token</returns> | ||
| public TypeValidatorDelegate TypeValidator | ||
| { | ||
| get | ||
| { | ||
| return _typeValidator; | ||
| } | ||
|
|
||
| set | ||
| { | ||
| _typeValidator = value ?? throw new ArgumentNullException(nameof(value), "TypeValidator cannot be set as null."); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a boolean to control if the LKG configuration will be used for token validation. | ||
|
|
@@ -494,9 +550,21 @@ public string RoleClaimType | |
| /// Gets the <see cref="IList{String}"/> that contains valid types that will be used to check against the JWT header's 'typ' claim. | ||
| /// If this property is not set, the 'typ' header claim will not be validated and all types will be accepted. | ||
| /// In the case of a JWE, this property will ONLY apply to the inner token header. | ||
| /// The default is <c>null</c>. | ||
| /// The default is an empty collection. | ||
| /// </summary> | ||
| public IList<string> ValidTypes { get; } | ||
| /// <exception cref="ArgumentNullException">Thrown when the value is set as null.</exception> | ||
| /// <returns>The <see cref="IList{String}"/> that contains valid token types that will be used to check against the token's 'typ' claim.</returns> | ||
| public IList<string> ValidTypes | ||
| { | ||
| get | ||
| { | ||
| return _validTokenTypes; | ||
| } | ||
| set | ||
| { | ||
| _validTokenTypes = value ?? throw new ArgumentNullException(nameof(value)); | ||
| } | ||
| } | ||
|
|
||
| public bool ValidateActor { get; set; } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.