Package: django3-rest-jwt-permission
Django Rest Framework JWT permissionsModule that check API View permissions from JWT payloads.Installationpip install django3-rest-jwt-permissionUsingAddrest_jwt_permissionin yourINSTALLED_APPSand configure thesettingsas you wish. Here is an example:REST_JWT_PERMISSION={"SCOPE_PROVIDERS":["rest_jwt_permission.providers.APIEndpointScopeProvider","rest_jwt_permission.providers.AdminScopeProvider"],"GET_PAYLOAD_FROM_SCOPES_HANDLER":("rest_jwt_permission.handlers.get_payload_from_scopes"),"GET_SCOPES_FROM_PAYLOAD_HANDLER":("rest_jwt_permission.handlers.get_scopes_from_payload"),"GET_PAYLOAD_FROM_REQUEST_HANDLER":("rest_jwt_permission.handlers.get_jwt_payload_from_request")}Now you can useJWTAPIPermissionclass in your API Views throughpermission_classesproperty or even setting it as the default permission class in yoursettingsExampleFor a more pratical example, checkrest-jwt-permission-example.MotivationInspired by GitHubPersonal access tokenand byAuth0 API Keys blog post, this package provides a Django Rest Framework Permission object to check permissions from JWT payloads.This enables your API to check permissions avoiding an extra database hit.How it worksBasically, it extracts a list of all Rest API Views and generate an unique ID for each endpoint + action. Then, after authenticaton, your API should inject which permission identifiers the user has access. The JWT payload should look like the following:{"scopes":["myviewset:get""function_endpoint:get","basicview:get","simpleviewsetpermission:custom_action:put","modelviewsetpermission:retrieve:get","modelviewsetpermission:destroy:delete","modelviewsetpermission:some_detail_metod:patch",]}On each authenticated request, theJWTAPIPermissionpermission class will generate the unique ID for the requested view and will check whether the JWT payloads contains the ID. If so, the user has access.:warning: This package does not automatically injects the scopes payload into the JWT, although we have helpers (rest_jwt_permission.handlers.get_payload_from_scopes) you can use to do that. We strongly recommend you to useREST framework JWT Authpackage as it provides all you need to make this eaiser. You can change the payload handler though it'sJWT_PAYLOAD_HANDLERsetting.You can also create some sort of admin page to select the permissions for user and/or groups like GitHub token scopes, and use that to inject the token into the JWT:ScopesScopes are basically what users can access (has permission to do). Each scope should has an unique identifier (seeScopebase class). You can extend the baseScopeclass and add extra properties and methods.ProvidersProvidersare objects that returns a list of existing scopes. We currently have 2 built-in providers:APIEndpointScopeProvider: returns all scopes for Django REST Framework registered views.AdminScopeProvider: returns admin related scopes. Currently, only returnssuperuserscope.You can develop new providers to your project as you need or even extend the built-ins.SettingsThis project was build with extension in mind, so it is easy to extend, add or remove features.See the list of settings you can customize, all of them are inside theREST_JWT_PERMISSIONsetting key:SCOPE_PROVIDERS: List of providers used to extract the existing scopes.
Defaults to:"SCOPE_PROVIDERS": [
    "rest_jwt_permission.providers.APIEndpointScopeProvider",
    "rest_jwt_permission.providers.AdminScopeProvider"
]GET_PAYLOAD_FROM_SCOPES_HANDLER: Handler function to get the payload with scopes to be injected into JWT. Defaults to:"GET_PAYLOAD_FROM_SCOPES_HANDLER": "rest_jwt_permission.handlers.get_payload_from_scopes"GET_SCOPES_FROM_PAYLOAD_HANDLER: Handler function to get the scopes from a JWT payload. Defaults to:"GET_SCOPES_FROM_PAYLOAD_HANDLER": "rest_jwt_permission.handlers.get_scopes_from_payload"GET_PAYLOAD_FROM_REQUEST_HANDLER: Handler function to get JWT payload from Request. Defaults to:"GET_PAYLOAD_FROM_REQUEST_HANDLER": "rest_jwt_permission.handlers.get_jwt_payload_from_request"JWT_PAYLOAD_SCOPES_KEY: Payload key that will contain the scopes. Defaults to:"JWT_PAYLOAD_SCOPES_KEY": "scopes"Showing all available rolesYou can use theshow_rolesmanagement command to print all available roles according to your providers.python manage.py show_rolesRunning testsInstall dependencies fromdev-requirements.txtand runpy.test --cov:pip install dev-requirements.txt && py.test --covCompatibilityDjango >= 3.2.11Django Rest Framework >= 3.12.2LicenseMIT