Skip to content

Commit

Permalink
feat: Convert RolesClaimTransformationSource to flags to allow for co…
Browse files Browse the repository at this point in the history
…mbining sources, add "All" flag for convience.
  • Loading branch information
Alexr03 committed May 9, 2024
1 parent 957dc16 commit f7a4a90
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)

var result = principal.Clone();

if (this.roleSource == RolesClaimTransformationSource.ResourceAccess)
if (this.roleSource.HasFlag(RolesClaimTransformationSource.ResourceAccess))
{
var resourceAccessValue = principal.FindFirst("resource_access")?.Value;
if (string.IsNullOrWhiteSpace(resourceAccessValue))
Expand Down Expand Up @@ -107,11 +107,9 @@ out var rolesElement
identity.AddClaim(new Claim(this.roleClaimType, value));
}
}

return Task.FromResult(result);
}

if (this.roleSource == RolesClaimTransformationSource.Realm)
if (this.roleSource.HasFlag(RolesClaimTransformationSource.Realm))
{
var realmAccessValue = principal.FindFirst("realm_access")?.Value;
if (string.IsNullOrWhiteSpace(realmAccessValue))
Expand Down Expand Up @@ -144,8 +142,6 @@ out var rolesElement
identity.AddClaim(new Claim(this.roleClaimType, value));
}
}

return Task.FromResult(result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,26 @@ public class KeycloakAuthorizationOptions : KeycloakInstallationOptions
/// <summary>
/// RolesClaimTransformationSource
/// </summary>
[Flags]
public enum RolesClaimTransformationSource
{
/// <summary>
/// Specifies that no transformation should be applied from the source.
/// </summary>
None,
None = 0,

/// <summary>
/// Specifies that transformation should be applied to the realm.
/// </summary>
Realm,
Realm = 1 << 0,

/// <summary>
/// Specifies that transformation should be applied to the resource access.
/// </summary>
ResourceAccess
ResourceAccess = 1 << 1,

/// <summary>
/// Specifies that transformation should be applied to all sources.
/// </summary>
All = Realm | ResourceAccess
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class KeycloakRolesClaimsTransformationTests
[Theory]
[InlineData(RolesClaimTransformationSource.Realm)]
[InlineData(RolesClaimTransformationSource.ResourceAccess)]
[InlineData(RolesClaimTransformationSource.All)]
public async Task ClaimsTransformationShouldMap(RolesClaimTransformationSource roleSource)
{
var target = new KeycloakRolesClaimsTransformation(ClaimTypes.Role, roleSource, ClientId);
Expand All @@ -25,6 +26,20 @@ public async Task ClaimsTransformationShouldMap(RolesClaimTransformationSource r
}
}

[Fact]
public async Task ClaimsTransformationShouldHandleNoneSource()
{
var target = new KeycloakRolesClaimsTransformation(
ClaimTypes.Role,
RolesClaimTransformationSource.None,
ClientId
);
var claimsPrincipal = GetClaimsPrincipal(MyRealmClaimValue, MyResourceClaimValue);

claimsPrincipal = await target.TransformAsync(claimsPrincipal);
claimsPrincipal.Claims.Count(item => ClaimTypes.Role == item.Type).Should().Be(0);
}

[Fact]
public async Task ClaimsTransformationShouldHandleMissingResourceClaim()
{
Expand Down

0 comments on commit f7a4a90

Please sign in to comment.