Skip to content

Commit

Permalink
Test updates. (#3080)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaytak authored Jan 9, 2025
1 parent 36ec5c0 commit 5090280
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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<CustomClaim>(CustomClaimName);
return _customClaim;
}
}

public CustomJsonWebToken(string jwtEncodedString) : base(jwtEncodedString) { }

Expand All @@ -17,26 +31,29 @@ public CustomJsonWebToken(string header, string payload) : base(header, payload)

private protected override void ReadPayloadValue(ref Utf8JsonReader reader, IDictionary<string, object> 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<CustomClaim>(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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>
{
{ "CustomClaim", expectedCustomClaim },
{ CustomJsonWebToken.CustomClaimName, System.Text.Json.JsonSerializer.Serialize(expectedCustomClaim) },
}
});

var derivedToken = new CustomJsonWebToken(tokenStr);
derivedToken.TryGetPayloadValue<CustomClaim>(
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);
}

Expand Down

0 comments on commit 5090280

Please sign in to comment.