Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix ID-porten acr claim parsing #1299

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,26 @@ public static bool TryGetOrganizationNumber(this Claim? consumerClaim, [NotNullW

public static bool TryGetAuthenticationLevel(this ClaimsPrincipal claimsPrincipal, [NotNullWhen(true)] out int? authenticationLevel)
{
foreach (var claimType in new[] { IdportenAuthLevelClaim, AltinnAuthLevelClaim })
if (claimsPrincipal.TryGetClaimValue(AltinnAuthLevelClaim, out var claimValue) && int.TryParse(claimValue, out var level))
{
if (!claimsPrincipal.TryGetClaimValue(claimType, out var claimValue)) continue;
// The acr claim value is "LevelX" where X is the authentication level
var valueToParse = claimType == IdportenAuthLevelClaim ? claimValue[5..] : claimValue;
if (!int.TryParse(valueToParse, out var level)) continue;

authenticationLevel = level;
return true;
}

if (claimsPrincipal.TryGetClaimValue(IdportenAuthLevelClaim, out claimValue))
{
// The acr claim value is either "idporten-loa-substantial" (previously "Level3") or "idporten-loa-high" (previously "Level4")
// https://docs.digdir.no/docs/idporten/oidc/oidc_protocol_new_idporten#new-acr-values
authenticationLevel = claimValue switch
{
"idporten-loa-substantial" => 3,
"idporten-loa-high" => 4,
_ => null
};

return authenticationLevel.HasValue;
}
elsand marked this conversation as resolved.
Show resolved Hide resolved

authenticationLevel = null;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Security.Claims;
using Digdir.Domain.Dialogporten.Application.Common.Extensions;

namespace Digdir.Domain.Dialogporten.Application.Unit.Tests.Features.V1.Common.Extensions;

public class ClaimsPrincipalExtensionsTests
{
[Fact]
public void TryGetAuthenticationLevel_Should_Parse_Idporten_Acr_Claim_For_Level3()
{
// Arrange
var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new[]
{
new Claim("acr", "idporten-loa-substantial")
}));

// Act
var result = claimsPrincipal.TryGetAuthenticationLevel(out var authenticationLevel);

// Assert
Assert.True(result);
Assert.Equal(3, authenticationLevel);
}

[Fact]
public void TryGetAuthenticationLevel_Should_Parse_Idporten_Acr_Claim_For_Level4()
{
// Arrange
var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new[]
{
new Claim("acr", "idporten-loa-high")
}));

// Act
var result = claimsPrincipal.TryGetAuthenticationLevel(out var authenticationLevel);

// Assert
Assert.True(result);
Assert.Equal(4, authenticationLevel);
}

[Fact]
public void TryGetAuthenticationLevel_Should_Parse_Altinn_Authlevel_First()
{
// Arrange
var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new[]
{
new Claim("acr", "idporten-loa-high"),
new Claim("urn:altinn:authlevel", "5")
}));

// Act
var result = claimsPrincipal.TryGetAuthenticationLevel(out var authenticationLevel);

// Assert
Assert.True(result);
Assert.Equal(5, authenticationLevel);
}
}
Loading