You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Flask-REST-JSONAPI support OAuth via `Flask-OAuthlib <https://github.com/lepture/flask-oauthlib>`_
9
+
10
+
Example:
11
+
12
+
.. code-block:: python
13
+
14
+
from flask import Flask
15
+
from flask_rest_jsonapi import Api
16
+
from flask_oauthlib.provider import OAuth2Provider
17
+
18
+
app = Flask(__name__)
19
+
oauth2 = OAuth2Provider()
20
+
21
+
api = Api()
22
+
api.init_app(app)
23
+
api.oauth_manager(oauth2)
24
+
25
+
26
+
In this example Flask-REST-JSONAPI will protect all your resource methods with this decorator ::
27
+
28
+
oauth2.require_oauth(<scope>)
29
+
30
+
The pattern of the scope is like that ::
31
+
32
+
<action>_<resource_type>
33
+
34
+
Where action is:
35
+
36
+
* list: for the get method of a ResourceList
37
+
* create: for the post method of a ResourceList
38
+
* get: for the get method of a ResourceDetail
39
+
* update: for the patch method of a ResourceDetail
40
+
* delete: for the delete method of a ResourceDetail
41
+
42
+
Example ::
43
+
44
+
list_person
45
+
46
+
If you want to customize the scope you can provide a function that computes your custom scope. The function have to looks like that:
47
+
48
+
.. code-block:: python
49
+
50
+
def get_scope(resource, method):
51
+
"""Compute the name of the scope for oauth
52
+
53
+
:param Resource resource: the resource manager
54
+
:param str method: an http method
55
+
:return str: the name of the scope
56
+
"""
57
+
return 'custom_scope'
58
+
59
+
Usage example:
60
+
61
+
.. code-block:: python
62
+
63
+
from flask import Flask
64
+
from flask_rest_jsonapi import Api
65
+
from flask_oauthlib.provider import OAuth2Provider
66
+
67
+
app = Flask(__name__)
68
+
oauth2 = OAuth2Provider()
69
+
70
+
api = Api()
71
+
api.init_app(app)
72
+
api.oauth_manager(oauth2)
73
+
api.scope_setter(get_scope)
74
+
75
+
.. note::
76
+
77
+
You can name the custom scope computation method as you want but you have to set the 2 required parameters: resource and method like in this previous example.
78
+
79
+
If you want to disable OAuth or make custom methods protection for a resource you can add this option to the resource manager.
Flask-REST-JSONAPI use a decorator to check permission for each method named has_permission. You can provide args and kwargs to this decorators so you can retrieve this args and kwargs in the permission_manager. The default usage of the permission system does not provides any args or kwargs to the decorator.
43
+
44
+
If permission is denied I recommand to raise exception like that:
45
+
46
+
.. code-block:: python
47
+
48
+
raise JsonApiException(<error_source>,
49
+
<error_details>,
50
+
title='Permission denied',
51
+
status='403')
52
+
53
+
You can disable the permission system or make custom permission checking management of a resource like that:
If you want to use both permission system and oauth support to retrieve information like user from oauth (request.oauth.user) in the permission system you have to initialize permission system before to initialize oauth support because of decorators cascading.
70
+
71
+
Example:
72
+
73
+
.. code-block:: python
74
+
75
+
from flask import Flask
76
+
from flask_rest_jsonapi import Api
77
+
from flask_oauthlib.provider import OAuth2Provider
78
+
from your_project.permission import permission_manager
79
+
80
+
app = Flask(__name__)
81
+
oauth2 = OAuth2Provider()
82
+
83
+
api = Api()
84
+
api.init_app(app)
85
+
api.permission_manager(permission_manager) # initialize permission system first
86
+
api.oauth_manager(oauth2) # initialize oauth support second
0 commit comments