Skip to content

Commit

Permalink
chore(web-api): fixed OverrideClaim, check for serviceproviderscope i…
Browse files Browse the repository at this point in the history
…n middleware (#1037)

Fixed OverrideClaim in ServiceOwnerOnBehalfOfPersonMiddleware, check for
serviceproviderscope in ServiceOwnerOnBehalfOfPersonMiddleware
  • Loading branch information
knuhau authored Aug 21, 2024
1 parent fecc4c0 commit 02e72ef
Showing 1 changed file with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Security.Claims;
using Digdir.Domain.Dialogporten.Application.Common.Extensions;
using Digdir.Domain.Dialogporten.Domain.Parties;
using Digdir.Domain.Dialogporten.WebApi.Common.Authorization;
using Digdir.Domain.Dialogporten.WebApi.Common.Extensions;

namespace Digdir.Domain.Dialogporten.WebApi.Common.Authentication;
Expand All @@ -22,6 +24,11 @@ public Task InvokeAsync(HttpContext context)
return _next(context);
}

if (!context.User.HasScope(AuthorizationScope.ServiceProvider))
{
return _next(context);
}

if (!context.Request.Query.TryGetValue(EndUserId, out var endUserIdQuery))
{
return _next(context);
Expand Down Expand Up @@ -53,21 +60,13 @@ private static void OverrideClaim(ClaimsPrincipal claimsPrincipal, string claimT
{
throw new InvalidOperationException("ClaimsPrincipal does not have a ClaimsIdentity.");
}

if (!claimsPrincipal.HasClaim(claim => claim.Type == claimType))
var existingPidClaims = claimsPrincipal
.FindAll(c => c.Type == claimType)
.ToList();
foreach (var pidClaim in existingPidClaims)
{
identity.AddClaim(new Claim(claimType, newClaimValue));
return;
pidClaim.Subject?.RemoveClaim(pidClaim);
}

foreach (var ident in claimsPrincipal.Identities)
{
foreach (var claim in ident.FindAll(c => c.Type == claimType))
{
ident.RemoveClaim(claim);
}
}

identity.AddClaim(new Claim(claimType, newClaimValue));
}
}
Expand Down

0 comments on commit 02e72ef

Please sign in to comment.