Skip to content

Commit 058c87e

Browse files
committed
Added custom validation error and validation delegates for token type validation
1 parent 37a0a07 commit 058c87e

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using Microsoft.IdentityModel.Tokens;
6+
7+
#nullable enable
8+
namespace Microsoft.IdentityModel.TestUtils
9+
{
10+
internal class CustomTokenTypeValidationDelegates
11+
{
12+
internal static ValidationResult<ValidatedTokenType> CustomTokenTypeValidatorDelegate(
13+
string? type,
14+
SecurityToken? securityToken,
15+
ValidationParameters validationParameters,
16+
CallContext callContext)
17+
{
18+
// Returns a CustomTokenTypeValidationError : TokenTypeValidationError
19+
return new CustomTokenTypeValidationError(
20+
new MessageDetail(nameof(CustomTokenTypeValidatorDelegate), null),
21+
typeof(SecurityTokenInvalidTypeException),
22+
ValidationError.GetCurrentStackFrame(),
23+
type,
24+
null);
25+
}
26+
27+
internal static ValidationResult<ValidatedTokenType> CustomTokenTypeValidatorCustomExceptionDelegate(
28+
string? type,
29+
SecurityToken? securityToken,
30+
ValidationParameters validationParameters,
31+
CallContext callContext)
32+
{
33+
return new CustomTokenTypeValidationError(
34+
new MessageDetail(nameof(CustomTokenTypeValidatorCustomExceptionDelegate), null),
35+
typeof(CustomSecurityTokenInvalidTypeException),
36+
ValidationError.GetCurrentStackFrame(),
37+
type,
38+
null);
39+
}
40+
41+
internal static ValidationResult<ValidatedTokenType> CustomTokenTypeValidatorCustomExceptionCustomFailureTypeDelegate(
42+
string? type,
43+
SecurityToken? securityToken,
44+
ValidationParameters validationParameters,
45+
CallContext callContext)
46+
{
47+
return new CustomTokenTypeValidationError(
48+
new MessageDetail(nameof(CustomTokenTypeValidatorCustomExceptionCustomFailureTypeDelegate), null),
49+
typeof(CustomSecurityTokenInvalidTypeException),
50+
ValidationError.GetCurrentStackFrame(),
51+
type,
52+
CustomTokenTypeValidationError.CustomTokenTypeValidationFailureType);
53+
}
54+
55+
internal static ValidationResult<ValidatedTokenType> CustomTokenTypeValidatorUnknownExceptionDelegate(
56+
string? type,
57+
SecurityToken? securityToken,
58+
ValidationParameters validationParameters,
59+
CallContext callContext)
60+
{
61+
return new CustomTokenTypeValidationError(
62+
new MessageDetail(nameof(CustomTokenTypeValidatorUnknownExceptionDelegate), null),
63+
typeof(NotSupportedException),
64+
ValidationError.GetCurrentStackFrame(),
65+
type,
66+
null);
67+
}
68+
69+
internal static ValidationResult<ValidatedTokenType> CustomTokenTypeValidatorWithoutGetExceptionOverrideDelegate(
70+
string? type,
71+
SecurityToken? securityToken,
72+
ValidationParameters validationParameters,
73+
CallContext callContext)
74+
{
75+
return new CustomTokenTypeWithoutGetExceptionValidationOverrideError(
76+
new MessageDetail(nameof(CustomTokenTypeValidatorWithoutGetExceptionOverrideDelegate), null),
77+
typeof(CustomSecurityTokenInvalidTypeException),
78+
ValidationError.GetCurrentStackFrame(),
79+
type,
80+
null);
81+
}
82+
83+
internal static ValidationResult<ValidatedTokenType> TokenTypeValidatorDelegate(
84+
string? type,
85+
SecurityToken? securityToken,
86+
ValidationParameters validationParameters,
87+
CallContext callContext)
88+
{
89+
return new TokenTypeValidationError(
90+
new MessageDetail(nameof(TokenTypeValidatorDelegate), null),
91+
typeof(SecurityTokenInvalidTypeException),
92+
ValidationError.GetCurrentStackFrame(),
93+
type,
94+
null);
95+
}
96+
97+
internal static ValidationResult<ValidatedTokenType> TokenTypeValidatorThrows(
98+
string? type,
99+
SecurityToken? securityToken,
100+
ValidationParameters validationParameters,
101+
CallContext callContext)
102+
{
103+
throw new CustomSecurityTokenInvalidTypeException(nameof(TokenTypeValidatorThrows), null);
104+
}
105+
106+
internal static ValidationResult<ValidatedTokenType> TokenTypeValidatorCustomTokenTypeExceptionTypeDelegate(
107+
string? type,
108+
SecurityToken? securityToken,
109+
ValidationParameters validationParameters,
110+
CallContext callContext)
111+
{
112+
return new TokenTypeValidationError(
113+
new MessageDetail(nameof(TokenTypeValidatorCustomTokenTypeExceptionTypeDelegate), null),
114+
typeof(CustomSecurityTokenInvalidTypeException),
115+
ValidationError.GetCurrentStackFrame(),
116+
type,
117+
null);
118+
}
119+
120+
internal static ValidationResult<ValidatedTokenType> TokenTypeValidatorCustomExceptionTypeDelegate(
121+
string? type,
122+
SecurityToken? securityToken,
123+
ValidationParameters validationParameters,
124+
CallContext callContext)
125+
{
126+
return new TokenTypeValidationError(
127+
new MessageDetail(nameof(TokenTypeValidatorCustomExceptionTypeDelegate), null),
128+
typeof(CustomSecurityTokenException),
129+
ValidationError.GetCurrentStackFrame(),
130+
type,
131+
null);
132+
}
133+
}
134+
}
135+
#nullable restore

test/Microsoft.IdentityModel.TestUtils/TokenValidationExtensibility/CustomValidationErrors.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,52 @@ public CustomLifetimeWithoutGetExceptionValidationOverrideError(
157157
}
158158
#endregion
159159

160+
#region TokenTypeValidationErrors
161+
internal class CustomTokenTypeValidationError : TokenTypeValidationError
162+
{
163+
/// <summary>
164+
/// A custom validation failure type.
165+
/// </summary>
166+
public static readonly ValidationFailureType CustomTokenTypeValidationFailureType = new TokenTypeValidationFailure("CustomTokenTypeValidationFailureType");
167+
private class TokenTypeValidationFailure : ValidationFailureType { internal TokenTypeValidationFailure(string name) : base(name) { } }
168+
169+
public CustomTokenTypeValidationError(
170+
MessageDetail messageDetail,
171+
Type exceptionType,
172+
StackFrame stackFrame,
173+
string? invalidTokenType,
174+
ValidationFailureType? validationFailureType = null,
175+
Exception? innerException = null)
176+
: base(messageDetail, exceptionType, stackFrame, invalidTokenType, validationFailureType, innerException)
177+
{
178+
}
179+
internal override Exception GetException()
180+
{
181+
if (ExceptionType == typeof(CustomSecurityTokenInvalidTypeException))
182+
{
183+
var exception = new CustomSecurityTokenInvalidTypeException(MessageDetail.Message, InnerException) { InvalidType = InvalidTokenType };
184+
exception.SetValidationError(this);
185+
return exception;
186+
}
187+
return base.GetException();
188+
}
189+
}
190+
191+
internal class CustomTokenTypeWithoutGetExceptionValidationOverrideError : TokenTypeValidationError
192+
{
193+
public CustomTokenTypeWithoutGetExceptionValidationOverrideError(
194+
MessageDetail messageDetail,
195+
Type exceptionType,
196+
StackFrame stackFrame,
197+
string? invalidTokenType,
198+
ValidationFailureType? validationFailureType = null,
199+
Exception? innerException = null)
200+
: base(messageDetail, exceptionType, stackFrame, invalidTokenType, validationFailureType, innerException)
201+
{
202+
}
203+
}
204+
#endregion // TokenTypeValidationErrors
205+
160206
// Other custom validation errors to be added here for signature validation, issuer signing key, etc.
161207
}
162208
#nullable restore

0 commit comments

Comments
 (0)