Skip to content

Commit 8fe46a8

Browse files
trwalketrwalke
andauthored
Changing the handling of client claims to use JSON (#4886)
* Changing the handling of client claims to use JSON * Updating tests to account for JSON formatting --------- Co-authored-by: trwalke <[email protected]>
1 parent 867f5cf commit 8fe46a8

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

src/client/Microsoft.Identity.Client/Internal/JsonWebToken.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
using Microsoft.Identity.Client.PlatformsCommon.Interfaces;
99
using Microsoft.Identity.Client.Utils;
1010
using System.Security.Cryptography;
11+
#if SUPPORTS_SYSTEM_TEXT_JSON
12+
using JObject = System.Text.Json.Nodes.JsonObject;
13+
#else
14+
using Microsoft.Identity.Json.Linq;
15+
#endif
1116

1217
namespace Microsoft.Identity.Client.Internal
1318
{
@@ -65,18 +70,18 @@ private string CreateJsonPayload()
6570
payload.Append('{');
6671
}
6772

68-
int i = 0;
69-
foreach (var kvp in _claimsToSign)
73+
var json = new JObject();
74+
75+
foreach (var claim in _claimsToSign)
7076
{
71-
payload.Append($"\"{kvp.Key}\":\"{kvp.Value}\"");
72-
73-
if (i!= _claimsToSign.Count-1)
74-
{
75-
i++;
76-
payload.Append(',');
77-
}
77+
json[claim.Key] = claim.Value;
7878
}
7979

80+
var jsonClaims = JsonHelper.JsonObjectToString(json);
81+
82+
//Remove extra brackets from JSON result
83+
payload.Append(jsonClaims.Substring(1, jsonClaims.Length - 2));
84+
8085
payload.Append('}');
8186

8287
return payload.ToString();

tests/Microsoft.Identity.Test.Common/TestConstants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static HashSet<string> s_scope
3535
public const string PublicCloudConfidentialClientID = "88f91eac-c606-4c67-a0e2-a5e8a186854f";
3636
public const string AutomationTestCertName = "LabAuth.MSIDLab.com";
3737
public static Dictionary<string, string> AdditionalAssertionClaims =>
38-
new Dictionary<string, string>() { { "Key1", "Val1" }, { "Key2", "Val2" } };
38+
new Dictionary<string, string>() { { "Key1", "Val1" }, { "Key2", "Val2" }, { "customClaims", "{\"xms_az_claim\": [\"GUID\", \"GUID2\", \"GUID3\"]}" } };
3939

4040
public static readonly SortedSet<string> s_scopeForAnotherResource = new SortedSet<string>(new[] { "r2/scope1", "r2/scope2" }, StringComparer.OrdinalIgnoreCase);
4141
public static readonly SortedSet<string> s_cacheMissScope = new SortedSet<string>(new[] { "r3/scope1", "r3/scope2" }, StringComparer.OrdinalIgnoreCase);

tests/Microsoft.Identity.Test.Unit/PublicApiTests/ClientCredentialWithCertTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ public void ClientAssertionTests(bool sendX5C, bool useSha2AndPss, bool addExtra
480480
if (appendDefaultClaims == false && addExtraClaims == false)
481481
appendDefaultClaims = true;
482482

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

511513
if (useSha2AndPss)

0 commit comments

Comments
 (0)