Skip to content

Commit 67bc569

Browse files
committed
CBV docs, fixes #178
1 parent 8d42125 commit 67bc569

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

docs/views/class_based.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,58 @@
11
Class-based Views
22
=================
33

4+
Django OAuth Toolkit provides generic classes useful to implement OAuth2 protected endpoints
5+
using the *Class Based View* approach.
6+
7+
8+
.. class:: ProtectedResourceView(ProtectedResourceMixin, View):
9+
10+
A view that provides OAuth2 authentication out of the box. To implement a protected
11+
endpoint, just define your CBV as::
12+
13+
class MyEndpoint(ProtectedResourceView):
14+
"""
15+
A GET endpoint that needs OAuth2 authentication
16+
"""
17+
def get(self, request, *args, **kwargs):
18+
return HttpResponse('Hello, World!')
19+
20+
**Please notice**: ``OPTION`` method is not OAuth2 protected to allow preflight requests.
21+
22+
.. class:: ScopedProtectedResourceView(ScopedResourceMixin, ProtectedResourceView):
23+
24+
A view that provides OAuth2 authentication and scopes handling out of the box. To implement
25+
a protected endpoint, just define your CBV specifying the ``required_scopes`` field::
26+
27+
class MyScopedEndpoint(ScopedProtectedResourceView):
28+
required_scopes = ['can_make_it can_break_it']
29+
30+
"""
31+
A GET endpoint that needs OAuth2 authentication
32+
and a set of scopes: 'can_make_it' and 'can_break_it'
33+
"""
34+
def get(self, request, *args, **kwargs):
35+
return HttpResponse('Hello, World!')
36+
37+
38+
.. class:: ReadWriteScopedResourceView(ReadWriteScopedResourceMixin, ProtectedResourceView):
39+
40+
A view that provides OAuth2 authentication and read/write default scopes.
41+
``GET``, ``HEAD``, ``OPTIONS`` http methods require ``read`` scope, others methods
42+
need the ``write`` scope. If you need, you can always specify an additional list of
43+
scopes in the ``required_scopes`` field::
44+
45+
class MyRWEndpoint(ReadWriteScopedResourceView):
46+
required_scopes = ['has_additional_powers'] # optional
47+
48+
"""
49+
A GET endpoint that needs OAuth2 authentication
50+
and the 'read' scope. If required_scopes was specified,
51+
clients also need those scopes.
52+
"""
53+
def get(self, request, *args, **kwargs):
54+
return HttpResponse('Hello, World!')
55+
56+
57+
Generic views in DOT are obtained composing a set of mixins you can find in the :doc:`views.mixins <mixins>`
58+
module: feel free to use those mixins directly if you want to provide your own class based views.

docs/views/mixins.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Mixins for Class Based Views
2+
============================
3+
4+
.. automodule:: oauth2_provider.views.mixins
5+
:members:

docs/views/views.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Django OAuth Toolkit provides a set of pre-defined views for different purposes:
99
function_based
1010
class_based
1111
application
12+
mixins

0 commit comments

Comments
 (0)