Skip to content

Commit

Permalink
Added custom validation error and validation delegates for token type…
Browse files Browse the repository at this point in the history
… validation
  • Loading branch information
iNinja committed Nov 20, 2024
1 parent 37a0a07 commit 058c87e
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// 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 CustomTokenTypeValidationDelegates
{
internal static ValidationResult<ValidatedTokenType> CustomTokenTypeValidatorDelegate(
string? type,
SecurityToken? securityToken,
ValidationParameters validationParameters,
CallContext callContext)
{
// Returns a CustomTokenTypeValidationError : TokenTypeValidationError
return new CustomTokenTypeValidationError(
new MessageDetail(nameof(CustomTokenTypeValidatorDelegate), null),
typeof(SecurityTokenInvalidTypeException),
ValidationError.GetCurrentStackFrame(),
type,
null);
}

internal static ValidationResult<ValidatedTokenType> CustomTokenTypeValidatorCustomExceptionDelegate(
string? type,
SecurityToken? securityToken,
ValidationParameters validationParameters,
CallContext callContext)
{
return new CustomTokenTypeValidationError(
new MessageDetail(nameof(CustomTokenTypeValidatorCustomExceptionDelegate), null),
typeof(CustomSecurityTokenInvalidTypeException),
ValidationError.GetCurrentStackFrame(),
type,
null);
}

internal static ValidationResult<ValidatedTokenType> CustomTokenTypeValidatorCustomExceptionCustomFailureTypeDelegate(
string? type,
SecurityToken? securityToken,
ValidationParameters validationParameters,
CallContext callContext)
{
return new CustomTokenTypeValidationError(
new MessageDetail(nameof(CustomTokenTypeValidatorCustomExceptionCustomFailureTypeDelegate), null),
typeof(CustomSecurityTokenInvalidTypeException),
ValidationError.GetCurrentStackFrame(),
type,
CustomTokenTypeValidationError.CustomTokenTypeValidationFailureType);
}

internal static ValidationResult<ValidatedTokenType> CustomTokenTypeValidatorUnknownExceptionDelegate(
string? type,
SecurityToken? securityToken,
ValidationParameters validationParameters,
CallContext callContext)
{
return new CustomTokenTypeValidationError(
new MessageDetail(nameof(CustomTokenTypeValidatorUnknownExceptionDelegate), null),
typeof(NotSupportedException),
ValidationError.GetCurrentStackFrame(),
type,
null);
}

internal static ValidationResult<ValidatedTokenType> CustomTokenTypeValidatorWithoutGetExceptionOverrideDelegate(
string? type,
SecurityToken? securityToken,
ValidationParameters validationParameters,
CallContext callContext)
{
return new CustomTokenTypeWithoutGetExceptionValidationOverrideError(
new MessageDetail(nameof(CustomTokenTypeValidatorWithoutGetExceptionOverrideDelegate), null),
typeof(CustomSecurityTokenInvalidTypeException),
ValidationError.GetCurrentStackFrame(),
type,
null);
}

internal static ValidationResult<ValidatedTokenType> TokenTypeValidatorDelegate(
string? type,
SecurityToken? securityToken,
ValidationParameters validationParameters,
CallContext callContext)
{
return new TokenTypeValidationError(
new MessageDetail(nameof(TokenTypeValidatorDelegate), null),
typeof(SecurityTokenInvalidTypeException),
ValidationError.GetCurrentStackFrame(),
type,
null);
}

internal static ValidationResult<ValidatedTokenType> TokenTypeValidatorThrows(
string? type,
SecurityToken? securityToken,
ValidationParameters validationParameters,
CallContext callContext)
{
throw new CustomSecurityTokenInvalidTypeException(nameof(TokenTypeValidatorThrows), null);
}

internal static ValidationResult<ValidatedTokenType> TokenTypeValidatorCustomTokenTypeExceptionTypeDelegate(
string? type,
SecurityToken? securityToken,
ValidationParameters validationParameters,
CallContext callContext)
{
return new TokenTypeValidationError(
new MessageDetail(nameof(TokenTypeValidatorCustomTokenTypeExceptionTypeDelegate), null),
typeof(CustomSecurityTokenInvalidTypeException),
ValidationError.GetCurrentStackFrame(),
type,
null);
}

internal static ValidationResult<ValidatedTokenType> TokenTypeValidatorCustomExceptionTypeDelegate(
string? type,
SecurityToken? securityToken,
ValidationParameters validationParameters,
CallContext callContext)
{
return new TokenTypeValidationError(
new MessageDetail(nameof(TokenTypeValidatorCustomExceptionTypeDelegate), null),
typeof(CustomSecurityTokenException),
ValidationError.GetCurrentStackFrame(),
type,
null);
}
}
}
#nullable restore
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,52 @@ public CustomLifetimeWithoutGetExceptionValidationOverrideError(
}
#endregion

#region TokenTypeValidationErrors
internal class CustomTokenTypeValidationError : TokenTypeValidationError
{
/// <summary>
/// A custom validation failure type.
/// </summary>
public static readonly ValidationFailureType CustomTokenTypeValidationFailureType = new TokenTypeValidationFailure("CustomTokenTypeValidationFailureType");
private class TokenTypeValidationFailure : ValidationFailureType { internal TokenTypeValidationFailure(string name) : base(name) { } }

public CustomTokenTypeValidationError(
MessageDetail messageDetail,
Type exceptionType,
StackFrame stackFrame,
string? invalidTokenType,
ValidationFailureType? validationFailureType = null,
Exception? innerException = null)
: base(messageDetail, exceptionType, stackFrame, invalidTokenType, validationFailureType, innerException)
{
}
internal override Exception GetException()
{
if (ExceptionType == typeof(CustomSecurityTokenInvalidTypeException))
{
var exception = new CustomSecurityTokenInvalidTypeException(MessageDetail.Message, InnerException) { InvalidType = InvalidTokenType };
exception.SetValidationError(this);
return exception;
}
return base.GetException();
}
}

internal class CustomTokenTypeWithoutGetExceptionValidationOverrideError : TokenTypeValidationError
{
public CustomTokenTypeWithoutGetExceptionValidationOverrideError(
MessageDetail messageDetail,
Type exceptionType,
StackFrame stackFrame,
string? invalidTokenType,
ValidationFailureType? validationFailureType = null,
Exception? innerException = null)
: base(messageDetail, exceptionType, stackFrame, invalidTokenType, validationFailureType, innerException)
{
}
}
#endregion // TokenTypeValidationErrors

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

0 comments on commit 058c87e

Please sign in to comment.