Skip to content

Commit

Permalink
Added custom validation delegates and validation errors for issuer si…
Browse files Browse the repository at this point in the history
…gning key extensibility testing
  • Loading branch information
iNinja committed Nov 20, 2024
1 parent a4476e1 commit ad1e0bd
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// 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 CustomIssuerSigningKeyValidationDelegates
{
internal static ValidationResult<ValidatedSigningKeyLifetime> CustomIssuerSigningKeyValidatorDelegate(
SecurityKey signingKey,
SecurityToken securityToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
CallContext callContext)
{
// Returns a CustomIssuerSigningKeyValidationError : IssuerSigningKeyValidationError
return new CustomIssuerSigningKeyValidationError(
new MessageDetail(nameof(CustomIssuerSigningKeyValidatorDelegate), null),
typeof(SecurityTokenInvalidSigningKeyException),
ValidationError.GetCurrentStackFrame(),
signingKey,
null);
}

internal static ValidationResult<ValidatedSigningKeyLifetime> CustomIssuerSigningKeyValidatorCustomExceptionDelegate(
SecurityKey signingKey,
SecurityToken securityToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
CallContext callContext)
{
return new CustomIssuerSigningKeyValidationError(
new MessageDetail(nameof(CustomIssuerSigningKeyValidatorCustomExceptionDelegate), null),
typeof(CustomSecurityTokenInvalidSigningKeyException),
ValidationError.GetCurrentStackFrame(),
signingKey,
null);
}

internal static ValidationResult<ValidatedSigningKeyLifetime> CustomIssuerSigningKeyValidatorCustomExceptionCustomFailureTypeDelegate(
SecurityKey signingKey,
SecurityToken securityToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
CallContext callContext)
{
return new CustomIssuerSigningKeyValidationError(
new MessageDetail(nameof(CustomIssuerSigningKeyValidatorCustomExceptionCustomFailureTypeDelegate), null),
typeof(CustomSecurityTokenInvalidSigningKeyException),
ValidationError.GetCurrentStackFrame(),
signingKey,
CustomIssuerSigningKeyValidationError.CustomIssuerSigningKeyValidationFailureType);
}

internal static ValidationResult<ValidatedSigningKeyLifetime> CustomIssuerSigningKeyValidatorUnknownExceptionDelegate(
SecurityKey signingKey,
SecurityToken securityToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
CallContext callContext)
{
return new CustomIssuerSigningKeyValidationError(
new MessageDetail(nameof(CustomIssuerSigningKeyValidatorUnknownExceptionDelegate), null),
typeof(NotSupportedException),
ValidationError.GetCurrentStackFrame(),
signingKey,
null);
}

internal static ValidationResult<ValidatedSigningKeyLifetime> CustomIssuerSigningKeyValidatorWithoutGetExceptionOverrideDelegate(
SecurityKey signingKey,
SecurityToken securityToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
CallContext callContext)
{
return new CustomIssuerSigningKeyWithoutGetExceptionValidationOverrideError(
new MessageDetail(nameof(CustomIssuerSigningKeyValidatorWithoutGetExceptionOverrideDelegate), null),
typeof(CustomSecurityTokenInvalidSigningKeyException),
ValidationError.GetCurrentStackFrame(),
signingKey,
null);
}

internal static ValidationResult<ValidatedSigningKeyLifetime> IssuerSigningKeyValidatorDelegate(
SecurityKey signingKey,
SecurityToken securityToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
CallContext callContext)
{
return new IssuerSigningKeyValidationError(
new MessageDetail(nameof(IssuerSigningKeyValidatorDelegate), null),
typeof(SecurityTokenInvalidSigningKeyException),
ValidationError.GetCurrentStackFrame(),
signingKey,
null);
}

internal static ValidationResult<ValidatedSigningKeyLifetime> IssuerSigningKeyValidatorThrows(
SecurityKey signingKey,
SecurityToken securityToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
CallContext callContext)
{
throw new CustomSecurityTokenInvalidSigningKeyException(nameof(IssuerSigningKeyValidatorThrows), null);
}

internal static ValidationResult<ValidatedSigningKeyLifetime> IssuerSigningKeyValidatorCustomIssuerSigningKeyExceptionTypeDelegate(
SecurityKey signingKey,
SecurityToken securityToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
CallContext callContext)
{
return new IssuerSigningKeyValidationError(
new MessageDetail(nameof(IssuerSigningKeyValidatorCustomIssuerSigningKeyExceptionTypeDelegate), null),
typeof(CustomSecurityTokenInvalidSigningKeyException),
ValidationError.GetCurrentStackFrame(),
signingKey,
null);
}

internal static ValidationResult<ValidatedSigningKeyLifetime> IssuerSigningKeyValidatorCustomExceptionTypeDelegate(
SecurityKey signingKey,
SecurityToken securityToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
CallContext callContext)
{
return new IssuerSigningKeyValidationError(
new MessageDetail(nameof(IssuerSigningKeyValidatorCustomExceptionTypeDelegate), null),
typeof(CustomSecurityTokenException),
ValidationError.GetCurrentStackFrame(),
signingKey,
null);
}
}
}
#nullable restore
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,53 @@ public CustomLifetimeWithoutGetExceptionValidationOverrideError(
}
#endregion

#region IssuerSigningKeyValidationErrors
internal class CustomIssuerSigningKeyValidationError : IssuerSigningKeyValidationError
{
/// <summary>
/// A custom validation failure type.
/// </summary>
public static readonly ValidationFailureType CustomIssuerSigningKeyValidationFailureType = new IssuerSigningKeyValidationFailure("CustomIssuerSigningKeyValidationFailureType");
private class IssuerSigningKeyValidationFailure : ValidationFailureType { internal IssuerSigningKeyValidationFailure(string name) : base(name) { } }

public CustomIssuerSigningKeyValidationError(
MessageDetail messageDetail,
Type exceptionType,
StackFrame stackFrame,
SecurityKey? securityKey,
ValidationFailureType? validationFailureType = null,
Exception? innerException = null)
: base(messageDetail, exceptionType, stackFrame, securityKey, validationFailureType, innerException)
{
}

internal override Exception GetException()
{
if (ExceptionType == typeof(CustomSecurityTokenInvalidSigningKeyException))
{
var exception = new CustomSecurityTokenInvalidSigningKeyException(MessageDetail.Message, InnerException) { SigningKey = InvalidSigningKey };
exception.SetValidationError(this);
return exception;
}
return base.GetException();
}
}

internal class CustomIssuerSigningKeyWithoutGetExceptionValidationOverrideError : IssuerSigningKeyValidationError
{
public CustomIssuerSigningKeyWithoutGetExceptionValidationOverrideError(
MessageDetail messageDetail,
Type exceptionType,
StackFrame stackFrame,
SecurityKey? securityKey,
ValidationFailureType? failureType = null,
Exception? innerException = null)
: base(messageDetail, exceptionType, stackFrame, securityKey, failureType, innerException)
{
}
}
#endregion // IssuerSigningKeyValidationErrors

// Other custom validation errors to be added here for signature validation, issuer signing key, etc.
}
#nullable restore

0 comments on commit ad1e0bd

Please sign in to comment.