|
7 | 7 | import inspect |
8 | 8 | from functools import wraps |
9 | 9 |
|
| 10 | +from flask import request, abort |
| 11 | + |
10 | 12 | from flask_rest_jsonapi.resource import ResourceList, ResourceRelationship |
11 | 13 | from flask_rest_jsonapi.decorators import jsonapi_exception_formatter |
12 | 14 |
|
@@ -93,13 +95,24 @@ def oauth_manager(self, oauth_manager): |
93 | 95 |
|
94 | 96 | :param oauth_manager: the oauth manager |
95 | 97 | """ |
96 | | - for resource in self.resource_registry: |
97 | | - if getattr(resource, 'disable_oauth', None) is not True: |
98 | | - for method in getattr(resource, 'methods', ('GET', 'POST', 'PATCH', 'DELETE')): |
99 | | - scope = self.get_scope(resource, method) |
100 | | - setattr(resource, |
101 | | - method.lower(), |
102 | | - oauth_manager.require_oauth(scope)(getattr(resource, method.lower()))) |
| 98 | + @self.app.before_request |
| 99 | + def before_request(): |
| 100 | + endpoint = request.endpoint |
| 101 | + resource = self.app.view_functions[endpoint].view_class |
| 102 | + |
| 103 | + scope = self.get_scope(resource, request.method) |
| 104 | + |
| 105 | + valid, req = oauth_manager.verify_request([scope]) |
| 106 | + |
| 107 | + for func in oauth_manager._after_request_funcs: |
| 108 | + valid, req = func(valid, req) |
| 109 | + |
| 110 | + if not valid: |
| 111 | + if oauth_manager._invalid_response: |
| 112 | + return oauth_manager._invalid_response(req) |
| 113 | + return abort(401) |
| 114 | + |
| 115 | + request.oauth = req |
103 | 116 |
|
104 | 117 | def scope_setter(self, func): |
105 | 118 | """Plug oauth scope setter function to the API |
|
0 commit comments