Skip to content

Commit

Permalink
Added properties to ValidationResult without throwing. Renamed IsSucc…
Browse files Browse the repository at this point in the history
…ess to IsValid. (#2923)
  • Loading branch information
iNinja authored Oct 18, 2024
1 parent d88aa8d commit b18623d
Show file tree
Hide file tree
Showing 23 changed files with 105 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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.
ValidationResult<ValidatedToken> result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
return result.IsSuccess;
return result.IsValid;
}

[BenchmarkCategory("ValidateTokenAsync_FailTwiceBeforeSuccess"), Benchmark(Baseline = true)]
Expand Down Expand Up @@ -135,7 +135,7 @@ public async Task<bool> JsonWebTokenHandler_ValidateTokenAsyncWithVP_SucceedOnTh
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);

return result.IsSuccess;
return result.IsValid;
}

[BenchmarkCategory("ValidateTokenAsync_FailFourTimesBeforeSuccess"), Benchmark(Baseline = true)]
Expand Down Expand Up @@ -171,7 +171,7 @@ public async Task<bool> JsonWebTokenHandler_ValidateTokenAsyncWithVP_SucceedOnFi
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, CancellationToken.None).ConfigureAwait(false);

return result.IsSuccess;
return result.IsValid;
}

[BenchmarkCategory("ValidateTokenAsyncClaimAccess"), Benchmark(Baseline = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private static (ValidationResult<SecurityKey>? validResult, bool KidMatched, Key
{
SecurityKey key = keysList[i];
ValidationResult<SecurityKey> result = ValidateSignatureWithKey(jwtToken, key, validationParameters, callContext);
if (result.IsSuccess)
if (result.IsValid)
{
jwtToken.SigningKey = key;
return (result, true, null);
Expand Down Expand Up @@ -240,7 +240,7 @@ private static ValidationResult<SecurityKey> ValidateSignatureWithKey(
validationParameters,
callContext);

if (!result.IsSuccess)
if (!result.IsValid)
return new ValidationError(
new MessageDetail(
TokenLogMessages.IDX10518,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
}

ValidationResult<SecurityToken> readResult = ReadToken(token, callContext);
if (readResult.IsSuccess)
if (readResult.IsValid)
{
ValidationResult<ValidatedToken> validationResult = await ValidateTokenAsync(
readResult.UnwrapResult(),
Expand All @@ -79,7 +79,7 @@ internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
cancellationToken)
.ConfigureAwait(false);

if (validationResult.IsSuccess)
if (validationResult.IsValid)
return validationResult; // No need to unwrap and re-wrap the result.

StackFrame validationFailureStackFrame = StackFrames.TokenStringValidationFailed ??= new StackFrame(true);
Expand Down Expand Up @@ -132,14 +132,14 @@ await ValidateJWEAsync(jsonWebToken, validationParameters, currentConfiguration,

if (validationParameters.ConfigurationManager is null)
{
if (result.IsSuccess)
if (result.IsValid)
return result;

StackFrame tokenValidationStackFrame = StackFrames.TokenValidationFailedNullConfigurationManager ??= new StackFrame(true);
return result.UnwrapError().AddStackFrame(tokenValidationStackFrame);
}

if (result.IsSuccess)
if (result.IsValid)
{
// Set current configuration as LKG if it exists.
if (currentConfiguration is not null)
Expand Down Expand Up @@ -168,7 +168,7 @@ await ValidateJWEAsync(jsonWebToken, validationParameters, currentConfiguration,
await ValidateJWEAsync(jsonWebToken, validationParameters, currentConfiguration, callContext, cancellationToken).ConfigureAwait(false) :
await ValidateJWSAsync(jsonWebToken, validationParameters, currentConfiguration, callContext, cancellationToken).ConfigureAwait(false);

if (result.IsSuccess)
if (result.IsValid)
{
validationParameters.ConfigurationManager.LastKnownGoodConfiguration = currentConfiguration;
return result;
Expand All @@ -193,7 +193,7 @@ await ValidateJWEAsync(jsonWebToken, validationParameters, currentConfiguration,
await ValidateJWEAsync(jsonWebToken, validationParameters, lkgConfiguration, callContext, cancellationToken).ConfigureAwait(false) :
await ValidateJWSAsync(jsonWebToken, validationParameters, lkgConfiguration, callContext, cancellationToken).ConfigureAwait(false);

if (result.IsSuccess)
if (result.IsValid)
return result;
}
}
Expand All @@ -214,14 +214,14 @@ private async ValueTask<ValidationResult<ValidatedToken>> ValidateJWEAsync(
{
ValidationResult<string> decryptionResult = DecryptToken(
jwtToken, validationParameters, configuration, callContext);
if (!decryptionResult.IsSuccess)
if (!decryptionResult.IsValid)
{
StackFrame decryptionFailureStackFrame = StackFrames.DecryptionFailed ??= new StackFrame(true);
return decryptionResult.UnwrapError().AddStackFrame(decryptionFailureStackFrame);
}

ValidationResult<SecurityToken> readResult = ReadToken(decryptionResult.UnwrapResult(), callContext);
if (!readResult.IsSuccess)
if (!readResult.IsValid)
{
StackFrame readFailureStackFrame = StackFrames.DecryptedReadFailed ??= new StackFrame(true);
return readResult.UnwrapError().AddStackFrame(readFailureStackFrame);
Expand All @@ -232,7 +232,7 @@ private async ValueTask<ValidationResult<ValidatedToken>> ValidateJWEAsync(
await ValidateJWSAsync(decryptedToken!, validationParameters, configuration, callContext, cancellationToken)
.ConfigureAwait(false);

if (!validationResult.IsSuccess)
if (!validationResult.IsValid)
{
StackFrame validationFailureStackFrame = StackFrames.JWEValidationFailed ??= new StackFrame(true);
return validationResult.UnwrapError().AddStackFrame(validationFailureStackFrame);
Expand All @@ -259,7 +259,7 @@ private async ValueTask<ValidationResult<ValidatedToken>> ValidateJWSAsync(
ValidationResult<ValidatedLifetime> lifetimeValidationResult = validationParameters.LifetimeValidator(
notBefore, expires, jsonWebToken, validationParameters, callContext);

if (!lifetimeValidationResult.IsSuccess)
if (!lifetimeValidationResult.IsValid)
{
StackFrame lifetimeValidationFailureStackFrame = StackFrames.LifetimeValidationFailed ??= new StackFrame(true);
return lifetimeValidationResult.UnwrapError().AddStackFrame(lifetimeValidationFailureStackFrame);
Expand All @@ -271,7 +271,7 @@ private async ValueTask<ValidationResult<ValidatedToken>> ValidateJWSAsync(
ValidationResult<string> audienceValidationResult = validationParameters.AudienceValidator(
tokenAudiences, jsonWebToken, validationParameters, callContext);

if (!audienceValidationResult.IsSuccess)
if (!audienceValidationResult.IsValid)
{
StackFrame audienceValidationFailureStackFrame = StackFrames.AudienceValidationFailed ??= new StackFrame(true);
return audienceValidationResult.UnwrapError().AddStackFrame(audienceValidationFailureStackFrame);
Expand All @@ -281,7 +281,7 @@ private async ValueTask<ValidationResult<ValidatedToken>> ValidateJWSAsync(
jsonWebToken.Issuer, jsonWebToken, validationParameters, callContext, cancellationToken)
.ConfigureAwait(false);

if (!issuerValidationResult.IsSuccess)
if (!issuerValidationResult.IsValid)
{
StackFrame issuerValidationFailureStackFrame = StackFrames.IssuerValidationFailed ??= new StackFrame(true);
return issuerValidationResult.UnwrapError().AddStackFrame(issuerValidationFailureStackFrame);
Expand All @@ -290,7 +290,7 @@ private async ValueTask<ValidationResult<ValidatedToken>> ValidateJWSAsync(
ValidationResult<DateTime?> replayValidationResult = validationParameters.TokenReplayValidator(
expires, jsonWebToken.EncodedToken, validationParameters, callContext);

if (!replayValidationResult.IsSuccess)
if (!replayValidationResult.IsValid)
{
StackFrame replayValidationFailureStackFrame = StackFrames.ReplayValidationFailed ??= new StackFrame(true);
return replayValidationResult.UnwrapError().AddStackFrame(replayValidationFailureStackFrame);
Expand All @@ -301,7 +301,7 @@ private async ValueTask<ValidationResult<ValidatedToken>> ValidateJWSAsync(
if (validationParameters.ValidateActor && !string.IsNullOrWhiteSpace(jsonWebToken.Actor))
{
ValidationResult<SecurityToken> actorReadingResult = ReadToken(jsonWebToken.Actor, callContext);
if (!actorReadingResult.IsSuccess)
if (!actorReadingResult.IsValid)
{
StackFrame actorReadingFailureStackFrame = StackFrames.ActorReadFailed ??= new StackFrame(true);
return actorReadingResult.UnwrapError().AddStackFrame(actorReadingFailureStackFrame);
Expand All @@ -313,7 +313,7 @@ private async ValueTask<ValidationResult<ValidatedToken>> ValidateJWSAsync(
await ValidateJWSAsync(actorToken, actorParameters, configuration, callContext, cancellationToken)
.ConfigureAwait(false);

if (!innerActorValidationResult.IsSuccess)
if (!innerActorValidationResult.IsValid)
{
StackFrame actorValidationFailureStackFrame = StackFrames.ActorValidationFailed ??= new StackFrame(true);
return innerActorValidationResult.UnwrapError().AddStackFrame(actorValidationFailureStackFrame);
Expand All @@ -324,7 +324,7 @@ await ValidateJWSAsync(actorToken, actorParameters, configuration, callContext,

ValidationResult<ValidatedTokenType> typeValidationResult = validationParameters.TypeValidator(
jsonWebToken.Typ, jsonWebToken, validationParameters, callContext);
if (!typeValidationResult.IsSuccess)
if (!typeValidationResult.IsValid)
{
StackFrame typeValidationFailureStackFrame = StackFrames.TypeValidationFailed ??= new StackFrame(true);
return typeValidationResult.UnwrapError().AddStackFrame(typeValidationFailureStackFrame);
Expand All @@ -333,7 +333,7 @@ await ValidateJWSAsync(actorToken, actorParameters, configuration, callContext,
// The signature validation delegate is yet to be migrated to ValidationParameters.
ValidationResult<SecurityKey> signatureValidationResult = ValidateSignature(
jsonWebToken, validationParameters, configuration, callContext);
if (!signatureValidationResult.IsSuccess)
if (!signatureValidationResult.IsValid)
{
StackFrame signatureValidationFailureStackFrame = StackFrames.SignatureValidationFailed ??= new StackFrame(true);
return signatureValidationResult.UnwrapError().AddStackFrame(signatureValidationFailureStackFrame);
Expand All @@ -342,7 +342,7 @@ await ValidateJWSAsync(actorToken, actorParameters, configuration, callContext,
ValidationResult<ValidatedSigningKeyLifetime> issuerSigningKeyValidationResult =
validationParameters.IssuerSigningKeyValidator(
signatureValidationResult.UnwrapResult(), jsonWebToken, validationParameters, configuration, callContext);
if (!issuerSigningKeyValidationResult.IsSuccess)
if (!issuerSigningKeyValidationResult.IsValid)
{
StackFrame issuerSigningKeyValidationFailureStackFrame = StackFrames.IssuerSigningKeyValidationFailed ??= new StackFrame(true);
return issuerSigningKeyValidationResult.UnwrapError().AddStackFrame(issuerSigningKeyValidationFailureStackFrame);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal static ValidationResult<string> DecryptJwtToken(
}

ValidationResult<string> result = validationParameters.AlgorithmValidator(zipAlgorithm, key, jsonWebToken, validationParameters, callContext);
if (!result.IsSuccess)
if (!result.IsValid)
{
(exceptionStrings ??= new StringBuilder()).AppendLine(result.UnwrapError().MessageDetail.Message);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(

var conditionsResult = ValidateConditions(samlToken, validationParameters, callContext);

if (!conditionsResult.IsSuccess)
if (!conditionsResult.IsValid)
{
return conditionsResult.UnwrapError().AddStackFrame(new StackFrame(true));
}
Expand Down Expand Up @@ -78,7 +78,7 @@ internal virtual ValidationResult<ValidatedConditions> ValidateConditions(Saml2S
validationParameters,
callContext);

if (!lifetimeValidationResult.IsSuccess)
if (!lifetimeValidationResult.IsValid)
{
StackFrames.LifetimeValidationFailed ??= new StackFrame(true);
return lifetimeValidationResult.UnwrapError().AddStackFrame(StackFrames.LifetimeValidationFailed);
Expand All @@ -94,7 +94,7 @@ internal virtual ValidationResult<ValidatedConditions> ValidateConditions(Saml2S
validationParameters,
callContext);

if (!oneTimeUseValidationResult.IsSuccess)
if (!oneTimeUseValidationResult.IsValid)
{
StackFrames.OneTimeUseValidationFailed ??= new StackFrame(true);
return oneTimeUseValidationResult.UnwrapError().AddStackFrame(StackFrames.OneTimeUseValidationFailed);
Expand Down Expand Up @@ -128,7 +128,7 @@ internal virtual ValidationResult<ValidatedConditions> ValidateConditions(Saml2S
samlToken,
validationParameters,
callContext);
if (!audienceValidationResult.IsSuccess)
if (!audienceValidationResult.IsValid)
return audienceValidationResult.UnwrapError();

// Audience is valid, save it for later.
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.IdentityModel.Tokens/InternalAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,6 @@ Microsoft.IdentityModel.Tokens.ValidationParameters.ValidIssuers.get -> System.C
Microsoft.IdentityModel.Tokens.ValidationParameters.ValidTypes.get -> System.Collections.Generic.IList<string>
Microsoft.IdentityModel.Tokens.ValidationResult<TResult>
Microsoft.IdentityModel.Tokens.ValidationResult<TResult>.Equals(Microsoft.IdentityModel.Tokens.ValidationResult<TResult> other) -> bool
Microsoft.IdentityModel.Tokens.ValidationResult<TResult>.IsSuccess.get -> bool
Microsoft.IdentityModel.Tokens.ValidationResult<TResult>.ToResult() -> Microsoft.IdentityModel.Tokens.ValidationResult<TResult>
Microsoft.IdentityModel.Tokens.ValidationResult<TResult>.UnwrapError() -> Microsoft.IdentityModel.Tokens.ValidationError
Microsoft.IdentityModel.Tokens.ValidationResult<TResult>.UnwrapResult() -> TResult
Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.IdentityModel.Tokens/InternalAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Microsoft.IdentityModel.Tokens.AudienceValidationError.TokenAudiences.get -> Sys
Microsoft.IdentityModel.Tokens.IssuerValidationSource.IssuerMatchedConfiguration = 1 -> Microsoft.IdentityModel.Tokens.IssuerValidationSource
Microsoft.IdentityModel.Tokens.IssuerValidationSource.IssuerMatchedValidationParameters = 2 -> Microsoft.IdentityModel.Tokens.IssuerValidationSource
Microsoft.IdentityModel.Tokens.ValidationError.GetException(System.Type exceptionType, System.Exception innerException) -> System.Exception
Microsoft.IdentityModel.Tokens.ValidationResult<TResult>.Error.get -> Microsoft.IdentityModel.Tokens.ValidationError
Microsoft.IdentityModel.Tokens.ValidationResult<TResult>.IsValid.get -> bool
Microsoft.IdentityModel.Tokens.ValidationResult<TResult>.Result.get -> TResult
static Microsoft.IdentityModel.Tokens.AudienceValidationError.AudiencesCountZero -> System.Diagnostics.StackFrame
static Microsoft.IdentityModel.Tokens.AudienceValidationError.AudiencesNull -> System.Diagnostics.StackFrame
static Microsoft.IdentityModel.Tokens.AudienceValidationError.ValidateAudienceFailed -> System.Diagnostics.StackFrame
Expand Down
Loading

0 comments on commit b18623d

Please sign in to comment.