Skip to content

Commit 804a408

Browse files
authored
Fix UniqueJsonKeyClaimAction key comparison dotnet#38572 (dotnet#38623)
1 parent bab568a commit 804a408

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/Security/Authentication/OpenIdConnect/src/UniqueJsonKeyClaimAction.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public override void Run(JsonElement userData, ClaimsIdentity identity, string i
3636
return;
3737
}
3838

39-
var claim = identity.FindFirst(c => string.Equals(c.Type, JsonKey, System.StringComparison.OrdinalIgnoreCase));
40-
if (claim != null && string.Equals(claim.Value, value, System.StringComparison.Ordinal))
39+
var claim = identity.FindFirst(c => string.Equals(c.Type, ClaimType, StringComparison.OrdinalIgnoreCase));
40+
if (claim != null && string.Equals(claim.Value, value, StringComparison.Ordinal))
4141
{
4242
// Duplicate
4343
return;
@@ -47,9 +47,9 @@ public override void Run(JsonElement userData, ClaimsIdentity identity, string i
4747
{
4848
// If this claimType is mapped by the JwtSeurityTokenHandler, then this property will be set
4949
return c.Properties.TryGetValue(JwtSecurityTokenHandler.ShortClaimTypeProperty, out var shortType)
50-
&& string.Equals(shortType, JsonKey, System.StringComparison.OrdinalIgnoreCase);
50+
&& string.Equals(shortType, ClaimType, StringComparison.OrdinalIgnoreCase);
5151
});
52-
if (claim != null && string.Equals(claim.Value, value, System.StringComparison.Ordinal))
52+
if (claim != null && string.Equals(claim.Value, value, StringComparison.Ordinal))
5353
{
5454
// Duplicate with an alternate name.
5555
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Security.Claims;
5+
using System.Text.Json;
6+
using Microsoft.AspNetCore.Authentication.OpenIdConnect.Claims;
7+
8+
namespace Microsoft.AspNetCore.Authentication.Test.OpenIdConnect;
9+
10+
public class UniqueJsonKeyClaimActionTests
11+
{
12+
[Fact]
13+
public void AddsIfNoDuplicateExists()
14+
{
15+
var userData = JsonDocument.Parse("{ \"jsonKey\": \"value\" }");
16+
17+
var identity = new ClaimsIdentity();
18+
19+
var action = new UniqueJsonKeyClaimAction("claimType", "valueType", "jsonKey");
20+
action.Run(userData.RootElement, identity, "iss");
21+
22+
var claim = identity.FindFirst("claimType");
23+
Assert.NotNull(claim);
24+
Assert.Equal("claimType", claim.Type);
25+
Assert.Equal("value", claim.Value);
26+
}
27+
28+
[Fact]
29+
public void DoesNotAddIfDuplicateExists()
30+
{
31+
var userData = JsonDocument.Parse("{ \"jsonKey\": \"value\" }");
32+
33+
var identity = new ClaimsIdentity();
34+
identity.AddClaim(new Claim("claimType", "value", "valueType"));
35+
36+
var action = new UniqueJsonKeyClaimAction("claimType", "valueType", "jsonKey");
37+
action.Run(userData.RootElement, identity, "iss");
38+
39+
var claims = identity.FindAll("claimType");
40+
Assert.Single(claims);
41+
Assert.Equal("claimType", claims.First().Type);
42+
Assert.Equal("value", claims.First().Value);
43+
}
44+
}

0 commit comments

Comments
 (0)