Skip to content

Commit

Permalink
Replace overload with delegates to read token values.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaytak committed Nov 5, 2024
1 parent 96b6b36 commit 3613363
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class JsonClaimSet

internal JsonClaimSet()
{
_jsonClaims = new Dictionary<string, object>();
_jsonClaims = [];
}

internal JsonClaimSet(Dictionary<string, object> jsonClaims)
Expand Down
60 changes: 60 additions & 0 deletions src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ public JsonWebToken(string jwtEncodedString)
_encodedToken = jwtEncodedString;
}

/// <summary>
/// Initializes a new instance of <see cref="JsonWebToken"/> from a ReadOnlyMemory{char} in JWS or JWE Compact serialized format.
/// </summary>
/// <param name="encodedTokenMemory">A ReadOnlyMemory{char} containing the JSON Web Token serialized in JWS or JWE Compact format.</param>
/// <param name="readTokenHeaderValueDelegate">A custom delegate to be called when each header claim is being read. If null, default implementation is called.</param>
/// <param name="readTokenPayloadValueDelegate">A custom delegate to be called when each payload claim is being read. If null, default implementation is called.</param>
public JsonWebToken(
ReadOnlyMemory<char> encodedTokenMemory,
ReadTokenHeaderValueDelegate readTokenHeaderValueDelegate,
ReadTokenPayloadValueDelegate readTokenPayloadValueDelegate)
{
if (encodedTokenMemory.IsEmpty)
throw LogHelper.LogExceptionMessage(new ArgumentNullException(nameof(encodedTokenMemory)));

ReadTokenHeaderValueDelegate = readTokenHeaderValueDelegate ?? ReadTokenHeaderValue;
ReadTokenPayloadValueDelegate = readTokenPayloadValueDelegate ?? ReadTokenPayloadValue;

ReadToken(encodedTokenMemory);

_encodedTokenMemory = encodedTokenMemory;

}

/// <summary>
/// Initializes a new instance of <see cref="JsonWebToken"/> from a ReadOnlyMemory{char} in JWS or JWE Compact serialized format.
/// </summary>
Expand Down Expand Up @@ -141,6 +164,43 @@ public JsonWebToken(string header, string payload)
_encodedToken = encodedToken;
}

/// <summary>
/// Called for each claim when token header is being read.
/// </summary>
/// <remarks>
/// An example implementation:
/// <code>
/// object ReadPayloadValueDelegate(ref Utf8JsonReader reader, string claimName) =>
/// {
/// if (reader.ValueTextEquals("CustomProp"))
/// {
/// return JsonSerializerPrimitives.ReadString(ref reader, JwtRegisteredClaimNames.CustomProp, ClassName, true);
/// }
/// return JsonWebToken.ReadTokenHeaderValue(ref reader, claimName);
/// }
/// </code>
/// </remarks>
internal ReadTokenHeaderValueDelegate ReadTokenHeaderValueDelegate { get; set; } = ReadTokenHeaderValue;


/// <summary>
/// Called for each claim when token payload is being read.
/// </summary>
/// <remarks>
/// An example implementation:
/// <code>
/// object ReadPayloadValueDelegate(ref Utf8JsonReader reader, string claimName) =>
/// {
/// if (reader.ValueTextEquals("CustomProp"))
/// {
/// return JsonSerializerPrimitives.ReadString(ref reader, JwtRegisteredClaimNames.CustomProp, ClassName, true);
/// }
/// return JsonWebToken.ReadTokenPayloadValue(ref reader, claimName);
/// }
/// </code>
/// </remarks>
internal ReadTokenPayloadValueDelegate ReadTokenPayloadValueDelegate { get; set; } = ReadTokenPayloadValue;

internal string ActualIssuer { get; set; }

internal ClaimsIdentity ActorClaimsIdentity { get; set; }
Expand Down
19 changes: 19 additions & 0 deletions src/Microsoft.IdentityModel.Tokens/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;

namespace Microsoft.IdentityModel.Tokens
Expand Down Expand Up @@ -206,4 +207,22 @@ namespace Microsoft.IdentityModel.Tokens
/// <returns>The validated <see cref="SecurityToken"/>.</returns>
internal delegate ValidationResult<SecurityKey> SignatureValidationDelegate(SecurityToken token, ValidationParameters validationParameters, BaseConfiguration? configuration, CallContext? callContext);
#nullable restore

/// <summary>
/// Definition for ReadTokenHeaderValueDelegate.
/// Called for each claim when token header is being read.
/// </summary>
/// <param name="reader">Reader for the underlying token bytes.</param>
/// <param name="claimName">The name of the claim being read.</param>
/// <returns></returns>
public delegate object ReadTokenHeaderValueDelegate(ref Utf8JsonReader reader, string claimName);

Check failure on line 218 in src/Microsoft.IdentityModel.Tokens/Delegates.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Symbol 'ReadTokenHeaderValueDelegate' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 218 in src/Microsoft.IdentityModel.Tokens/Delegates.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Symbol 'ReadTokenHeaderValueDelegate' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

/// <summary>
/// Definition for ReadTokenPayloadValueDelegate.
/// Called for each claim when token payload is being read.
/// </summary>
/// <param name="reader">Reader for the underlying token bytes.</param>
/// <param name="claimName">The name of the claim being read.</param>
/// <returns></returns>
public delegate object ReadTokenPayloadValueDelegate(ref Utf8JsonReader reader, string claimName);

Check failure on line 227 in src/Microsoft.IdentityModel.Tokens/Delegates.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Symbol 'ReadTokenPayloadValueDelegate' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 227 in src/Microsoft.IdentityModel.Tokens/Delegates.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Symbol 'ReadTokenPayloadValueDelegate' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
}
10 changes: 10 additions & 0 deletions src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,16 @@ public string NameClaimType
/// </summary>
public IDictionary<string, object> PropertyBag { get; set; }

/// <summary>
/// Gets or sets a delegate that will be called when reading token payload claims.
/// </summary>
public ReadTokenHeaderValueDelegate ReadTokenHeaderValue { get; set; }

Check failure on line 455 in src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Symbol 'ReadTokenHeaderValue.get' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 455 in src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Symbol 'ReadTokenHeaderValue.set' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 455 in src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Symbol 'ReadTokenHeaderValue.get' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 455 in src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Symbol 'ReadTokenHeaderValue.set' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

/// <summary>
/// Gets or sets a delegate that will be called when reading token payload claims.
/// </summary>
public ReadTokenPayloadValueDelegate ReadTokenPayloadValue { get; set; }

Check failure on line 460 in src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Symbol 'ReadTokenPayloadValue.get' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 460 in src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Symbol 'ReadTokenPayloadValue.set' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

/// <summary>
/// Gets or sets a boolean to control if configuration required to be refreshed before token validation.
/// </summary>
Expand Down

This file was deleted.

0 comments on commit 3613363

Please sign in to comment.