diff --git a/test/Microsoft.IdentityModel.JsonWebTokens.Tests/CustomJsonWebToken.cs b/test/Microsoft.IdentityModel.JsonWebTokens.Tests/CustomJsonWebToken.cs index 813b82e4b3..f0e03afcf1 100644 --- a/test/Microsoft.IdentityModel.JsonWebTokens.Tests/CustomJsonWebToken.cs +++ b/test/Microsoft.IdentityModel.JsonWebTokens.Tests/CustomJsonWebToken.cs @@ -1,13 +1,27 @@ -using System; +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +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"; + // Represents claims known to this custom implementation and not to the IdentityModel. + public const string CustomClaimName = "CustomClaim"; + + private CustomClaim _customClaim; + + public CustomClaim CustomClaim + { + get + { + _customClaim ??= Payload.GetValue(CustomClaimName); + return _customClaim; + } + } public CustomJsonWebToken(string jwtEncodedString) : base(jwtEncodedString) { } @@ -17,26 +31,29 @@ public CustomJsonWebToken(string header, string payload) : base(header, payload) private protected override void ReadPayloadValue(ref Utf8JsonReader reader, IDictionary claims) { + // Handle custom claims. if (reader.ValueTextEquals(CustomClaimName)) { - _customClaim = JsonSerializerPrimitives.ReadString(ref reader, CustomClaimName, ClassName, true); + // Deserialize the custom object claim in an appropriate way. + reader.Read(); // Move to the value. + _customClaim = JsonSerializer.Deserialize(reader.GetString()); claims[CustomClaimName] = _customClaim; + reader.Read(); } else { + // Call base implementation to handle other claims known to IdentityModel. base.ReadPayloadValue(ref reader, claims); } } + } - private string _customClaim; - - public string CustomClaim + public class CustomClaim + { + public CustomClaim() { - get - { - _customClaim ??= Payload.GetStringValue(CustomClaimName); - return _customClaim; - } } + + public string CustomClaimValue { get; set; } } } diff --git a/test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenTests.cs b/test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenTests.cs index e0c2133600..185cb1f860 100644 --- a/test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenTests.cs +++ b/test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenTests.cs @@ -1743,19 +1743,22 @@ public void StringAndMemoryConstructors_CreateEquivalentTokens(JwtTheoryData the [Fact] public void DerivedJsonWebToken_IsCreatedCorrectly() { - var expectedCustomClaim = "customclaim"; + var expectedCustomClaim = new CustomClaim() { CustomClaimValue = "customclaim" }; var tokenStr = new JsonWebTokenHandler().CreateToken(new SecurityTokenDescriptor { Issuer = Default.Issuer, Claims = new Dictionary { - { "CustomClaim", expectedCustomClaim }, + { CustomJsonWebToken.CustomClaimName, System.Text.Json.JsonSerializer.Serialize(expectedCustomClaim) }, } }); var derivedToken = new CustomJsonWebToken(tokenStr); + derivedToken.TryGetPayloadValue( + CustomJsonWebToken.CustomClaimName, out CustomClaim customClaim); - Assert.Equal(expectedCustomClaim, derivedToken.CustomClaim); + Assert.Equal(expectedCustomClaim.CustomClaimValue, derivedToken.CustomClaim.CustomClaimValue); + Assert.Equal(expectedCustomClaim.CustomClaimValue, customClaim.CustomClaimValue); Assert.Equal(Default.Issuer, derivedToken.Issuer); }