-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update JsonWebToken to enable extensibility (#2582)
* Extract token reading code into a separate ReadPropertyValue. * Rename. Use IDictionary. * Add a test. Add JsonWebTokens InternalsVisibleTo for tests.
- Loading branch information
Showing
4 changed files
with
141 additions
and
71 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
src/Microsoft.IdentityModel.JsonWebTokens/InternalsVisibleTo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.IdentityModel.JsonWebTokens.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
test/Microsoft.IdentityModel.JsonWebTokens.Tests/CustomJsonWebToken.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using Microsoft.IdentityModel.Tokens.Json; | ||
|
||
namespace Microsoft.IdentityModel.JsonWebTokens.Tests | ||
{ | ||
public class CustomJsonWebToken : JsonWebToken | ||
{ | ||
private const string CustomClaimName = "CustomClaim"; | ||
|
||
public CustomJsonWebToken(string jwtEncodedString) : base(jwtEncodedString) { } | ||
|
||
public CustomJsonWebToken(ReadOnlyMemory<char> encodedTokenMemory) : base(encodedTokenMemory) { } | ||
|
||
public CustomJsonWebToken(string header, string payload) : base(header, payload) { } | ||
|
||
private protected override void ReadPayloadValue(ref Utf8JsonReader reader, IDictionary<string, object> claims) | ||
{ | ||
if (reader.ValueTextEquals(CustomClaimName)) | ||
{ | ||
_customClaim = JsonSerializerPrimitives.ReadString(ref reader, CustomClaimName, ClassName, true); | ||
claims[CustomClaimName] = _customClaim; | ||
} | ||
else | ||
{ | ||
base.ReadPayloadValue(ref reader, claims); | ||
} | ||
} | ||
|
||
private string _customClaim; | ||
|
||
public string CustomClaim | ||
{ | ||
get | ||
{ | ||
_customClaim ??= Payload.GetStringValue(CustomClaimName); | ||
return _customClaim; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters