-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Algorithm validation: Remove exceptions (#2719)
* Remove throws when validating algorithm used for signing a token. * Adding small commits into one * Remove the use of delegate check and move to ValidationParameters * Replaced .Any() method call from check --------- Co-authored-by: Franco Fung <francofung@microsoft.com>
- Loading branch information
1 parent
2e7c701
commit 8c5e456
Showing
9 changed files
with
336 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
70 changes: 70 additions & 0 deletions
70
src/Microsoft.IdentityModel.Tokens/Validation/AlgorithmValidationResult.cs
This file contains 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,70 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
#nullable enable | ||
namespace Microsoft.IdentityModel.Tokens | ||
{ | ||
/// <summary> | ||
/// Contains the result of validating the Algorithm of 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 AlgorithmValidationResult : ValidationResult | ||
{ | ||
private Exception? _exception; | ||
private const string TokenSource = "Microsoft.IdentityModel.Tokens"; | ||
|
||
/// <summary> | ||
/// Creates an instance of <see cref="AlgorithmValidationResult"/>. | ||
/// </summary> | ||
/// <paramref name="algorithm"/>The algorithm to be validated. | ||
public AlgorithmValidationResult(string? algorithm) | ||
: base(ValidationFailureType.ValidationSucceeded) | ||
{ | ||
Algorithm = algorithm; | ||
IsValid = true; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an instance of <see cref=" AlgorithmValidationResult"/> | ||
/// </summary> | ||
/// <paramref name="algorithm"/>The algorithm 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 AlgorithmValidationResult(string? algorithm, ValidationFailureType validationFailure, ExceptionDetail exceptionDetail) | ||
: base(validationFailure, exceptionDetail) | ||
{ | ||
Algorithm = algorithm; | ||
IsValid = false; | ||
} | ||
|
||
/// <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 SecurityTokenInvalidAlgorithmException securityTokenInvalidAlgorithmException) | ||
{ | ||
securityTokenInvalidAlgorithmException.InvalidAlgorithm = Algorithm; | ||
securityTokenInvalidAlgorithmException.Source = TokenSource; | ||
} | ||
|
||
return _exception; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the algorithm used to sign the token. | ||
/// </summary> | ||
public string? Algorithm { get; } | ||
|
||
} | ||
} | ||
#nullable restore |
This file contains 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 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 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 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 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 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.