Skip to content

Add more support for AuthorizeAttribute #138

Description

@Jack-Edwards

Couple of limitations with regards to customizing the AuthorizeAttribute:

  • Cannot provide Roles due to IAPI0002
  • Cannot provide AuthenticationSchemes due to IAPI0002

Regarding the AuthenticationSchemes, I would like to be able to accept Basic authentication in exchange for a JWT. JWT Bearer authentication would be accepted by all other endpoints.

AuthenticationSchemes Example

public sealed partial record GetTokenQuery();

[Handler]
[Tags("Auth")]
[MapGet("/auth/token")]
[Authorize]
public static partial class GetTokenHandler
{
    private static readonly string[] AuthenticationSchemes = ["Basic"];

    internal static void CustomizeEndpoint(IEndpointConventionBuilder endpoint)
        => endpoint.RequireAuthorization(new AuthorizationPolicy(new List<IAuthorizationRequirement>{ new DenyAnonymousAuthorizationRequirement() }, AuthenticationSchemes));
    
    private static ValueTask<Ok<TokenResponse>> HandleAsync(GetTokenQuery query, ClientAuthorizationService clientAuthorizationService, CancellationToken cancellationToken)
    {
        var token = clientAuthorizationService.CreateToken("foo");
        return ValueTask.FromResult(TypedResults.Ok(token));
    }
}

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions