Skip to content

Commit ca9b7a2

Browse files
committed
Long term fix for scope creep
1 parent cb83285 commit ca9b7a2

File tree

5 files changed

+23
-25
lines changed

5 files changed

+23
-25
lines changed

apps/capabilities/permissions.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,15 @@ def has_permission(self, request, view):
3737
slug__in=token_scopes
3838
).values_list('protected_resources', flat=True).all())
3939

40-
# this is a shorterm fix to reject all tokens that do not have either
41-
# patient/coverage.read or patient/ExplanationOfBenefit.read
42-
if ("patient/Coverage.read" in token_scopes) or ("patient/ExplanationOfBenefit.read" in token_scopes):
43-
for scope in scopes:
44-
for method, path in json.loads(scope):
45-
if method != request.method:
46-
continue
47-
if path == request.path:
48-
return True
49-
if re.fullmatch(path, request.path) is not None:
50-
return True
51-
return False
52-
else:
53-
return False
40+
for scope in scopes:
41+
for method, path in json.loads(scope):
42+
if method != request.method:
43+
continue
44+
if path == request.path:
45+
return True
46+
if re.fullmatch(path, request.path) is not None:
47+
return True
48+
return False
5449
else:
5550
# BB2-237: Replaces ASSERT with exception. We should never reach here.
5651
mesg = ("TokenHasScope requires the `oauth2_provider.rest_framework.OAuth2Authentication`"

apps/capabilities/tests.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import unittest
32

43
from django.contrib.auth.models import Group
54
from django.test import TestCase
@@ -41,7 +40,6 @@ def setUp(self):
4140
protected_resources=json.dumps([["POST", "/path"]]),
4241
)
4342

44-
@unittest.skip("Broke with quick fix")
4543
def test_request_is_protected(self):
4644
request = SimpleRequest("scope")
4745
request.method = "GET"

apps/dot_ext/tests/demographic_scopes_test_cases.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
"request_scopes": APPLICATION_SCOPES_FULL,
182182
# Result:
183183
"result_has_error": False,
184-
"result_token_scopes_granted": APPLICATION_SCOPES_FULL,
184+
"result_token_scopes_granted": APPLICATION_SCOPES_NON_DEMOGRAPHIC,
185185
"result_access_token_count": 1,
186186
"result_refresh_token_count": 1,
187187
"result_archived_token_count": 0,
@@ -221,7 +221,7 @@
221221
"request_scopes": APPLICATION_SCOPES_FULL,
222222
# Result:
223223
"result_has_error": False,
224-
"result_token_scopes_granted": APPLICATION_SCOPES_FULL,
224+
"result_token_scopes_granted": APPLICATION_SCOPES_NON_DEMOGRAPHIC,
225225
"result_access_token_count": 3,
226226
"result_refresh_token_count": 3,
227227
"result_archived_token_count": 1,
@@ -314,7 +314,7 @@
314314
"request_scopes": SCOPES_JUST_PATIENT_AND_A,
315315
# Result:
316316
"result_has_error": False,
317-
"result_token_scopes_granted": SCOPES_JUST_PATIENT_AND_A,
317+
"result_token_scopes_granted": SCOPES_JUST_A,
318318
"result_access_token_count": 3,
319319
"result_refresh_token_count": 3,
320320
"result_archived_token_count": 8,

apps/dot_ext/tests/test_views.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22
import base64
3-
import unittest
43
from datetime import date, timedelta
54

65
from django.conf import settings
@@ -163,18 +162,15 @@ def test_post_with_restricted_scopes_issues_token_with_same_scopes(self):
163162
# and here we test that only the capability-a scope has been issued
164163
self.assertEqual(content["scope"], "capability-a")
165164

166-
@unittest.skip("Broke with quick fix")
167165
def test_post_with_share_demographic_scopes(self):
168166
# Test with-out new_auth switch
169167
self.testing_post_with_share_demographic_scopes()
170168

171-
@unittest.skip("Broke with quick fix")
172169
@override_switch("new_auth", active=True)
173170
def test_post_with_share_demographic_scopes_new_auth_switch(self):
174171
# Test with new_auth switch.
175172
self.testing_post_with_share_demographic_scopes()
176173

177-
@unittest.skip("Broke with quick fix")
178174
@override_switch("require-scopes", active=True)
179175
def testing_post_with_share_demographic_scopes(self):
180176
"""
@@ -206,6 +202,7 @@ def testing_post_with_share_demographic_scopes(self):
206202
# Loop through test cases in dictionary
207203
cases = VIEW_OAUTH2_SCOPES_TEST_CASES
208204
for case in cases:
205+
print(case)
209206
# Setup request parameters for test case
210207
request_bene_share_demographic_scopes = cases[case][
211208
"request_bene_share_demographic_scopes"

apps/dot_ext/views/authorization.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import waffle
77
from waffle import get_waffle_flag_model
88

9+
from django.conf import settings
910
from django.http.response import HttpResponse, HttpResponseBadRequest
1011
from django.template.response import TemplateResponse
1112
from django.utils.decorators import method_decorator
@@ -170,8 +171,15 @@ def form_valid(self, form):
170171
application_available_scopes = CapabilitiesScopes().get_available_scopes(application=application)
171172

172173
# Set scopes to those available to application and beneficiary demographic info choices
173-
scopes = ' '.join([s for s in scopes.split(" ")
174-
if s in application_available_scopes])
174+
if share_demographic_scopes:
175+
scopes = ' '.join(
176+
[s for s in scopes.split(" ") if s in application_available_scopes]
177+
)
178+
else:
179+
scopes = ' '.join(
180+
[s for s in scopes.split(" ")
181+
if s in application_available_scopes and s not in settings.BENE_PERSONAL_INFO_SCOPES]
182+
)
175183

176184
# Init deleted counts
177185
data_access_grant_delete_cnt = 0

0 commit comments

Comments
 (0)