-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathClaimsHelper.cs
More file actions
138 lines (125 loc) · 4.86 KB
/
ClaimsHelper.cs
File metadata and controls
138 lines (125 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Identity.Client.Utils;
using JObject = System.Text.Json.Nodes.JsonObject;
using System;
namespace Microsoft.Identity.Client.Internal
{
internal static class ClaimsHelper
{
private const string AccessTokenClaim = "access_token";
private const string XmsClientCapability = "xms_cc";
/// <summary>
/// Normalizes a claims JSON string so that semantically identical claims always produce
/// the same string. This prevents cache key fragmentation when callers pass the same
/// logical claims in different whitespace or key-ordering variants.
/// </summary>
internal static string NormalizeClaimsJson(string claimsJson)
{
if (string.IsNullOrWhiteSpace(claimsJson))
{
return claimsJson;
}
try
{
JObject parsed = JsonHelper.ParseIntoJsonObject(claimsJson);
JObject sorted = SortJsonObjectKeys(parsed);
return JsonHelper.JsonObjectToString(sorted);
}
catch (JsonException ex)
{
throw new MsalClientException(
MsalError.InvalidJsonClaimsFormat,
MsalErrorMessage.InvalidJsonClaimsFormat(claimsJson),
ex);
}
}
/// <summary>
/// Merges two JSON claims objects. If either is null/empty the other is returned as-is.
/// </summary>
internal static string MergeClaimsObjects(string claims1, string claims2)
{
if (string.IsNullOrEmpty(claims1)) return claims2;
if (string.IsNullOrEmpty(claims2)) return claims1;
JObject obj1 = JsonHelper.ParseIntoJsonObject(claims1);
JObject obj2 = JsonHelper.ParseIntoJsonObject(claims2);
JObject merged = JsonHelper.Merge(obj1, obj2);
return JsonHelper.JsonObjectToString(merged);
}
private static JObject SortJsonObjectKeys(JObject obj)
{
var sorted = new JObject();
foreach (var key in obj.Select(kvp => kvp.Key).OrderBy(k => k, StringComparer.Ordinal))
{
var value = obj[key];
if (value is JObject nestedObj)
{
sorted[key] = SortJsonObjectKeys(nestedObj);
}
else
{
// JsonNode.DeepClone is .NET 6+; use Parse(ToJsonString()) for portability.
sorted[key] = value is null ? null : JsonNode.Parse(value.ToJsonString());
}
}
return sorted;
}
internal static string GetMergedClaimsAndClientCapabilities(
string claims,
IEnumerable<string> clientCapabilities)
{
if (clientCapabilities != null && clientCapabilities.Any())
{
JObject capabilitiesJson = CreateClientCapabilitiesRequestJson(clientCapabilities);
JObject mergedClaimsAndCapabilities = MergeClaimsIntoCapabilityJson(claims, capabilitiesJson);
return JsonHelper.JsonObjectToString(mergedClaimsAndCapabilities);
}
return claims;
}
internal static JObject MergeClaimsIntoCapabilityJson(string claims, JObject capabilitiesJson)
{
if (!string.IsNullOrEmpty(claims))
{
JObject claimsJson;
try
{
claimsJson = JsonHelper.ParseIntoJsonObject(claims);
}
catch (JsonException ex)
{
throw new MsalClientException(
MsalError.InvalidJsonClaimsFormat,
MsalErrorMessage.InvalidJsonClaimsFormat(claims),
ex);
}
capabilitiesJson = JsonHelper.Merge(capabilitiesJson, claimsJson);
}
return capabilitiesJson;
}
private static JObject CreateClientCapabilitiesRequestJson(IEnumerable<string> clientCapabilities)
{
// "access_token": {
// "xms_cc": {
// values: ["cp1", "cp2"]
// }
// }
return new JObject
{
[AccessTokenClaim] = new JObject
{
[XmsClientCapability] = new JObject
{
["values"] = new JsonArray(clientCapabilities.Select(c => JsonValue.Create(c)).ToArray())
}
}
};
}
}
}