Skip to content

Commit 314ab24

Browse files
Standardize error responses for token/auth flows - missing token error popped up
1 parent 73e0cb5 commit 314ab24

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

apps/dot_ext/views/authorization.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from django.conf import settings
88
from django.contrib.auth import get_user_model
99
from django.contrib.auth.views import redirect_to_login
10-
from django.http import HttpResponseForbidden, JsonResponse
10+
from django.http import JsonResponse
1111
from django.http.response import HttpResponse, HttpResponseBadRequest
1212
from django.template.response import TemplateResponse
13-
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
13+
from django.core.exceptions import ObjectDoesNotExist
1414
from django.utils.decorators import method_decorator
1515
from django.views.decorators.csrf import csrf_exempt
1616
from django.views.decorators.debug import sensitive_post_parameters
@@ -157,7 +157,10 @@ def dispatch(self, request, *args, **kwargs):
157157
try:
158158
self.validate_v3_authorization_request()
159159
except AccessDeniedTokenCustomError as e:
160-
return HttpResponseForbidden(e)
160+
return JsonResponse(
161+
{'status_code': 403, 'message': str(e)},
162+
status=403,
163+
)
161164

162165
# TODO: Should the client_id match a valid application here before continuing, instead of after matching to FHIR_ID?
163166
if not kwargs.get('is_subclass_approvalview', False):
@@ -465,13 +468,12 @@ def validate_v3_token_call(self, request) -> None:
465468
if flag.id is None or flag.is_active_for_user(application_user):
466469
return
467470
else:
468-
raise PermissionDenied(
469-
settings.APPLICATION_DOES_NOT_HAVE_V3_ENABLED_YET.format(application.name)
471+
raise AccessDeniedTokenCustomError(
472+
description=settings.APPLICATION_DOES_NOT_HAVE_V3_ENABLED_YET.format(application.name)
470473
)
471474
except ObjectDoesNotExist:
472-
return JsonResponse(
473-
{'status_code': 403, 'message': 'Unable to verify permission.'},
474-
status=403,
475+
raise AccessDeniedTokenCustomError(
476+
description='Unable to verify permission.'
475477
)
476478

477479
@method_decorator(sensitive_post_parameters("password"))
@@ -486,12 +488,9 @@ def post(self, request, *args, **kwargs):
486488
app = validate_app_is_active(request)
487489
except (InvalidClientError, InvalidGrantError, InvalidRequestError) as error:
488490
return json_response_from_oauth2_error(error)
489-
except PermissionDenied:
490-
log.exception('Permission denied during token endpoint processing.')
491-
# This error will not match other errors thrown by this waffle_flag as Github raised
492-
# a security concern about it, but only here.
491+
except AccessDeniedTokenCustomError as e:
493492
return JsonResponse(
494-
{'status_code': 403, 'message': 'You do not have permission to perform this action.'},
493+
{'status_code': 403, 'message': str(e)},
495494
status=403,
496495
)
497496

0 commit comments

Comments
 (0)