Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ Vinay Karanam
Eduardo Oliveira
Andrea Greco
Dominik George
David Hill
26 changes: 26 additions & 0 deletions docs/tutorial/tutorial_03.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,29 @@ Now supposing your access token value is `123456` you can try to access your aut
::

curl -H "Authorization: Bearer 123456" -X GET http://localhost:8000/secret

Working with Rest_framework generic class based views
-----------------------------------------------------

If you have completed the `Django REST framework tutorial
<https://www.django-rest-framework.org/tutorial/3-class-based-views/#using-generic-class-based-views>`_,
you will be familiar with the 'Snippet' example, in particular the SnippetList and SnippetDetail classes.

It would be really nice to reuse those views, but also support token handling. Instead of reworking
those classes to be ProtectedResourceView based, the solution is much simpler than that.

Assuming you have already modified the settings as was already shown.

The key is setting a class variable to override the default *permissions_classes* with something that will use our :term:`Access Token` properly.

.. code-block:: python

from django.contrib.auth.decorators import login_required

class SnippetList(generics.ListCreateAPIView):
...
permission_classes = [TokenHasReadWriteScope]

class SnippetDetail(generics.ListCreateAPIView):
...
permission_classes = [TokenHasReadWriteScope]