|
4 | 4 | from functools import wraps |
5 | 5 | from time import strftime |
6 | 6 |
|
| 7 | +from django.conf import settings |
7 | 8 | from django.contrib.auth import get_user_model |
8 | 9 | from django.contrib.auth.views import redirect_to_login |
9 | 10 | from django.http import JsonResponse |
|
15 | 16 | from django.views.decorators.debug import sensitive_post_parameters |
16 | 17 | from apps.dot_ext.constants import TOKEN_ENDPOINT_V3_KEY |
17 | 18 | from oauth2_provider.exceptions import OAuthToolkitError |
18 | | -from oauth2_provider.views.base import app_authorized, get_access_token_model |
| 19 | +from oauth2_provider.views.base import app_authorized |
| 20 | +from oauth2_provider.models import get_refresh_token_model, get_access_token_model |
19 | 21 | from oauth2_provider.views.base import AuthorizationView as DotAuthorizationView |
20 | 22 | from oauth2_provider.views.base import TokenView as DotTokenView |
21 | 23 | from oauth2_provider.views.base import RevokeTokenView as DotRevokeTokenView |
|
30 | 32 | import html |
31 | 33 | from apps.dot_ext.scopes import CapabilitiesScopes |
32 | 34 | import apps.logging.request_logger as bb2logging |
| 35 | +from apps.versions import Versions |
33 | 36 |
|
34 | 37 | from ..signals import beneficiary_authorized_application |
35 | 38 | from ..forms import SimpleAllowForm |
|
43 | 46 | ) |
44 | 47 | from ..models import Approval |
45 | 48 | from ..utils import ( |
| 49 | + get_api_version_number_from_url, |
46 | 50 | remove_application_user_pair_tokens_data_access, |
47 | 51 | validate_app_is_active, |
48 | 52 | json_response_from_oauth2_error, |
@@ -75,8 +79,50 @@ def _wrapped(request, *args, **kwargs): |
75 | 79 | return _wrapped |
76 | 80 |
|
77 | 81 |
|
| 82 | +def check_v3_endpoint_access(view_func): |
| 83 | + @wraps(view_func) |
| 84 | + def _wrapped(request, *args, **kwargs): |
| 85 | + # 4250-TODO how do we not call this so many times? |
| 86 | + path_info = request.__dict__.get('path_info') |
| 87 | + version = get_api_version_number_from_url(path_info) |
| 88 | + if version != Versions.V3: |
| 89 | + return view_func(request, *args, **kwargs) |
| 90 | + |
| 91 | + flag = get_waffle_flag_model().get('v3_early_adopter') |
| 92 | + req_meta = request.META |
| 93 | + url_query = parse_qs(req_meta.get('QUERY_STRING')) |
| 94 | + client_id = url_query.get('client_id', [None]) |
| 95 | + try: |
| 96 | + if client_id[0]: |
| 97 | + application = get_application_model().objects.get(client_id=client_id[0]) |
| 98 | + else: |
| 99 | + url_query = parse_qs(request._body.decode('utf-8')) |
| 100 | + refresh_token_from_request = url_query.get('refresh_token', [None]) |
| 101 | + refresh_token = get_refresh_token_model().objects.get(token=refresh_token_from_request[0]) |
| 102 | + application = get_application_model().objects.get(id=refresh_token.application_id) |
| 103 | + |
| 104 | + application_user = get_user_model().objects.get(id=application.user_id) |
| 105 | + |
| 106 | + if flag.id is not None and flag.is_active_for_user(application_user): |
| 107 | + return view_func(request, *args, **kwargs) |
| 108 | + else: |
| 109 | + return JsonResponse( |
| 110 | + {'status_code': 403, 'message': settings.APPLICATION_DOES_NOT_HAVE_V3_ENABLED_YET.format(application.name)}, |
| 111 | + status=403, |
| 112 | + ) |
| 113 | + except ObjectDoesNotExist: |
| 114 | + # 4250-TODO Do we need this? |
| 115 | + return JsonResponse( |
| 116 | + {'status_code': 500, 'message': 'Error retrieving data'}, |
| 117 | + status=500, |
| 118 | + ) |
| 119 | + |
| 120 | + return _wrapped |
| 121 | + |
| 122 | + |
78 | 123 | @method_decorator(csrf_exempt, name="dispatch") |
79 | 124 | @method_decorator(require_post_state_decorator, name="dispatch") |
| 125 | +@method_decorator(check_v3_endpoint_access, name="dispatch") |
80 | 126 | class AuthorizationView(DotAuthorizationView): |
81 | 127 | """ |
82 | 128 | Override the base authorization view from dot to |
@@ -407,6 +453,7 @@ def dispatch(self, request, uuid, *args, **kwargs): |
407 | 453 |
|
408 | 454 |
|
409 | 455 | @method_decorator(csrf_exempt, name="dispatch") |
| 456 | +@method_decorator(check_v3_endpoint_access, name="dispatch") |
410 | 457 | class TokenView(DotTokenView): |
411 | 458 |
|
412 | 459 | def validate_token_endpoint_request_body(self, request): |
|
0 commit comments