-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extensibility tests: Lifetime - JWT, SAML and SAML2 (#3028)
* Handle potential exception thrown by the lifetime validation delegate in JWT, SAML, and SAML2 * Added custom lifetime validation delegates for testing (cherry picked from commit 6f104dc) * Added lifetime extensibility tests for JWT, SAML, and SAML2 (cherry picked from commit e602638) * Updated validation failure type position in tests * Added missing validation failure type to custom lifetime validation delegates * Handle success case as unexpected in lifetime extensibility tests * Removed duplicate test code adopting the refactored approach after merging dev
- Loading branch information
Showing
12 changed files
with
595 additions
and
26 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
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
37 changes: 37 additions & 0 deletions
37
...Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenHandler.Extensibility.Lifetime.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,37 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.IdentityModel.TestUtils.TokenValidationExtensibility.Tests; | ||
using Xunit; | ||
|
||
#nullable enable | ||
namespace Microsoft.IdentityModel.JsonWebTokens.Extensibility.Tests | ||
{ | ||
public partial class JsonWebTokenHandlerValidateTokenAsyncTests | ||
{ | ||
[Theory, MemberData( | ||
nameof(GenerateLifetimeExtensibilityTestCases), | ||
parameters: ["JWT", 2], | ||
DisableDiscoveryEnumeration = true)] | ||
public async Task ValidateTokenAsync_LifetimeValidator_Extensibility( | ||
LifetimeExtensibilityTheoryData theoryData) | ||
{ | ||
await ExtensibilityTesting.ValidateTokenAsync_Extensibility( | ||
theoryData, | ||
this, | ||
nameof(ValidateTokenAsync_LifetimeValidator_Extensibility)); | ||
} | ||
|
||
public static TheoryData<LifetimeExtensibilityTheoryData> GenerateLifetimeExtensibilityTestCases( | ||
string tokenHandlerType, | ||
int extraStackFrames) | ||
{ | ||
return ExtensibilityTesting.GenerateLifetimeExtensibilityTestCases( | ||
tokenHandlerType, | ||
extraStackFrames, | ||
"JsonWebTokenHandler.ValidateToken.Internal.cs"); | ||
} | ||
} | ||
} | ||
#nullable restore |
159 changes: 159 additions & 0 deletions
159
...IdentityModel.TestUtils/TokenValidationExtensibility/CustomLifetimeValidationDelegates.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,159 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
#nullable enable | ||
namespace Microsoft.IdentityModel.TestUtils | ||
{ | ||
internal class CustomLifetimeValidationDelegates | ||
{ | ||
internal static ValidationResult<ValidatedLifetime> CustomLifetimeValidatorDelegate( | ||
DateTime? notBefore, | ||
DateTime? expires, | ||
SecurityToken? securityToken, | ||
ValidationParameters validationParameters, | ||
CallContext callContext) | ||
{ | ||
// Returns a CustomLifetimeValidationError : LifetimeValidationError | ||
return new CustomLifetimeValidationError( | ||
new MessageDetail(nameof(CustomLifetimeValidatorDelegate), null), | ||
ValidationFailureType.LifetimeValidationFailed, | ||
typeof(SecurityTokenInvalidLifetimeException), | ||
ValidationError.GetCurrentStackFrame(), | ||
notBefore, | ||
expires, | ||
null); | ||
} | ||
|
||
internal static ValidationResult<ValidatedLifetime> CustomLifetimeValidatorCustomExceptionDelegate( | ||
DateTime? notBefore, | ||
DateTime? expires, | ||
SecurityToken? securityToken, | ||
ValidationParameters validationParameters, | ||
CallContext callContext) | ||
{ | ||
return new CustomLifetimeValidationError( | ||
new MessageDetail(nameof(CustomLifetimeValidatorCustomExceptionDelegate), null), | ||
ValidationFailureType.LifetimeValidationFailed, | ||
typeof(CustomSecurityTokenInvalidLifetimeException), | ||
ValidationError.GetCurrentStackFrame(), | ||
notBefore, | ||
expires, | ||
null); | ||
} | ||
|
||
internal static ValidationResult<ValidatedLifetime> CustomLifetimeValidatorCustomExceptionCustomFailureTypeDelegate( | ||
DateTime? notBefore, | ||
DateTime? expires, | ||
SecurityToken? securityToken, | ||
ValidationParameters validationParameters, | ||
CallContext callContext) | ||
{ | ||
return new CustomLifetimeValidationError( | ||
new MessageDetail(nameof(CustomLifetimeValidatorCustomExceptionCustomFailureTypeDelegate), null), | ||
CustomLifetimeValidationError.CustomLifetimeValidationFailureType, | ||
typeof(CustomSecurityTokenInvalidLifetimeException), | ||
ValidationError.GetCurrentStackFrame(), | ||
notBefore, | ||
expires); | ||
} | ||
|
||
internal static ValidationResult<ValidatedLifetime> CustomLifetimeValidatorUnknownExceptionDelegate( | ||
DateTime? notBefore, | ||
DateTime? expires, | ||
SecurityToken? securityToken, | ||
ValidationParameters validationParameters, | ||
CallContext callContext) | ||
{ | ||
return new CustomLifetimeValidationError( | ||
new MessageDetail(nameof(CustomLifetimeValidatorUnknownExceptionDelegate), null), | ||
ValidationFailureType.LifetimeValidationFailed, | ||
typeof(NotSupportedException), | ||
ValidationError.GetCurrentStackFrame(), | ||
notBefore, | ||
expires, | ||
null); | ||
} | ||
|
||
internal static ValidationResult<ValidatedLifetime> CustomLifetimeValidatorWithoutGetExceptionOverrideDelegate( | ||
DateTime? notBefore, | ||
DateTime? expires, | ||
SecurityToken? securityToken, | ||
ValidationParameters validationParameters, | ||
CallContext callContext) | ||
{ | ||
return new CustomLifetimeWithoutGetExceptionValidationOverrideError( | ||
new MessageDetail(nameof(CustomLifetimeValidatorWithoutGetExceptionOverrideDelegate), null), | ||
ValidationFailureType.LifetimeValidationFailed, | ||
typeof(CustomSecurityTokenInvalidLifetimeException), | ||
ValidationError.GetCurrentStackFrame(), | ||
notBefore, | ||
expires, | ||
null); | ||
} | ||
|
||
internal static ValidationResult<ValidatedLifetime> LifetimeValidatorDelegate( | ||
DateTime? notBefore, | ||
DateTime? expires, | ||
SecurityToken? securityToken, | ||
ValidationParameters validationParameters, | ||
CallContext callContext) | ||
{ | ||
return new LifetimeValidationError( | ||
new MessageDetail(nameof(LifetimeValidatorDelegate), null), | ||
ValidationFailureType.LifetimeValidationFailed, | ||
typeof(SecurityTokenInvalidLifetimeException), | ||
ValidationError.GetCurrentStackFrame(), | ||
notBefore, | ||
expires, | ||
null); | ||
} | ||
|
||
internal static ValidationResult<ValidatedLifetime> LifetimeValidatorThrows( | ||
DateTime? notBefore, | ||
DateTime? expires, | ||
SecurityToken? securityToken, | ||
ValidationParameters validationParameters, | ||
CallContext callContext) | ||
{ | ||
throw new CustomSecurityTokenInvalidLifetimeException(nameof(LifetimeValidatorThrows), null); | ||
} | ||
|
||
internal static ValidationResult<ValidatedLifetime> LifetimeValidatorCustomLifetimeExceptionTypeDelegate( | ||
DateTime? notBefore, | ||
DateTime? expires, | ||
SecurityToken? securityToken, | ||
ValidationParameters validationParameters, | ||
CallContext callContext) | ||
{ | ||
return new LifetimeValidationError( | ||
new MessageDetail(nameof(LifetimeValidatorCustomLifetimeExceptionTypeDelegate), null), | ||
ValidationFailureType.LifetimeValidationFailed, | ||
typeof(CustomSecurityTokenInvalidLifetimeException), | ||
ValidationError.GetCurrentStackFrame(), | ||
notBefore, | ||
expires, | ||
null); | ||
} | ||
|
||
internal static ValidationResult<ValidatedLifetime> LifetimeValidatorCustomExceptionTypeDelegate( | ||
DateTime? notBefore, | ||
DateTime? expires, | ||
SecurityToken? securityToken, | ||
ValidationParameters validationParameters, | ||
CallContext callContext) | ||
{ | ||
return new LifetimeValidationError( | ||
new MessageDetail(nameof(LifetimeValidatorCustomExceptionTypeDelegate), null), | ||
ValidationFailureType.LifetimeValidationFailed, | ||
typeof(CustomSecurityTokenException), | ||
ValidationError.GetCurrentStackFrame(), | ||
notBefore, | ||
expires, | ||
null); | ||
} | ||
} | ||
} | ||
#nullable restore |
Oops, something went wrong.