Permissions

    More details on how to add custom permissions to your API Endpoints can be found at the official Django REST Framework documentation

    The TokenHasScope permission class allows access only when the current access token has been authorized for all the scopes listed in the required_scopes field of the view.

    For example:

    The required_scopes attribute is mandatory.

    The TokenHasReadWriteScope permission class allows access based on the READ_SCOPE and WRITE_SCOPE configured in the settings.

    The required_scopes attribute is optional and can be used by other scopes needed in the view.

    For example:

    1. authentication_classes = [OAuth2Authentication]
    2. permission_classes = [TokenHasReadWriteScope]
    3. required_scopes = ['music']

    When a request is performed both the READ_SCOPE \ WRITE_SCOPE and ‘music’ scopes are required to be authorized for the current access token.

    The TokenHasResourceScope permission class allows access only when the current access token has been authorized for all the scopes listed in the required_scopes field of the view but according of request’s method.

    When the current request’s method is one of the “safe” methods, the access is allowed only if the access token has been authorized for the scope:read scope (for example music:read). When the request’s method is one of “non safe” methods, the access is allowed only if the access token has been authorized for the scope:write scope (for example music:write).

    The IsAuthenticatedOrTokenHasScope permission class allows access only when the current access token has been authorized for all the scopes listed in the required_scopes field of the view but according to the request’s method. It also allows access to Authenticated users who are authenticated in django, but were not authenticated through the OAuth2Authentication class. This allows for protection of the API using scopes, but still let’s users browse the full browseable API. To restrict users to only browse the parts of the browseable API they should be allowed to see, you can combine this with the DjangoModelPermission or the DjangoObjectPermission.

    For example:

    1. class SongView(views.APIView):
    2. permission_classes = [IsAuthenticatedOrTokenHasScope, DjangoModelPermission]
    3. required_scopes = ['music']

    The required_scopes attribute is mandatory.

    The TokenMatchesOASRequirements permission class allows the access based on a per-method basis and with alternative lists of required scopes. This permission provides full functionality required by REST API specifications like the .

    The required_alternate_scopes attribute is a required map keyed by HTTP method name where each value is a list of alternative lists of required scopes.

    The following is a minimal OAS declaration that shows the same required alternate scopes. It is complete enough to try it in the swagger editor.

    1. openapi: "3.0.0"
    2. info:
    3. title: songs
    4. version: v1
    5. components:
    6. securitySchemes:
    7. song_auth:
    8. flows:
    9. implicit:
    10. authorizationUrl: http://localhost:8000/o/authorize
    11. read: read about a song
    12. create: create a new song
    13. update: update an existing song
    14. delete: delete a song
    15. post: create a new song
    16. widget: widget scope
    17. scope2: scope too
    18. scope3: another scope
    19. paths:
    20. /songs:
    21. get:
    22. security:
    23. - song_auth: [read]
    24. responses:
    25. '200':
    26. post:
    27. - song_auth: [create]
    28. - song_auth: [post, widget]
    29. responses:
    30. '201':
    31. description: new song added
    32. put:
    33. security:
    34. - song_auth: [update]
    35. - song_auth: [put, widget]
    36. responses:
    37. '204':
    38. description: song updated
    39. delete:
    40. security:
    41. - song_auth: [delete]
    42. - song_auth: [scope2, scope3]
    43. responses:
    44. description: song deleted