Skip to content

Commit 1e184af

Browse files
Clean up and address PR feedback. Modify conditionals, throw 403 earlier in auth process
1 parent 1f83723 commit 1e184af

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

apps/dot_ext/views/authorization.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class AuthorizationView(DotAuthorizationView):
9191
# TODO: rename this so that it isn't the same as self.version (works but confusing)
9292
# this needs to be here for urls.py as_view(version) calls, but don't use it
9393
version = 0
94+
# Variable to help reduce the amount of times validate_v3_authorization_request is called
95+
validate_v3_call = True
9496
form_class = SimpleAllowForm
9597
login_url = "/mymedicare/login"
9698

@@ -149,6 +151,11 @@ def dispatch(self, request, *args, **kwargs):
149151
initially create an AuthFlowUuid object for authorization
150152
flow tracing in logs.
151153
"""
154+
path_info = self.request.__dict__.get('path_info')
155+
version = get_api_version_number_from_url(path_info)
156+
# If it is not version 3, we don't need to check anything, just return
157+
if version == Versions.V3 and self.validate_v3_call:
158+
self.validate_v3_authorization_request()
152159
# TODO: Should the client_id match a valid application here before continuing, instead of after matching to FHIR_ID?
153160
if not kwargs.get('is_subclass_approvalview', False):
154161
# Create new authorization flow trace UUID in session and AuthFlowUuid instance, if subclass is not ApprovalView
@@ -241,7 +248,11 @@ def validate_v3_authorization_request(self):
241248
try:
242249
application = get_application_model().objects.get(client_id=client_id[0])
243250
application_user = get_user_model().objects.get(id=application.user_id)
244-
if flag.id is not None and flag.is_active_for_user(application_user):
251+
252+
if flag.id is None or flag.is_active_for_user(application_user):
253+
# Update the class variable to ensure subsequent calls to dispatch don't call this function
254+
# more times than is needed
255+
self.validate_v3_call = False
245256
return
246257
else:
247258
raise AccessDeniedTokenCustomError(
@@ -292,11 +303,6 @@ def form_valid(self, form):
292303
refresh_token_delete_cnt = 0
293304

294305
try:
295-
path_info = self.request.__dict__.get('path_info')
296-
version = get_api_version_number_from_url(path_info)
297-
# If it is not version 3, we don't need to check anything, just return
298-
if version == Versions.V3:
299-
self.validate_v3_authorization_request()
300306

301307
if not scopes:
302308
# Since the create_authorization_response will re-inject scopes even when none are
@@ -456,7 +462,7 @@ def validate_v3_token_call(self, request) -> None:
456462
application = get_application_model().objects.get(id=refresh_token.application_id)
457463
application_user = get_user_model().objects.get(id=application.user_id)
458464

459-
if flag.id is not None and flag.is_active_for_user(application_user):
465+
if flag.id is None or flag.is_active_for_user(application_user):
460466
return
461467
else:
462468
raise PermissionDenied(

apps/fhir/bluebutton/permissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def has_permission(self, request, view):
122122
application_user = get_user_model().objects.get(id=application.user_id)
123123
flag = get_waffle_flag_model().get('v3_early_adopter')
124124

125-
if flag.id is not None and flag.is_active_for_user(application_user):
125+
if flag.id is None or flag.is_active_for_user(application_user):
126126
return True
127127
else:
128128
raise PermissionDenied(

apps/fhir/bluebutton/tests/test_wellknown_endpoints.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def setUp(self):
5151

5252
@skipIf((not settings.RUN_ONLINE_TESTS), 'Can\'t reach external sites.')
5353
@override_switch('v3_endpoints', active=True)
54+
@override_flag('v3_early_adopter', active=False)
5455
def test_userinfo_returns_403(self):
5556
first_access_token = self.create_token('John', 'Smith', fhir_id_v2=FHIR_ID_V2)
5657
ac = AccessToken.objects.get(token=first_access_token)

apps/wellknown/permissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def has_permission(self, request, view):
2828
application_user = get_user_model().objects.get(id=application.user_id)
2929
flag = get_waffle_flag_model().get('v3_early_adopter')
3030

31-
if flag.id is not None and flag.is_active_for_user(application_user):
31+
if flag.id is None or flag.is_active_for_user(application_user):
3232
return True
3333
else:
3434
raise PermissionDenied(

0 commit comments

Comments
 (0)