1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
import datetime
from django.contrib.auth import get_user_model
from django.test import RequestFactory
from django.urls import reverse
from django.utils import timezone
from oauth2_provider.models import get_access_token_model, get_application_model, get_refresh_token_model
from .common_testing import OAuth2ProviderTestCase as TestCase
Application = get_application_model()
AccessToken = get_access_token_model()
RefreshToken = get_refresh_token_model()
UserModel = get_user_model()
CLEARTEXT_SECRET = "1234567890abcdefghijklmnopqrstuvwxyz"
class BaseTest(TestCase):
factory = RequestFactory()
@classmethod
def setUpTestData(cls):
cls.test_user = UserModel.objects.create_user("test_user", "test@example.com", "123456")
cls.dev_user = UserModel.objects.create_user("dev_user", "dev@example.com", "123456")
cls.application = Application.objects.create(
name="Test Application",
redirect_uris="http://localhost http://example.com http://example.org",
user=cls.dev_user,
client_type=Application.CLIENT_CONFIDENTIAL,
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
client_secret=CLEARTEXT_SECRET,
)
class TestRevocationView(BaseTest):
def test_revoke_access_token(self):
tok = AccessToken.objects.create(
user=self.test_user,
token="1234567890",
application=self.application,
expires=timezone.now() + datetime.timedelta(days=1),
scope="read write",
)
data = {
"client_id": self.application.client_id,
"client_secret": CLEARTEXT_SECRET,
"token": tok.token,
}
url = reverse("oauth2_provider:revoke-token")
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b"")
self.assertFalse(AccessToken.objects.filter(pk=tok.pk).exists())
def test_revoke_access_token_public(self):
public_app = Application(
name="Test Application",
redirect_uris="http://localhost http://example.com http://example.org",
user=self.dev_user,
client_type=Application.CLIENT_PUBLIC,
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
)
public_app.save()
tok = AccessToken.objects.create(
user=self.test_user,
token="1234567890",
application=public_app,
expires=timezone.now() + datetime.timedelta(days=1),
scope="read write",
)
data = {
"client_id": public_app.client_id,
"token": tok.token,
}
url = reverse("oauth2_provider:revoke-token")
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 200)
def test_revoke_access_token_with_hint(self):
tok = AccessToken.objects.create(
user=self.test_user,
token="1234567890",
application=self.application,
expires=timezone.now() + datetime.timedelta(days=1),
scope="read write",
)
data = {
"client_id": self.application.client_id,
"client_secret": CLEARTEXT_SECRET,
"token": tok.token,
"token_type_hint": "access_token",
}
url = reverse("oauth2_provider:revoke-token")
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 200)
self.assertFalse(AccessToken.objects.filter(pk=tok.pk).exists())
def test_revoke_access_token_with_invalid_hint(self):
tok = AccessToken.objects.create(
user=self.test_user,
token="1234567890",
application=self.application,
expires=timezone.now() + datetime.timedelta(days=1),
scope="read write",
)
# invalid hint should have no effect
data = {
"client_id": self.application.client_id,
"client_secret": CLEARTEXT_SECRET,
"token": tok.token,
"token_type_hint": "bad_hint",
}
url = reverse("oauth2_provider:revoke-token")
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 200)
self.assertFalse(AccessToken.objects.filter(pk=tok.pk).exists())
def test_revoke_refresh_token(self):
tok = AccessToken.objects.create(
user=self.test_user,
token="1234567890",
application=self.application,
expires=timezone.now() + datetime.timedelta(days=1),
scope="read write",
)
rtok = RefreshToken.objects.create(
user=self.test_user, token="999999999", application=self.application, access_token=tok
)
data = {
"client_id": self.application.client_id,
"client_secret": CLEARTEXT_SECRET,
"token": rtok.token,
}
url = reverse("oauth2_provider:revoke-token")
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 200)
refresh_token = RefreshToken.objects.filter(pk=rtok.pk).first()
self.assertIsNotNone(refresh_token.revoked)
self.assertFalse(AccessToken.objects.filter(pk=rtok.access_token.pk).exists())
def test_revoke_refresh_token_with_revoked_access_token(self):
tok = AccessToken.objects.create(
user=self.test_user,
token="1234567890",
application=self.application,
expires=timezone.now() + datetime.timedelta(days=1),
scope="read write",
)
rtok = RefreshToken.objects.create(
user=self.test_user, token="999999999", application=self.application, access_token=tok
)
for token in (tok.token, rtok.token):
data = {
"client_id": self.application.client_id,
"client_secret": CLEARTEXT_SECRET,
"token": token,
}
url = reverse("oauth2_provider:revoke-token")
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 200)
self.assertFalse(AccessToken.objects.filter(pk=tok.pk).exists())
refresh_token = RefreshToken.objects.filter(pk=rtok.pk).first()
self.assertIsNotNone(refresh_token.revoked)
def test_revoke_token_with_wrong_hint(self):
"""
From the revocation rfc, `Section 4.1.2`_ :
If the server is unable to locate the token using the given hint,
it MUST extend its search across all of its supported token types
.. _`Section 4.1.2`: http://tools.ietf.org/html/draft-ietf-oauth-revocation-11#section-4.1.2
"""
tok = AccessToken.objects.create(
user=self.test_user,
token="1234567890",
application=self.application,
expires=timezone.now() + datetime.timedelta(days=1),
scope="read write",
)
data = {
"client_id": self.application.client_id,
"client_secret": CLEARTEXT_SECRET,
"token": tok.token,
"token_type_hint": "refresh_token",
}
url = reverse("oauth2_provider:revoke-token")
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 200)
self.assertFalse(AccessToken.objects.filter(pk=tok.pk).exists())
|