Skip to content

Commit

Permalink
Changing the handling of client claims to use JSON (#4886)
Browse files Browse the repository at this point in the history
* Changing the handling of client claims to use JSON

* Updating tests to account for JSON formatting

---------

Co-authored-by: trwalke <[email protected]>
  • Loading branch information
trwalke and trwalke authored Aug 23, 2024
1 parent 867f5cf commit 8fe46a8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
23 changes: 14 additions & 9 deletions src/client/Microsoft.Identity.Client/Internal/JsonWebToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
using Microsoft.Identity.Client.PlatformsCommon.Interfaces;
using Microsoft.Identity.Client.Utils;
using System.Security.Cryptography;
#if SUPPORTS_SYSTEM_TEXT_JSON
using JObject = System.Text.Json.Nodes.JsonObject;
#else
using Microsoft.Identity.Json.Linq;
#endif

namespace Microsoft.Identity.Client.Internal
{
Expand Down Expand Up @@ -65,18 +70,18 @@ private string CreateJsonPayload()
payload.Append('{');
}

int i = 0;
foreach (var kvp in _claimsToSign)
var json = new JObject();

foreach (var claim in _claimsToSign)
{
payload.Append($"\"{kvp.Key}\":\"{kvp.Value}\"");

if (i!= _claimsToSign.Count-1)
{
i++;
payload.Append(',');
}
json[claim.Key] = claim.Value;
}

var jsonClaims = JsonHelper.JsonObjectToString(json);

//Remove extra brackets from JSON result
payload.Append(jsonClaims.Substring(1, jsonClaims.Length - 2));

payload.Append('}');

return payload.ToString();
Expand Down
2 changes: 1 addition & 1 deletion tests/Microsoft.Identity.Test.Common/TestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static HashSet<string> s_scope
public const string PublicCloudConfidentialClientID = "88f91eac-c606-4c67-a0e2-a5e8a186854f";
public const string AutomationTestCertName = "LabAuth.MSIDLab.com";
public static Dictionary<string, string> AdditionalAssertionClaims =>
new Dictionary<string, string>() { { "Key1", "Val1" }, { "Key2", "Val2" } };
new Dictionary<string, string>() { { "Key1", "Val1" }, { "Key2", "Val2" }, { "customClaims", "{\"xms_az_claim\": [\"GUID\", \"GUID2\", \"GUID3\"]}" } };

public static readonly SortedSet<string> s_scopeForAnotherResource = new SortedSet<string>(new[] { "r2/scope1", "r2/scope2" }, StringComparer.OrdinalIgnoreCase);
public static readonly SortedSet<string> s_cacheMissScope = new SortedSet<string>(new[] { "r3/scope1", "r3/scope2" }, StringComparer.OrdinalIgnoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public void ClientAssertionTests(bool sendX5C, bool useSha2AndPss, bool addExtra
if (appendDefaultClaims == false && addExtraClaims == false)
appendDefaultClaims = true;

int expectedPayloadClaimsCount = (appendDefaultClaims ? 6 : 0) + (addExtraClaims ? 2 : 0);
int expectedPayloadClaimsCount = (appendDefaultClaims ? 6 : 0) + (addExtraClaims ? 3 : 0);
Assert.AreEqual(expectedPayloadClaimsCount, decodedToken.Payload.Count);
if (appendDefaultClaims)
{
Expand All @@ -506,6 +506,8 @@ public void ClientAssertionTests(bool sendX5C, bool useSha2AndPss, bool addExtra
{
Assert.AreEqual("Val1", decodedToken.Payload["Key1"]);
Assert.AreEqual("Val2", decodedToken.Payload["Key2"]);
//Ensure JSON formatting is preserved
Assert.AreEqual("{\"xms_az_claim\": [\"GUID\", \"GUID2\", \"GUID3\"]}", decodedToken.Payload["customClaims"]);
}

if (useSha2AndPss)
Expand Down

0 comments on commit 8fe46a8

Please sign in to comment.