Skip to content

Commit a5906e3

Browse files
committed
Conform to Oauth expectations better
1 parent 56a972b commit a5906e3

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

apps/dot_ext/tests/test_authorization.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -621,17 +621,17 @@ def test_revoke_endpoint(self):
621621
self.assertEqual(response.status_code, 200)
622622
# extract token and use it to make a revoke request
623623
tkn = response.json()['access_token']
624-
revoke_request_data = {
625-
'token': tkn,
626-
'client_id': application.client_id,
627-
'client_secret': application.client_secret_plain,
628-
}
624+
revoke_request_data = f"token={tkn}&client_id={application.client_id}&client_secret={application.client_secret_plain}"
625+
content_type = "application/x-www-form-urlencoded"
629626
c = Client()
630-
rev_response = c.post('/v1/o/revoke/', data=revoke_request_data)
627+
rev_response = c.post('/v1/o/revoke/', data=revoke_request_data, content_type=content_type)
631628
self.assertEqual(rev_response.status_code, 200)
632629
# check DAG deletion
633630
dags_count = DataAccessGrant.objects.count()
634631
self.assertEqual(dags_count, 0)
632+
# check token deletion
633+
tkn_count = AccessToken.objects.filter(token=tkn).count()
634+
self.assertEqual(tkn_count, 0)
635635

636636
def test_refresh_with_revoked_token(self):
637637
redirect_uri = 'http://localhost'

apps/dot_ext/views/authorization.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
)
2222
from oauth2_provider.models import get_application_model
2323
from oauthlib.oauth2.rfc6749.errors import InvalidClientError, InvalidGrantError
24-
from rest_framework import status
2524
from urllib.parse import urlparse, parse_qs
2625
import html
2726
from apps.dot_ext.scopes import CapabilitiesScopes
@@ -366,29 +365,27 @@ def post(self, request, *args, **kwargs):
366365
except (InvalidClientError, InvalidGrantError) as error:
367366
return json_response_from_oauth2_error(error)
368367

369-
try:
370-
tkn = json.loads(request.body.decode("UTF-8")).get("token")
371-
except Exception:
372-
tkn = request.POST.get("token")
373-
374-
escaped_tkn = html.escape(tkn)
368+
tkn = request.POST.get('token')
369+
if tkn is not None:
370+
escaped_tkn = html.escape(tkn)
371+
else:
372+
escaped_tkn = ""
375373

376374
try:
377375
token = at_model.objects.get(token=tkn)
378376
except at_model.DoesNotExist:
379-
return HttpResponse(f"Token {escaped_tkn} was Not Found. Please check the value and try again.",
380-
status=status.HTTP_404_NOT_FOUND)
377+
log.debug(f"Token {escaped_tkn} was not found.")
381378

382379
try:
383380
dag = DataAccessGrant.objects.get(
384381
beneficiary=token.user,
385382
application=app
386383
)
387384
dag.delete()
388-
except DataAccessGrant.DoesNotExist:
389-
log.debug(f"Token deleted, but DAG lookup failed for token {escaped_tkn}.")
385+
except Exception:
386+
log.debug(f"DAG lookup failed for token {escaped_tkn}.")
390387

391-
return HttpResponse(content="OK", status=200)
388+
return super().post(request, args, kwargs)
392389

393390

394391
@method_decorator(csrf_exempt, name="dispatch")

0 commit comments

Comments
 (0)