-
Notifications
You must be signed in to change notification settings - Fork 439
Implement and Test Audience and Lifetime validations in SamlSecurityTokenHandler with New Validation Model #2925
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
FuPingFranco
merged 9 commits into
dev
from
francofung/ImplementNewModelValidationForSAMLAudience
Oct 22, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3902976
Implement and test Audience and Lifetime validations in SamlSecurityT…
3526791
Removed unesserasary method declaration in InternalAPI.Unshipped.txt …
7fa43c9
Merge branch 'dev' into francofung/ImplementNewModelValidationForSAML…
FuPingFranco b310ab7
Addressing PR feedback. Cached condition validation filure stackFrame
ec1197f
Clean-up
87d9f25
Merge branch 'dev' into francofung/ImplementNewModelValidationForSAML…
FuPingFranco 2b249e9
Merge branch 'dev' into francofung/ImplementNewModelValidationForSAML…
iNinja 9271ba4
Merge branch 'dev' into francofung/ImplementNewModelValidationForSAML…
FuPingFranco dff7076
Merge branch 'dev' into francofung/ImplementNewModelValidationForSAML…
FuPingFranco 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
20 changes: 20 additions & 0 deletions
20
src/Microsoft.IdentityModel.Tokens.Saml/InternalAPI.Unshipped.txt
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 |
|---|---|---|
| @@ -1,9 +1,29 @@ | ||
| Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.StackFrames | ||
| Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidatedConditions | ||
| Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidatedConditions.ValidatedAudience.get -> string | ||
| Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidatedConditions.ValidatedAudience.set -> void | ||
| Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidatedConditions.ValidatedConditions() -> void | ||
| Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidatedConditions.ValidatedConditions(string ValidatedAudience, Microsoft.IdentityModel.Tokens.ValidatedLifetime? ValidatedLifetime) -> void | ||
| Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidatedConditions.ValidatedLifetime.get -> Microsoft.IdentityModel.Tokens.ValidatedLifetime? | ||
| Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidatedConditions.ValidatedLifetime.set -> void | ||
| Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidateTokenAsync(Microsoft.IdentityModel.Tokens.Saml.SamlSecurityToken samlToken, 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>> | ||
| Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.StackFrames | ||
| Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.ValidateTokenAsync(Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityToken samlToken, 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>> | ||
| Microsoft.IdentityModel.Tokens.Saml2.SamlSecurityTokenHandler.ValidateTokenAsync(SamlSecurityToken samlToken, 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>> | ||
| static Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.StackFrames.AssertionConditionsNull -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.StackFrames.AssertionConditionsValidationFailed -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.StackFrames.AssertionNull -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.StackFrames.AudienceValidationFailed -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.StackFrames.LifetimeValidationFailed -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.StackFrames.OneTimeUseValidationFailed -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.StackFrames.TokenNull -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.StackFrames.TokenValidationParametersNull -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.StackFrames.AssertionConditionsNull -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.StackFrames.AssertionConditionsValidationFailed -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.StackFrames.AssertionNull -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.StackFrames.AudienceValidationFailed -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.StackFrames.LifetimeValidationFailed -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.StackFrames.OneTimeUseValidationFailed -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.StackFrames.TokenNull -> System.Diagnostics.StackFrame | ||
| static Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.StackFrames.TokenValidationParametersNull -> System.Diagnostics.StackFrame | ||
| virtual Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidateConditions(Microsoft.IdentityModel.Tokens.Saml.SamlSecurityToken samlToken, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext) -> Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidatedConditions> |
121 changes: 121 additions & 0 deletions
121
...crosoft.IdentityModel.Tokens.Saml/Saml/SamlSecurityTokenHandler.ValidateToken.Internal.cs
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 |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| #nullable enable | ||
| namespace Microsoft.IdentityModel.Tokens.Saml | ||
| { | ||
| /// <summary> | ||
| /// A <see cref="SecurityTokenHandler"/> designed for creating and validating Saml Tokens. See: http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf | ||
| /// </summary> | ||
| public partial class SamlSecurityTokenHandler : SecurityTokenHandler | ||
| { | ||
| #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously | ||
| internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync( | ||
| #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously | ||
| SamlSecurityToken samlToken, | ||
| ValidationParameters validationParameters, | ||
| CallContext callContext, | ||
| #pragma warning disable CA1801 // Review unused parameters | ||
| CancellationToken cancellationToken) | ||
| #pragma warning restore CA1801 // Review unused parameters | ||
| { | ||
| if (samlToken is null) | ||
| { | ||
| StackFrames.TokenNull ??= new StackFrame(true); | ||
| return ValidationError.NullParameter( | ||
| nameof(samlToken), | ||
| StackFrames.TokenNull); | ||
| } | ||
|
|
||
| if (validationParameters is null) | ||
| { | ||
| StackFrames.TokenValidationParametersNull ??= new StackFrame(true); | ||
| return ValidationError.NullParameter( | ||
| nameof(validationParameters), | ||
| StackFrames.TokenValidationParametersNull); | ||
| } | ||
|
|
||
| var conditionsResult = ValidateConditions(samlToken, validationParameters, callContext); | ||
|
|
||
| if (!conditionsResult.IsValid) | ||
| { | ||
| StackFrames.AssertionConditionsValidationFailed ??= new StackFrame(true); | ||
| return conditionsResult.UnwrapError().AddStackFrame(StackFrames.AssertionConditionsValidationFailed); | ||
| } | ||
|
|
||
| return new ValidatedToken(samlToken, this, validationParameters); | ||
| } | ||
|
|
||
| // ValidatedConditions is basically a named tuple but using a record struct better expresses the intent. | ||
| internal record struct ValidatedConditions(string? ValidatedAudience, ValidatedLifetime? ValidatedLifetime); | ||
|
|
||
| internal virtual ValidationResult<ValidatedConditions> ValidateConditions( | ||
| SamlSecurityToken samlToken, | ||
| ValidationParameters validationParameters, | ||
| CallContext callContext) | ||
| { | ||
| if (samlToken.Assertion is null) | ||
| { | ||
| StackFrames.AssertionNull ??= new StackFrame(true); | ||
| return ValidationError.NullParameter( | ||
| nameof(samlToken.Assertion), | ||
| StackFrames.AssertionNull); | ||
| } | ||
|
|
||
| if (samlToken.Assertion.Conditions is null) | ||
| { | ||
| StackFrames.AssertionConditionsNull ??= new StackFrame(true); | ||
| return ValidationError.NullParameter( | ||
| nameof(samlToken.Assertion.Conditions), | ||
| StackFrames.AssertionConditionsNull); | ||
| } | ||
|
|
||
| var lifetimeValidationResult = validationParameters.LifetimeValidator( | ||
| samlToken.Assertion.Conditions.NotBefore, | ||
| samlToken.Assertion.Conditions.NotOnOrAfter, | ||
| samlToken, | ||
| validationParameters, | ||
| callContext); | ||
|
|
||
| if (!lifetimeValidationResult.IsValid) | ||
| { | ||
| StackFrames.LifetimeValidationFailed ??= new StackFrame(true); | ||
| return lifetimeValidationResult.UnwrapError().AddStackFrame(StackFrames.LifetimeValidationFailed); | ||
| } | ||
|
|
||
| string? validatedAudience = null; | ||
| foreach (var condition in samlToken.Assertion.Conditions.Conditions) | ||
| { | ||
|
|
||
| if (condition is SamlAudienceRestrictionCondition audienceRestriction) | ||
| { | ||
|
|
||
| // AudienceRestriction.Audiences is an ICollection<Uri> so we need make a conversion to List<string> before calling our audience validator | ||
| var audiencesAsList = audienceRestriction.Audiences.Select(static x => x.OriginalString).ToList(); | ||
|
|
||
| var audienceValidationResult = validationParameters.AudienceValidator( | ||
| audiencesAsList, | ||
| samlToken, | ||
| validationParameters, | ||
| callContext); | ||
|
|
||
| if (!audienceValidationResult.IsValid) | ||
| return audienceValidationResult.UnwrapError(); | ||
|
|
||
| validatedAudience = audienceValidationResult.UnwrapResult(); | ||
| } | ||
|
|
||
| if (validatedAudience != null) | ||
| break; | ||
| } | ||
|
|
||
| return new ValidatedConditions(validatedAudience, lifetimeValidationResult.UnwrapResult()); | ||
| } | ||
| } | ||
| } | ||
| #nullable restore | ||
28 changes: 28 additions & 0 deletions
28
...soft.IdentityModel.Tokens.Saml/Saml/SamlSecurityTokenHandler.ValidateToken.StackFrames.cs
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Diagnostics; | ||
|
|
||
| #nullable enable | ||
| namespace Microsoft.IdentityModel.Tokens.Saml | ||
| { | ||
| public partial class SamlSecurityTokenHandler : SecurityTokenHandler | ||
| { | ||
| // Cached stack frames to build exceptions from validation errors | ||
| internal static class StackFrames | ||
| { | ||
| // Stack frames from ValidateTokenAsync using SecurityToken | ||
| internal static StackFrame? TokenNull; | ||
| internal static StackFrame? TokenValidationParametersNull; | ||
|
|
||
| // Stack frames from ValidateConditions | ||
| internal static StackFrame? AudienceValidationFailed; | ||
| internal static StackFrame? AssertionNull; | ||
| internal static StackFrame? AssertionConditionsNull; | ||
| internal static StackFrame? AssertionConditionsValidationFailed; | ||
| internal static StackFrame? LifetimeValidationFailed; | ||
| internal static StackFrame? OneTimeUseValidationFailed; | ||
| } | ||
| } | ||
| } | ||
| #nullable restore |
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
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.
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.