Skip to content

Commit c1015fa

Browse files
committed
verify the request validity at the very begin of the request processing
1 parent 91efb22 commit c1015fa

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

flask_rest_jsonapi/api.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import inspect
88
from functools import wraps
99

10+
from flask import request, abort
11+
1012
from flask_rest_jsonapi.resource import ResourceList, ResourceRelationship
1113
from flask_rest_jsonapi.decorators import jsonapi_exception_formatter
1214

@@ -93,13 +95,24 @@ def oauth_manager(self, oauth_manager):
9395
9496
:param oauth_manager: the oauth manager
9597
"""
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
103116

104117
def scope_setter(self, func):
105118
"""Plug oauth scope setter function to the API

0 commit comments

Comments
 (0)