Skip to content

Commit 16f160d

Browse files
authored
fix: Add system user id to identifying claims (#1362)
## Description This adds a check to include the system user id in the list of identifiable claims, which is in turn used to generate a cache key for authorization requests on dialog details accesses. ## Related Issue(s) - #1363 ## Verification - [x] **Your** code builds clean without any errors or warnings - [x] Manual testing done (required) - [x] Relevant automated test added (if you find this hard, leave it and we'll help out) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced methods to simplify the retrieval of system user IDs from claims. - Enhanced claims processing to include system user identifiers from authorization details. - **Bug Fixes** - Streamlined logic in handling user ID extraction, improving efficiency. - **Tests** - Added a test to verify the correct extraction of system user identifiers from claims. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 4974676 commit 16f160d

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/Digdir.Domain.Dialogporten.Application/Common/Extensions/ClaimsPrincipalExtensions.cs

+25-3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ private static bool TryGetAuthorizationDetailsClaimValue(this ClaimsPrincipal cl
110110
return authorizationDetails is not null;
111111
}
112112

113+
public static bool TryGetSystemUserId(this Claim claim,
114+
[NotNullWhen(true)] out string? systemUserId) =>
115+
new List<Claim> { claim }.TryGetSystemUserId(out systemUserId);
116+
117+
public static bool TryGetSystemUserId(this List<Claim> claimsList,
118+
[NotNullWhen(true)] out string? systemUserId) =>
119+
new ClaimsPrincipal(new ClaimsIdentity(claimsList.ToArray())).TryGetSystemUserId(out systemUserId);
120+
113121
public static bool TryGetSystemUserId(this ClaimsPrincipal claimsPrincipal,
114122
[NotNullWhen(true)] out string? systemUserId)
115123
{
@@ -198,14 +206,28 @@ public static bool TryGetAuthenticationLevel(this ClaimsPrincipal claimsPrincipa
198206
return false;
199207
}
200208

201-
public static IEnumerable<Claim> GetIdentifyingClaims(this List<Claim> claims) =>
202-
claims.Where(c =>
209+
public static IEnumerable<Claim> GetIdentifyingClaims(this IEnumerable<Claim> claims)
210+
{
211+
var claimsList = claims.ToList();
212+
213+
var identifyingClaims = claimsList.Where(c =>
203214
c.Type == PidClaim ||
204215
c.Type == ConsumerClaim ||
205216
c.Type == SupplierClaim ||
206217
c.Type == IdportenAuthLevelClaim ||
207218
c.Type.StartsWith(AltinnClaimPrefix, StringComparison.Ordinal)
208-
).OrderBy(c => c.Type);
219+
).OrderBy(c => c.Type).ToList();
220+
221+
// If we have a RAR-claim, this is most likely a system user. Attempt to extract the
222+
// systemuser-uuid from the authorization_details claim and add to the list.
223+
var rarClaim = claimsList.FirstOrDefault(c => c.Type == AuthorizationDetailsClaim);
224+
if (rarClaim != null && rarClaim.TryGetSystemUserId(out var systemUserId))
225+
{
226+
identifyingClaims.Add(new Claim(AuthorizationDetailsType, systemUserId));
227+
}
228+
229+
return identifyingClaims;
230+
}
209231

210232
public static (UserIdType, string externalId) GetUserType(this ClaimsPrincipal claimsPrincipal)
211233
{

src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/DecisionRequestHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static List<XacmlJsonCategory> CreateAccessSubjectCategory(IEnumerable<C
101101
Id = SubjectId,
102102
Attribute = [new() { AttributeId = AttributeIdPerson, Value = claim.Value }]
103103
},
104-
RarAuthorizationDetailsClaimType when new ClaimsPrincipal(new ClaimsIdentity(new[] { claim })).TryGetSystemUserId(out var systemUserId) => new XacmlJsonCategory
104+
RarAuthorizationDetailsClaimType when claim.TryGetSystemUserId(out var systemUserId) => new XacmlJsonCategory
105105
{
106106
Id = SubjectId,
107107
Attribute =

tests/Digdir.Domain.Dialogporten.Application.Unit.Tests/Features/V1/Common/Extensions/ClaimsPrincipalExtensionsTests.cs

+15
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,19 @@ public void TryGetAuthenticationLevel_Should_Parse_Altinn_Authlevel_First()
5353
Assert.True(result);
5454
Assert.Equal(5, authenticationLevel);
5555
}
56+
57+
[Fact]
58+
public void GetIdentifyingClaims_Should_Include_SystemUserIdentifier_From_AuthorizationDetails()
59+
{
60+
// Arrange
61+
var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity([
62+
new Claim("authorization_details", "[{\"type\":\"urn:altinn:systemuser\",\"systemuser_id\":[\"e3b87b08-dce6-4edd-8308-db887950a83b\"],\"systemuser_org\":{\"authority\":\"iso6523-actorid-upis\",\"ID\":\"0192:991825827\"},\"system_id\":\"1d81b874-f139-4842-bd0a-e5cc64319272\"}]")
63+
]));
64+
65+
// Act
66+
var identifyingClaims = claimsPrincipal.Claims.GetIdentifyingClaims();
67+
68+
// Assert
69+
Assert.Contains(identifyingClaims, c => c.Type == "urn:altinn:systemuser" && c.Value == "e3b87b08-dce6-4edd-8308-db887950a83b");
70+
}
5671
}

0 commit comments

Comments
 (0)