From 25d0d07ee96ff714bda0bc745d4f84e7f03a4d87 Mon Sep 17 00:00:00 2001 From: Westin Musser Date: Wed, 12 Jun 2024 14:21:54 -0700 Subject: [PATCH 1/3] add base methods --- .../TokenHandler.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs b/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs index bba4f7c69b..26ceca7b30 100644 --- a/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs +++ b/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs @@ -5,6 +5,7 @@ using System; using System.ComponentModel; using System.Security.Claims; +using System.Text.Json; using System.Threading.Tasks; using static Microsoft.IdentityModel.Logging.LogHelper; @@ -53,8 +54,23 @@ public int TokenLifetimeInMinutes set => _defaultTokenLifetimeInMinutes = (value < 1) ? throw LogExceptionMessage(new ArgumentOutOfRangeException(nameof(value), FormatInvariant(LogMessages.IDX10104, LogHelper.MarkAsNonPII(value)))) : value; } + /// + /// Gets the of the token. + /// + public JsonTokenType TokenType { get; } + #region methods + /// + /// Validates a token. + /// On a validation failure, no exception will be thrown; instead, the exception will be set in the returned TokenValidationResult.Exception property. + /// Callers should always check the TokenValidationResult.IsValid property to verify the validity of the result. + /// + /// The token to be validated. + /// A required for validation. + /// A + public virtual TokenValidationResult ValidateToken(string token, TokenValidationParameters validationParameters) => throw new NotImplementedException(); + /// /// Validates a token. /// On a validation failure, no exception will be thrown; instead, the exception will be set in the returned TokenValidationResult.Exception property. @@ -75,6 +91,21 @@ public int TokenLifetimeInMinutes /// A public virtual Task ValidateTokenAsync(SecurityToken token, TokenValidationParameters validationParameters) => throw new NotImplementedException(); + /// + /// Evaluates whether a token can be converted into an instance of . + /// + /// The string to be evaluated. + /// A . + public virtual bool CanReadToken(string token) => throw new NotImplementedException(); + + /// + /// Creates a token as described by the provided . + /// + /// The used to create the token. + /// A . + /// + public virtual string CreateToken(SecurityTokenDescriptor tokenDescriptor) => throw new NotImplementedException(); + /// /// Converts a string into an instance of . /// From 57388150efc876b1b8c285dcb126e4a69ac99992 Mon Sep 17 00:00:00 2001 From: Westin Musser Date: Fri, 14 Jun 2024 12:27:05 -0700 Subject: [PATCH 2/3] update names of base methods --- .../JsonWebTokenHandler.cs | 2 +- .../SecurityTokenHandler.cs | 11 ------ .../TokenHandler.cs | 34 +++++++------------ .../json/JsonWebTokenHandler.cs | 2 +- 4 files changed, 15 insertions(+), 34 deletions(-) diff --git a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs index cf4ad2e264..771140bdb2 100644 --- a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs +++ b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs @@ -137,7 +137,7 @@ public IDictionary InboundClaimTypeMap /// 'false' if token.Length is greater than . /// 'true' if the token is in JSON compact serialization format. /// - public virtual bool CanReadToken(string token) + public override bool CanReadToken(string token) { if (string.IsNullOrWhiteSpace(token)) return false; diff --git a/src/Microsoft.IdentityModel.Tokens/SecurityTokenHandler.cs b/src/Microsoft.IdentityModel.Tokens/SecurityTokenHandler.cs index c7d48322a1..2bb78bc0ce 100644 --- a/src/Microsoft.IdentityModel.Tokens/SecurityTokenHandler.cs +++ b/src/Microsoft.IdentityModel.Tokens/SecurityTokenHandler.cs @@ -79,17 +79,6 @@ public virtual bool CanReadToken(XmlReader reader) return false; } - /// - /// Indicates whether the current token string can be read as a token - /// of the type handled by this instance. - /// - /// The token string thats needs to be read. - /// 'True' if the ReadToken method can parse the token string. - public virtual bool CanReadToken(string tokenString) - { - return false; - } - /// /// Gets security token. /// diff --git a/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs b/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs index 26ceca7b30..89bb653523 100644 --- a/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs +++ b/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs @@ -54,23 +54,8 @@ public int TokenLifetimeInMinutes set => _defaultTokenLifetimeInMinutes = (value < 1) ? throw LogExceptionMessage(new ArgumentOutOfRangeException(nameof(value), FormatInvariant(LogMessages.IDX10104, LogHelper.MarkAsNonPII(value)))) : value; } - /// - /// Gets the of the token. - /// - public JsonTokenType TokenType { get; } - #region methods - /// - /// Validates a token. - /// On a validation failure, no exception will be thrown; instead, the exception will be set in the returned TokenValidationResult.Exception property. - /// Callers should always check the TokenValidationResult.IsValid property to verify the validity of the result. - /// - /// The token to be validated. - /// A required for validation. - /// A - public virtual TokenValidationResult ValidateToken(string token, TokenValidationParameters validationParameters) => throw new NotImplementedException(); - /// /// Validates a token. /// On a validation failure, no exception will be thrown; instead, the exception will be set in the returned TokenValidationResult.Exception property. @@ -92,20 +77,27 @@ public int TokenLifetimeInMinutes public virtual Task ValidateTokenAsync(SecurityToken token, TokenValidationParameters validationParameters) => throw new NotImplementedException(); /// - /// Evaluates whether a token can be converted into an instance of . + /// Indicates whether the current token string can be read as a token + /// of the type handled by this instance. /// - /// The string to be evaluated. - /// A . - public virtual bool CanReadToken(string token) => throw new NotImplementedException(); + /// The token string thats needs to be read. + /// 'True' if the ReadToken method can parse the token string. + public virtual bool CanReadToken(string tokenString) => false; /// - /// Creates a token as described by the provided . + /// Creates a as a as described by the provided . /// /// The used to create the token. /// A . /// - public virtual string CreateToken(SecurityTokenDescriptor tokenDescriptor) => throw new NotImplementedException(); + public virtual string CreateSecuritTokenAsString(SecurityTokenDescriptor tokenDescriptor) => throw new NotImplementedException(); + /// + /// Creates a as described by the provided . + /// + /// + public virtual SecurityToken CreateSecurityToken(SecurityTokenDescriptor tokenDescriptor) => throw new NotImplementedException(); + /// /// Converts a string into an instance of . /// diff --git a/test/Microsoft.IdentityModel.JsonWebTokens.Tests/json/JsonWebTokenHandler.cs b/test/Microsoft.IdentityModel.JsonWebTokens.Tests/json/JsonWebTokenHandler.cs index 9f0a37702a..c1b41bd032 100644 --- a/test/Microsoft.IdentityModel.JsonWebTokens.Tests/json/JsonWebTokenHandler.cs +++ b/test/Microsoft.IdentityModel.JsonWebTokens.Tests/json/JsonWebTokenHandler.cs @@ -152,7 +152,7 @@ internal static IDictionary AddCtyClaimDefaultValue(IDictionary< /// 'false' if token.Length is greater than . /// 'true' if the token is in JSON compact serialization format. /// - public virtual bool CanReadToken(string token) + public override bool CanReadToken(string token) { if (string.IsNullOrWhiteSpace(token)) return false; From e96c148b2f1f2f96d2f649a88fea1e7ca456ae3e Mon Sep 17 00:00:00 2001 From: Westin Musser Date: Fri, 14 Jun 2024 12:53:18 -0700 Subject: [PATCH 3/3] typo --- src/Microsoft.IdentityModel.Tokens/TokenHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs b/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs index 89bb653523..11c62d48cd 100644 --- a/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs +++ b/src/Microsoft.IdentityModel.Tokens/TokenHandler.cs @@ -90,7 +90,7 @@ public int TokenLifetimeInMinutes /// The used to create the token. /// A . /// - public virtual string CreateSecuritTokenAsString(SecurityTokenDescriptor tokenDescriptor) => throw new NotImplementedException(); + public virtual string CreateSecurityTokenAsString(SecurityTokenDescriptor tokenDescriptor) => throw new NotImplementedException(); /// /// Creates a as described by the provided .