|
10 | 10 | from django.utils import timezone |
11 | 11 |
|
12 | 12 | from ..compat import urlparse, parse_qs, urlencode, get_user_model |
13 | | -from ..models import get_application_model, Grant, AccessToken |
| 13 | +from ..models import get_application_model, Grant, AccessToken, RefreshToken |
14 | 14 | from ..settings import oauth2_settings |
15 | 15 | from ..views import ProtectedResourceView |
16 | 16 |
|
@@ -547,6 +547,37 @@ def test_refresh(self): |
547 | 547 | content = json.loads(response.content.decode("utf-8")) |
548 | 548 | self.assertTrue('invalid_grant' in content.values()) |
549 | 549 |
|
| 550 | + def test_refresh_invalidates_old_tokens(self): |
| 551 | + """ |
| 552 | + Ensure existing refresh tokens are cleaned up when issuing new ones |
| 553 | + """ |
| 554 | + self.client.login(username="test_user", password="123456") |
| 555 | + authorization_code = self.get_auth() |
| 556 | + |
| 557 | + token_request_data = { |
| 558 | + 'grant_type': 'authorization_code', |
| 559 | + 'code': authorization_code, |
| 560 | + 'redirect_uri': 'http://example.it' |
| 561 | + } |
| 562 | + auth_headers = self.get_basic_auth_header(self.application.client_id, self.application.client_secret) |
| 563 | + |
| 564 | + response = self.client.post(reverse('oauth2_provider:token'), data=token_request_data, **auth_headers) |
| 565 | + content = json.loads(response.content.decode("utf-8")) |
| 566 | + |
| 567 | + rt = content['refresh_token'] |
| 568 | + at = content['access_token'] |
| 569 | + |
| 570 | + token_request_data = { |
| 571 | + 'grant_type': 'refresh_token', |
| 572 | + 'refresh_token': rt, |
| 573 | + 'scope': content['scope'], |
| 574 | + } |
| 575 | + response = self.client.post(reverse('oauth2_provider:token'), data=token_request_data, **auth_headers) |
| 576 | + self.assertEqual(response.status_code, 200) |
| 577 | + |
| 578 | + self.assertFalse(RefreshToken.objects.filter(token=rt).exists()) |
| 579 | + self.assertFalse(AccessToken.objects.filter(token=at).exists()) |
| 580 | + |
550 | 581 | def test_refresh_no_scopes(self): |
551 | 582 | """ |
552 | 583 | Request an access token using a refresh token without passing any scope |
|
0 commit comments