-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Token Type validation: Remove exceptions (#2688)
* Remove throws when validating Token Type. * Clean-up * Addressing Feedback --------- Co-authored-by: Franco Fung <francofung@microsoft.com>
- Loading branch information
1 parent
2062d42
commit 1dd1e98
Showing
7 changed files
with
483 additions
and
1 deletion.
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/TokenTypeValidationResult.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 TokenType 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 TokenTypeValidationResult : ValidationResult | ||
{ | ||
private Exception? _exception; | ||
private const string TokenSource = "Microsoft.IdentityModel.Tokens"; | ||
|
||
/// <summary> | ||
/// Creates an instance of <see cref="TokenTypeValidationResult"/>. | ||
/// </summary> | ||
/// <paramref name="type"/> is the type against which the token was validated. | ||
public TokenTypeValidationResult(string? type) | ||
: base(ValidationFailureType.ValidationSucceeded) | ||
{ | ||
Type = type; | ||
IsValid = true; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an instance of <see cref="TokenTypeValidationResult"/> | ||
/// </summary> | ||
/// <paramref name="type"/> is the type against which the token was 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 TokenTypeValidationResult(string? type, ValidationFailureType validationFailure, ExceptionDetail exceptionDetail) | ||
: base(validationFailure, exceptionDetail) | ||
{ | ||
Type = type; | ||
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 SecurityTokenInvalidTypeException securityTokenInvalidTypeException) | ||
{ | ||
securityTokenInvalidTypeException.InvalidType = Type; | ||
securityTokenInvalidTypeException.Source = TokenSource; | ||
} | ||
|
||
return _exception; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the security token type. | ||
/// </summary> | ||
public string? Type { 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
Oops, something went wrong.