Skip to content

Commit 6ecb4d7

Browse files
committed
Fix revoke and tests
1 parent 1f5aeb6 commit 6ecb4d7

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

apps/dot_ext/tests/demographic_scopes_test_cases.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"request_scopes": APPLICATION_SCOPES_FULL,
6969
# Result:
7070
"result_form_is_valid": True,
71-
"result_token_scopes_granted": APPLICATION_SCOPES_FULL,
71+
"result_token_scopes_granted": APPLICATION_SCOPES_NON_DEMOGRAPHIC,
7272
},
7373
"test 2: share_demographic_scopes = False": {
7474
# Request:
@@ -181,7 +181,7 @@
181181
"request_scopes": APPLICATION_SCOPES_FULL,
182182
# Result:
183183
"result_has_error": False,
184-
"result_token_scopes_granted": APPLICATION_SCOPES_NON_DEMOGRAPHIC,
184+
"result_token_scopes_granted": APPLICATION_SCOPES_FULL,
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_NON_DEMOGRAPHIC,
224+
"result_token_scopes_granted": APPLICATION_SCOPES_FULL,
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_A,
317+
"result_token_scopes_granted": SCOPES_JUST_PATIENT_AND_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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def testing_post_with_share_demographic_scopes(self):
202202
# Loop through test cases in dictionary
203203
cases = VIEW_OAUTH2_SCOPES_TEST_CASES
204204
for case in cases:
205+
print(case)
205206
# Setup request parameters for test case
206207
request_bene_share_demographic_scopes = cases[case][
207208
"request_bene_share_demographic_scopes"

apps/dot_ext/views/authorization.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from oauth2_provider.models import get_application_model
2323
from oauthlib.oauth2.rfc6749.errors import InvalidClientError, InvalidGrantError
2424
from urllib.parse import urlparse, parse_qs
25-
25+
import html
2626
from apps.dot_ext.scopes import CapabilitiesScopes
2727
import apps.logging.request_logger as bb2logging
2828

@@ -120,12 +120,12 @@ def sensitive_info_check(self, request):
120120
def get_template_names(self):
121121
flag = get_waffle_flag_model().get("limit_data_access")
122122
if waffle.switch_is_active('require-scopes'):
123-
if flag.rollout or (flag.id is not None and flag.is_active_for_user(self.application.user)):
123+
if flag.rollout or (flag.id is not None and self.application and flag.is_active_for_user(self.application.user)):
124124
return ["design_system/new_authorize_v2.html"]
125125
else:
126126
return ["design_system/authorize_v2.html"]
127127
else:
128-
if flag.rollout or (flag.id is not None and flag.is_active_for_user(self.user)):
128+
if flag.rollout or (flag.id is not None and self.user and flag.is_active_for_user(self.user)):
129129
return ["design_system/new_authorize_v2.html"]
130130
else:
131131
return ["design_system/authorize.html"]
@@ -179,7 +179,7 @@ def form_valid(self, form):
179179
refresh_token_delete_cnt = 0
180180

181181
if not scopes:
182-
return self.error_response("No scopes defined", application)
182+
return self.error_response("No scopes", application)
183183
try:
184184
uri, headers, body, status = self.create_authorization_response(
185185
request=self.request, scopes=scopes, credentials=credentials, allow=allow
@@ -356,6 +356,40 @@ def post(self, request, *args, **kwargs):
356356
return super().post(request, args, kwargs)
357357

358358

359+
@method_decorator(csrf_exempt, name="dispatch")
360+
class RevokeView(DotRevokeTokenView):
361+
362+
@method_decorator(sensitive_post_parameters("password"))
363+
def post(self, request, *args, **kwargs):
364+
at_model = get_access_token_model()
365+
try:
366+
app = validate_app_is_active(request)
367+
except (InvalidClientError, InvalidGrantError) as error:
368+
return json_response_from_oauth2_error(error)
369+
370+
tkn = request.POST.get('token')
371+
if tkn is not None:
372+
escaped_tkn = html.escape(tkn)
373+
else:
374+
escaped_tkn = ""
375+
376+
try:
377+
token = at_model.objects.get(token=tkn)
378+
except at_model.DoesNotExist:
379+
log.debug(f"Token {escaped_tkn} was not found.")
380+
381+
try:
382+
dag = DataAccessGrant.objects.get(
383+
beneficiary=token.user,
384+
application=app
385+
)
386+
dag.delete()
387+
except Exception:
388+
log.debug(f"DAG lookup failed for token {escaped_tkn}.")
389+
390+
return super().post(request, args, kwargs)
391+
392+
359393
@method_decorator(csrf_exempt, name="dispatch")
360394
class IntrospectTokenView(DotIntrospectTokenView):
361395

0 commit comments

Comments
 (0)