Skip to content

Conversation

@johnnyreilly
Copy link

@johnnyreilly johnnyreilly commented Jan 15, 2021

Hey @MaximRouiller!

This PR relates to #11 and provides a mechanism by which claims can be transformed. Rather than just transforming roles claims "in the box", this provides the consumer an OnClaimsReceived event which allows them to transform claims prior to them being supplied to the Principal.

To support the use case discussed in #11 of transforming roles claims, a user would do something like the following:

services.AddAuthentication("EasyAuth").AddEasyAuthAuthentication(options =>
    options.Events.OnClaimsReceived = (claims) => {
        var mappedRolesClaims = claims
            .Where(claim => claim.Type == "roles")
            .Select(claim => new Claim(ClaimTypes.Role, claim.Value))
            .ToList();

        return Task.FromResult(claims.Concat(mappedRolesClaims));
    });

I've made the hook async to align with the general pattern I see throughout the .NET codebase; see https://github.com/dotnet/aspnetcore/blob/master/src/Security/Authentication/Core/src/Events/RemoteAuthenticationEvents.cs for reference.

I've also added a EasyAuthAuthenticationDefaults.AuthenticationScheme = "EasyAuth" const which I thought was "nice to have" - completely not necessary but again aligns nicely with https://github.com/dotnet/aspnetcore/blob/master/src/Security/Authentication/Cookies/src/CookieAuthenticationDefaults.cs#L16

I've tested this with a project of mine deployed to an App Service and it's worked well. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant