Skip to content

Commit

Permalink
Add system user to subject category builder for xacml
Browse files Browse the repository at this point in the history
  • Loading branch information
oskogstad committed May 28, 2024
1 parent b94fd24 commit 738f2de
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Altinn.Authorization.ABAC.Xacml.JsonProfile;
using System.Security.Claims;
using System.Text.Json;
using Digdir.Domain.Dialogporten.Application.Common.Authorization;
using Digdir.Domain.Dialogporten.Application.Common.Extensions;
using Digdir.Domain.Dialogporten.Application.Externals.AltinnAuthorization;
Expand Down Expand Up @@ -63,9 +64,7 @@ private static List<XacmlJsonCategory> CreateAccessSubjectCategory(IEnumerable<C
{ Type: PidClaimType } => new XacmlJsonAttribute { AttributeId = NorwegianPersonIdentifier.Prefix, Value = x.Value },
{ Type: var type } when type.StartsWith(AltinnUrnNsPrefix, StringComparison.Ordinal) => new() { AttributeId = type, Value = x.Value },
{ Type: ConsumerClaimType } when x.TryGetOrganizationNumber(out var organizationNumber) => new() { AttributeId = NorwegianOrganizationIdentifier.Prefix, Value = organizationNumber },
// See question about array in ClaimsPrincipalExtensions.cs line 80
// TODO: https://docs.altinn.studio/authentication/systemauthentication/
{ Type: AltinnAutorizationDetailsClaim } => new() { AttributeId = AttributeIdSystemUser, Value = "some_unique_system_user_id" },
{ Type: AltinnAutorizationDetailsClaim } => new() { AttributeId = AttributeIdSystemUser, Value = GetSystemUserId(x) },
_ => null
})
.Where(x => x is not null)
Expand All @@ -82,6 +81,13 @@ private static List<XacmlJsonCategory> CreateAccessSubjectCategory(IEnumerable<C
return [new() { Id = SubjectId, Attribute = attributes }];
}

private static string GetSystemUserId(Claim claim)
{
var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new[] { claim }));
claimsPrincipal.TryGetSystemUserId(out var systemUserId);
return systemUserId!;
}

private static List<XacmlJsonCategory> CreateActionCategories(
HashSet<AltinnAction> altinnActions, out Dictionary<string, string> actionIdByName)
{
Expand Down

0 comments on commit 738f2de

Please sign in to comment.