-
Notifications
You must be signed in to change notification settings - Fork 435
Audience validation: remove exceptions #2655
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
12 commits
Select commit
Hold shift + click to select a range
950fe93
Added AudienceValidationResult type
iNinja 223cafc
Added new Validators.ValidateAudience method with CallContext and Res…
iNinja 896e13f
Disambiguate reference in documentation
iNinja 5ca7eea
Fixed issuer validation tests not ensuring no exception was thrown
iNinja 446bfbd
Removed initial value from Audience in AudienceValidationResult.
iNinja fe5cbaf
Fix typo
iNinja 64035fe
Made securityToken optional, removed null check and updated tests
iNinja 8bdc7f3
Updated audience matching method to remove allocations, return the ac…
iNinja adb8c39
Use pattern matching to cast exception
iNinja da8b0b8
Use List over IEnumerable to avoid allocations
iNinja e3ab461
Refactored to always iterate over List and avoid allocations. Origina…
iNinja dfc1eb6
Keep public interface receiving IEnumerable and internally use List
iNinja 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
67 changes: 67 additions & 0 deletions
67
src/Microsoft.IdentityModel.Tokens/Validation/AudienceValidationResult.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,67 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
|
||
namespace Microsoft.IdentityModel.Tokens | ||
{ | ||
/// <summary> | ||
/// Contains the result of validating the audiences from a <see cref="SecurityToken"/>. | ||
/// The <see cref="TokenValidationResult"/> contains a collection of <see cref="ValidationResult"/> for each step in the token validation. | ||
/// </summary> | ||
internal class AudienceValidationResult : ValidationResult | ||
{ | ||
private Exception _exception; | ||
|
||
/// <summary> | ||
/// Creates an instance of <see cref="AudienceValidationResult"/>. | ||
/// </summary> | ||
/// <paramref name="audience"/> is the audience that was validated successfully. | ||
public AudienceValidationResult(string audience) : base(ValidationFailureType.ValidationSucceeded) | ||
{ | ||
IsValid = true; | ||
Audience = audience; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an instance of <see cref="IssuerValidationResult"/> | ||
/// </summary> | ||
/// <paramref name="audience"/> is the audience that was intended to be validated. | ||
/// <paramref name="validationFailure"/> is the <see cref="ValidationFailureType"/> that occurred during validation. | ||
/// <paramref name="exceptionDetail"/> is the <see cref="ExceptionDetail"/> that occurred during validation. | ||
public AudienceValidationResult(string audience, ValidationFailureType validationFailure, ExceptionDetail exceptionDetail) | ||
: base(validationFailure, exceptionDetail) | ||
{ | ||
IsValid = false; | ||
Audience = audience; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the <see cref="Exception"/> that occurred during validation. | ||
/// </summary> | ||
public override Exception Exception | ||
{ | ||
get | ||
{ | ||
if (_exception != null || ExceptionDetail == null) | ||
return _exception; | ||
|
||
HasValidOrExceptionWasRead = true; | ||
_exception = ExceptionDetail.GetException(); | ||
if (_exception is SecurityTokenInvalidAudienceException securityTokenInvalidAudienceException) | ||
{ | ||
securityTokenInvalidAudienceException.InvalidAudience = Audience; | ||
securityTokenInvalidAudienceException.ExceptionDetail = ExceptionDetail; | ||
securityTokenInvalidAudienceException.Source = "Microsoft.IdentityModel.Tokens"; | ||
} | ||
|
||
return _exception; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the audience that was validated or intended to be validated. | ||
/// </summary> | ||
public string Audience { get; } | ||
} | ||
} |
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.