Skip to content

Commit dea3d72

Browse files
committed
Merge pull request #161 from Geekfish/feature/revocation-searches-all-types
Token Revocation - extend search to all token types.
2 parents be267f9 + 14e7cb0 commit dea3d72

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

oauth2_provider/oauth2_validators.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from django.utils import timezone
88
from django.contrib.auth import authenticate
9+
from django.core.exceptions import ObjectDoesNotExist
910
from oauthlib.oauth2 import RequestValidator
1011

1112
from .compat import unquote_plus
@@ -304,22 +305,20 @@ def revoke_token(self, token, token_type_hint, request, *args, **kwargs):
304305
:param token_type_hint: access_token or refresh_token.
305306
:param request: The HTTP Request (oauthlib.common.Request)
306307
"""
307-
if token_type_hint not in [None, 'access_token', 'refresh_token']:
308+
if token_type_hint not in ['access_token', 'refresh_token']:
308309
token_type_hint = None
309310

310-
if token_type_hint in [None, 'access_token']:
311-
try:
312-
AccessToken.objects.get(token=token).delete()
313-
return
314-
except AccessToken.DoesNotExist:
315-
pass
311+
token_types = {
312+
'access_token': AccessToken,
313+
'refresh_token': RefreshToken,
314+
}
316315

317-
if token_type_hint in [None, 'refresh_token']:
318-
try:
319-
RefreshToken.objects.get(token=token).delete()
320-
return
321-
except RefreshToken.DoesNotExist:
322-
pass
316+
token_type = token_types.get(token_type_hint, AccessToken)
317+
try:
318+
token_type.objects.get(token=token).delete()
319+
except ObjectDoesNotExist:
320+
for other_type in [_t for _t in token_types.values() if _t != token_type]:
321+
other_type.objects.filter(token=token).delete()
323322

324323
def validate_user(self, username, password, client, request, *args, **kwargs):
325324
"""

oauth2_provider/tests/test_token_revocation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,27 @@ def test_revoke_refresh_token(self):
118118
response = self.client.post(url)
119119
self.assertEqual(response.status_code, 200)
120120
self.assertFalse(RefreshToken.objects.filter(id=rtok.id).exists())
121+
122+
def test_revoke_token_with_wrong_hint(self):
123+
"""
124+
From the revocation rfc, `Section 4.1.2`_ :
125+
126+
If the server is unable to locate the token using the given hint, it MUST extend its search across all of its supported token typeso
127+
.. _`Section 4.1.2`: http://tools.ietf.org/html/draft-ietf-oauth-revocation-11#section-4.1.2
128+
"""
129+
tok = AccessToken.objects.create(user=self.test_user, token='1234567890',
130+
application=self.application,
131+
expires=timezone.now()+datetime.timedelta(days=1),
132+
scope='read write')
133+
134+
query_string = urlencode({
135+
'client_id': self.application.client_id,
136+
'client_secret': self.application.client_secret,
137+
'token': tok.token,
138+
'token_type_hint': 'refresh_token'
139+
})
140+
url = "{url}?{qs}".format(url=reverse('oauth2_provider:revoke-token'), qs=query_string)
141+
response = self.client.post(url)
142+
self.assertEqual(response.status_code, 200)
143+
self.assertFalse(AccessToken.objects.filter(id=tok.id).exists())
144+

0 commit comments

Comments
 (0)