Skip to content

Commit 21b0426

Browse files
author
Massimiliano
committed
decorators docs
1 parent 763073f commit 21b0426

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

docs/views/function_based.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,65 @@
11
Function-based views
22
====================
33

4+
Django OAuth Toolkit provides decorators to help you in protecting your function-based views.
5+
6+
.. function:: protected_resource(scopes=None, validator_cls=OAuth2Validator, server_cls=Server)
7+
8+
Decorator to protect views by providing OAuth2 authentication out of the box, optionally with
9+
scope handling. Basic usage, without using scopes::
10+
11+
from oauth2_provider.decorators import protected_resource
12+
13+
@protected_resource()
14+
def my_view(request):
15+
# An access token is required to get here...
16+
# ...
17+
pass
18+
19+
If you want to check scopes as well when accessing a view you can pass them along as
20+
decorator's parameter::
21+
22+
from oauth2_provider.decorators import protected_resource
23+
24+
@protected_resource(scopes=['can_make_it can_break_it'])
25+
def my_view(request):
26+
# An access token AND the right scopes are required to get here...
27+
# ...
28+
pass
29+
30+
The decorator also accept server and validator classes if you want or need to use your own
31+
OAuth2 logic::
32+
33+
from oauth2_provider.decorators import protected_resource
34+
from myapp.oauth2_validators import MyValidator
35+
36+
@protected_resource(validator_cls=MyValidator)
37+
def my_view(request):
38+
# You have to leverage your own logic to get here...
39+
# ...
40+
pass
41+
42+
43+
.. function:: rw_protected_resource(scopes=None, validator_cls=OAuth2Validator, server_cls=Server)
44+
45+
Decorator to protect views by providing OAuth2 authentication and read/write scopes out of the
46+
box. GET, HEAD, OPTIONS http methods require "read" scope.
47+
Otherwise "write" scope is required::
48+
49+
from oauth2_provider.decorators import protected_resource
50+
51+
@rw_protected_resource()
52+
def my_view(request):
53+
# If this is a POST, you have to provide 'write' scope to get here...
54+
# ...
55+
pass
56+
57+
If you need, you can ask for other scopes over "read" and "write"::
58+
59+
from oauth2_provider.decorators import protected_resource
60+
61+
@rw_protected_resource(scopes=['exotic_scope'])
62+
def my_view(request):
63+
# If this is a POST, you have to provide 'exotic_scope write' scopes to get here...
64+
# ...
65+
pass

0 commit comments

Comments
 (0)