|
9 | 9 | from .settings import oauth2_settings |
10 | 10 |
|
11 | 11 |
|
12 | | -def protected_resource(view_func, scopes=None, validator_cls=OAuth2Validator, server_cls=Server): |
| 12 | +def protected_resource(scopes=None, validator_cls=OAuth2Validator, server_cls=Server): |
13 | 13 | """ |
14 | | - Decorator to protect views by providing OAuth2 authentication out of the box, optionally with scope handling. |
| 14 | + Decorator to protect views by providing OAuth2 authentication out of the box, optionally with |
| 15 | + scope handling. |
15 | 16 | """ |
16 | | - if scopes is None: |
17 | | - scopes = [] |
18 | | - |
19 | | - @wraps(view_func) |
20 | | - def _validate(request, *args, **kwargs): |
21 | | - validator = validator_cls() |
22 | | - core = OAuthLibCore(server_cls(validator)) |
23 | | - valid, oauthlib_req = core.verify_request(request, scopes=scopes) |
24 | | - if valid: |
25 | | - return view_func(request, *args, **kwargs) |
26 | | - return HttpResponseForbidden() |
27 | | - return _validate |
28 | | - |
29 | | - |
30 | | -def rw_protected_resource(view_func, scopes=None, validator_cls=OAuth2Validator, server_cls=Server): |
| 17 | + _scopes = scopes or [] |
| 18 | + |
| 19 | + def decorator(view_func): |
| 20 | + @wraps(view_func) |
| 21 | + def _validate(request, *args, **kwargs): |
| 22 | + validator = validator_cls() |
| 23 | + core = OAuthLibCore(server_cls(validator)) |
| 24 | + valid, oauthlib_req = core.verify_request(request, scopes=_scopes) |
| 25 | + if valid: |
| 26 | + return view_func(request, *args, **kwargs) |
| 27 | + return HttpResponseForbidden() |
| 28 | + return _validate |
| 29 | + return decorator |
| 30 | + |
| 31 | + |
| 32 | +def rw_protected_resource(scopes=None, validator_cls=OAuth2Validator, server_cls=Server): |
31 | 33 | """ |
32 | | - Decorator to protect views by providing OAuth2 authentication and read/write scopes out of the box. |
| 34 | + Decorator to protect views by providing OAuth2 authentication and read/write scopes out of the |
| 35 | + box. |
33 | 36 | GET, HEAD, OPTIONS http methods require "read" scope. Otherwise "write" scope is required. |
34 | 37 | """ |
35 | | - if scopes is None: |
36 | | - scopes = [] |
37 | | - |
38 | | - @wraps(view_func) |
39 | | - def _validate(request, *args, **kwargs): |
40 | | - # Check if provided scopes are acceptable |
41 | | - provided_scopes = oauth2_settings._SCOPES |
42 | | - read_write_scopes = [oauth2_settings.READ_SCOPE, oauth2_settings.WRITE_SCOPE] |
43 | | - |
44 | | - if not set(read_write_scopes).issubset(set(provided_scopes)): |
45 | | - raise ImproperlyConfigured( |
46 | | - "rw_protected_resource decorator requires following scopes {0}" |
47 | | - " to be in OAUTH2_PROVIDER['SCOPES'] list in settings".format(read_write_scopes) |
48 | | - ) |
49 | | - |
50 | | - # Check if method is safe |
51 | | - if request.method.upper() in ['GET', 'HEAD', 'OPTIONS']: |
52 | | - scopes.append(oauth2_settings.READ_SCOPE) |
53 | | - else: |
54 | | - scopes.append(oauth2_settings.WRITE_SCOPE) |
55 | | - |
56 | | - # proceed with validation |
57 | | - validator = validator_cls() |
58 | | - core = OAuthLibCore(server_cls(validator)) |
59 | | - valid, oauthlib_req = core.verify_request(request, scopes=scopes) |
60 | | - if valid: |
61 | | - return view_func(request, *args, **kwargs) |
62 | | - return HttpResponseForbidden() |
63 | | - return _validate |
| 38 | + _scopes = scopes or [] |
| 39 | + |
| 40 | + def decorator(view_func): |
| 41 | + @wraps(view_func) |
| 42 | + def _validate(request, *args, **kwargs): |
| 43 | + # Check if provided scopes are acceptable |
| 44 | + provided_scopes = oauth2_settings._SCOPES |
| 45 | + read_write_scopes = [oauth2_settings.READ_SCOPE, oauth2_settings.WRITE_SCOPE] |
| 46 | + |
| 47 | + if not set(read_write_scopes).issubset(set(provided_scopes)): |
| 48 | + raise ImproperlyConfigured( |
| 49 | + "rw_protected_resource decorator requires following scopes {0}" |
| 50 | + " to be in OAUTH2_PROVIDER['SCOPES'] list in settings".format( |
| 51 | + read_write_scopes) |
| 52 | + ) |
| 53 | + |
| 54 | + # Check if method is safe |
| 55 | + if request.method.upper() in ['GET', 'HEAD', 'OPTIONS']: |
| 56 | + _scopes.append(oauth2_settings.READ_SCOPE) |
| 57 | + else: |
| 58 | + _scopes.append(oauth2_settings.WRITE_SCOPE) |
| 59 | + |
| 60 | + # proceed with validation |
| 61 | + validator = validator_cls() |
| 62 | + core = OAuthLibCore(server_cls(validator)) |
| 63 | + valid, oauthlib_req = core.verify_request(request, scopes=_scopes) |
| 64 | + if valid: |
| 65 | + return view_func(request, *args, **kwargs) |
| 66 | + return HttpResponseForbidden() |
| 67 | + return _validate |
| 68 | + return decorator |
0 commit comments