Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public async Task<bool> JsonWebTokenHandler_ValidateTokenAsyncWithVP()
{
// Because ValidationResult is an internal type, we cannot return it in the benchmark.
// We return a boolean instead until the type is made public.
Result<ValidationResult> result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
ValidationResult<ValidatedToken> result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
return result.IsSuccess;
}

Expand All @@ -131,7 +131,7 @@ public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsyncW
[BenchmarkCategory("ValidateTokenAsync_FailTwiceBeforeSuccess"), Benchmark]
public async Task<bool> JsonWebTokenHandler_ValidateTokenAsyncWithVP_SucceedOnThirdAttempt()
{
Result<ValidationResult> result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
ValidationResult<ValidatedToken> result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);

Expand Down Expand Up @@ -165,7 +165,7 @@ public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsyncW
[BenchmarkCategory("ValidateTokenAsync_FailFourTimesBeforeSuccess"), Benchmark]
public async Task<bool> JsonWebTokenHandler_ValidateTokenAsyncWithVP_SucceedOnFifthAttempt()
{
Result<ValidationResult> result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
ValidationResult<ValidatedToken> result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
Expand All @@ -186,7 +186,7 @@ public async Task<List<Claim>> JsonWebTokenHandler_ValidateTokenAsyncWithTVP_Cre
[BenchmarkCategory("ValidateTokenAsyncClaimAccess"), Benchmark]
public async Task<List<Claim>> JsonWebTokenHandler_ValidateTokenAsyncWithVP_CreateClaims()
{
Result<ValidationResult> result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
ValidationResult<ValidatedToken> result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
var claimsIdentity = result.UnwrapResult().ClaimsIdentity;
var claims = claimsIdentity.Claims;
return claims.ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public partial class JsonWebTokenHandler : TokenHandler
/// <param name="configuration">The <see cref="BaseConfiguration"/> to be used for validating the token.</param>
/// <param name="callContext"></param>
/// <returns>The decoded / cleartext contents of the JWE.</returns>
internal Result<string> DecryptToken(
internal ValidationResult<string> DecryptToken(
JsonWebToken jwtToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
Expand All @@ -32,42 +32,42 @@ internal Result<string> DecryptToken(
if (jwtToken == null)
{
StackFrame tokenNullStackFrame = StackFrames.DecryptionTokenNull ??= new StackFrame(true);
return ExceptionDetail.NullParameter(
return ValidationError.NullParameter(
nameof(jwtToken),
tokenNullStackFrame);
}

if (validationParameters == null)
{
StackFrame validationParametersNullStackFrame = StackFrames.DecryptionValidationParametersNull ??= new StackFrame(true);
return ExceptionDetail.NullParameter(
return ValidationError.NullParameter(
nameof(validationParameters),
validationParametersNullStackFrame);
}

if (string.IsNullOrEmpty(jwtToken.Enc))
{
StackFrame headerMissingStackFrame = StackFrames.DecryptionHeaderMissing ??= new StackFrame(true);
return new ExceptionDetail(
return new ValidationError(
new MessageDetail(TokenLogMessages.IDX10612),
ValidationFailureType.TokenDecryptionFailed,
typeof(SecurityTokenException),
headerMissingStackFrame);
}

(IList<SecurityKey>? contentEncryptionKeys, ExceptionDetail? exceptionDetail) result =
(IList<SecurityKey>? contentEncryptionKeys, ValidationError? validationError) result =
GetContentEncryptionKeys(jwtToken, validationParameters, configuration, callContext);

if (result.exceptionDetail != null)
if (result.validationError != null)
{
StackFrame decryptionGetKeysStackFrame = StackFrames.DecryptionGetEncryptionKeys ??= new StackFrame(true);
return result.exceptionDetail.AddStackFrame(decryptionGetKeysStackFrame);
return result.validationError.AddStackFrame(decryptionGetKeysStackFrame);
}

if (result.contentEncryptionKeys == null)
{
StackFrame noKeysTriedStackFrame = StackFrames.DecryptionNoKeysTried ??= new StackFrame(true);
return new ExceptionDetail(
return new ValidationError(
new MessageDetail(
TokenLogMessages.IDX10609,
LogHelper.MarkAsSecurityArtifact(jwtToken, JwtTokenUtilities.SafeLogJwtToken)),
Expand All @@ -88,7 +88,7 @@ internal Result<string> DecryptToken(
callContext);
}

internal (IList<SecurityKey>?, ExceptionDetail?) GetContentEncryptionKeys(JsonWebToken jwtToken, ValidationParameters validationParameters, BaseConfiguration? configuration, CallContext? callContext)
internal (IList<SecurityKey>?, ValidationError?) GetContentEncryptionKeys(JsonWebToken jwtToken, ValidationParameters validationParameters, BaseConfiguration? configuration, CallContext? callContext)
{
IList<SecurityKey>? keys = null;

Expand Down Expand Up @@ -207,7 +207,7 @@ internal Result<string> DecryptToken(
else
{
StackFrame decryptionKeyUnwrapFailedStackFrame = StackFrames.DecryptionKeyUnwrapFailed ??= new StackFrame(true);
ExceptionDetail exceptionDetail = new(
ValidationError validationError = new(
new MessageDetail(
TokenLogMessages.IDX10618,
keysAttempted?.ToString() ?? "",
Expand All @@ -217,7 +217,7 @@ internal Result<string> DecryptToken(
typeof(SecurityTokenKeyWrapException),
decryptionKeyUnwrapFailedStackFrame);

return (null, exceptionDetail);
return (null, validationError);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace Microsoft.IdentityModel.JsonWebTokens
public partial class JsonWebTokenHandler : TokenHandler
{
/// <summary>
/// Converts a string into an instance of <see cref="JsonWebToken"/>, returned inside of a <see cref="Result{TResult}"/>.
/// Converts a string into an instance of <see cref="JsonWebToken"/>, returned inside of a <see cref="ValidationResult{TResult}"/>.
/// </summary>
/// <param name="token">A JSON Web Token (JWT) in JWS or JWE Compact Serialization format.</param>
/// <param name="callContext"></param>
/// <returns>A <see cref="Result{TResult}"/> with the <see cref="JsonWebToken"/> if valid, or an error.</returns>
/// <returns>A <see cref="ValidationResult{TResult}"/> with the <see cref="JsonWebToken"/> if valid, or an error.</returns>
/// <exception cref="ArgumentNullException">returned if <paramref name="token"/> is null or empty.</exception>
/// <exception cref="SecurityTokenMalformedException">returned if the validationParameters.TokenReader delegate is not able to parse/read the token as a valid <see cref="JsonWebToken"/>.</exception>
/// <exception cref="SecurityTokenMalformedException">returned if <paramref name="token"/> is not a valid JWT, <see cref="JsonWebToken"/>.</exception>
internal static Result<SecurityToken> ReadToken(
internal static ValidationResult<SecurityToken> ReadToken(
string token,
#pragma warning disable CA1801 // TODO: remove pragma disable once callContext is used for logging
CallContext? callContext)
Expand All @@ -29,7 +29,7 @@ internal static Result<SecurityToken> ReadToken(
if (String.IsNullOrEmpty(token))
{
StackFrame nullTokenStackFrame = StackFrames.ReadTokenNullOrEmpty ?? new StackFrame(true);
return ExceptionDetail.NullParameter(
return ValidationError.NullParameter(
nameof(token),
nullTokenStackFrame);
}
Expand All @@ -44,7 +44,7 @@ internal static Result<SecurityToken> ReadToken(
#pragma warning restore CA1031 // Do not catch general exception types
{
StackFrame malformedTokenStackFrame = StackFrames.ReadTokenMalformed ?? new StackFrame(true);
return new ExceptionDetail(
return new ValidationError(
new MessageDetail(LogMessages.IDX14107),
ValidationFailureType.TokenReadingFailed,
typeof(SecurityTokenMalformedException),
Expand Down
Loading