ChilliCream / ChilliCream/graphql-platform

Custom authorization attribute for Hotchocolate.Authorization

Open
#6,817 1 comment 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

🌶️ hot chocolate Area: Authorization
Dominant language
C#
Stars
5.8k
Forks
810
Avg merge
15h 39m
Merged PRs (30d)
98

Description

Product

Hot Chocolate

Is your feature request related to a problem?

Policies in some cases are not handy as custom attributes with parameters such as [AuthorizeAge(22)] on Queries or Mutation

The solution you'd like

Make it possibile to write custom authorize attributes like microsoft Authorization

public class AuthorizeAgeAttribute : TypeFilterAttribute
{
    public AuthorizeAgeAttribute(int age) : base(typeof(AuthorizeAge))
    {
        Arguments = new object[] { age };
    }
}
public class AuthorizeAge : IAuthorizationFilter
{
    private readonly int _age;
    private readonly IUserService _userService;

    public AuthorizeMacroAndMicro(int age, IUserService userService)
    {
        _age = age;
        _userService = userService;
    }

    public void OnAuthorization(AuthorizationFilterContext context)
    {
        if (_userService.Age < _age)
            context.Result = new ForbidResult();
    }
}

And being able to use it in mutation or queries like:

 [ExtendObjectType("Query")]
 public class HorrorFilmsQueryResolver
 {
     public HorrorFilmsQueryResolver() { }

     [GraphQLName("HorrorFilms")]
     [AuthorizeAge(18)]
     public Task<IEnumerable<Film>> Get([Service] IHorrorFilmsService filmService) => filmService.GetAll();

 }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start by tracing Hot Chocolate authorization support and its handling of attributes on the example Query or mutation entry points, then review existing authorization tests if available; done means a parameterized custom attribute can be applied to a resolver and enforce the requested authorization decision.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, authorization
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.