rscarrera27 / Sanic-JWT-Extended

⚡️An open source Sanic extension that provides "extended" JWT support
https://sanic-jwt-extended.seonghyeon.dev
MIT License
35 stars 8 forks source link

Feature/multirole rbac #33

Open vi3m opened 3 years ago

vi3m commented 3 years ago

I had to made this change to support RBAC with multiple roles. This is under such scenarios, where we want a user to have more than one role. Made up random use case: A user can be an ADMIN for say 2 out of 5 sections of the application. Or, if we want to implement RBAC for a Suite of Applications.

At the moment, when creating an access token, it already accepts a list of roles.

For eg, create_access_token(...., role=['ADMIN', 'MANAGER']) totally works. And we can successfully retrieve the list of roles from token.role

The only drawback here is, when using jwt_required with allow or deny parameters, this list of roles are not handled, it is only handled when the role is a string. This PR is an attempt to handle that.

Currently the functionality is that, if at least one role in token.role is in allow or deny list. It allows or denies.

codecov[bot] commented 3 years ago

Codecov Report

:exclamation: No coverage uploaded for pull request base (master@0eb9282). Click here to learn what that means. The diff coverage is 20.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master      #33   +/-   ##
=========================================
  Coverage          ?   85.30%           
=========================================
  Files             ?        9           
  Lines             ?      422           
  Branches          ?        0           
=========================================
  Hits              ?      360           
  Misses            ?       62           
  Partials          ?        0           
Flag Coverage Δ
unittests 85.30% <20.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
sanic_jwt_extended/decorators.py 70.71% <20.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 0eb9282...debe3d2. Read the comment docs.