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
|
import json
from unittest.mock import patch
import pytest
from django.contrib.auth import get_user_model
from django.core.exceptions import SuspiciousOperation
from django.test import RequestFactory
from django.urls import reverse
from django.views.generic import View
from oauthlib.oauth2 import BackendApplicationServer
from oauth2_provider.models import get_access_token_model, get_application_model
from oauth2_provider.oauth2_backends import OAuthLibCore
from oauth2_provider.oauth2_validators import OAuth2Validator
from oauth2_provider.views import ProtectedResourceView
from oauth2_provider.views.mixins import OAuthLibMixin
from . import presets
from .common_testing import OAuth2ProviderTestCase as TestCase
from .utils import get_basic_auth_header
Application = get_application_model()
AccessToken = get_access_token_model()
UserModel = get_user_model()
CLEARTEXT_SECRET = "abcdefghijklmnopqrstuvwxyz1234567890"
# mocking a protected resource view
class ResourceView(ProtectedResourceView):
def get(self, request, *args, **kwargs):
return "This is a protected resource"
@pytest.mark.usefixtures("oauth2_settings")
@pytest.mark.oauth2_settings(presets.DEFAULT_SCOPES_RW)
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_client_credentials_app",
user=cls.dev_user,
client_type=Application.CLIENT_PUBLIC,
authorization_grant_type=Application.GRANT_CLIENT_CREDENTIALS,
client_secret=CLEARTEXT_SECRET,
)
class TestClientCredential(BaseTest):
def test_client_credential_access_allowed(self):
"""
Request an access token using Client Credential Flow with hashed secrets
"""
self.assertNotEqual(self.application.client_secret, CLEARTEXT_SECRET)
token_request_data = {
"grant_type": "client_credentials",
}
auth_headers = get_basic_auth_header(self.application.client_id, CLEARTEXT_SECRET)
response = self.client.post(reverse("oauth2_provider:token"), data=token_request_data, **auth_headers)
self.assertEqual(response.status_code, 200)
# secret mismatch should return a 401
auth_headers = get_basic_auth_header(self.application.client_id, "not-the-secret")
response = self.client.post(reverse("oauth2_provider:token"), data=token_request_data, **auth_headers)
self.assertEqual(response.status_code, 401)
def test_client_credential_does_not_issue_refresh_token(self):
token_request_data = {
"grant_type": "client_credentials",
}
auth_headers = get_basic_auth_header(self.application.client_id, CLEARTEXT_SECRET)
response = self.client.post(reverse("oauth2_provider:token"), data=token_request_data, **auth_headers)
self.assertEqual(response.status_code, 200)
content = json.loads(response.content.decode("utf-8"))
self.assertNotIn("refresh_token", content)
def test_client_credential_user_is_none_on_access_token(self):
token_request_data = {"grant_type": "client_credentials"}
auth_headers = get_basic_auth_header(self.application.client_id, CLEARTEXT_SECRET)
response = self.client.post(reverse("oauth2_provider:token"), data=token_request_data, **auth_headers)
self.assertEqual(response.status_code, 200)
content = json.loads(response.content.decode("utf-8"))
access_token = AccessToken.objects.get(token=content["access_token"])
self.assertIsNone(access_token.user)
class ExampleView(OAuthLibMixin, View):
server_class = BackendApplicationServer
validator_class = OAuth2Validator
oauthlib_backend_class = OAuthLibCore
def get_scopes(self):
return ["read", "write"]
class TestExtendedRequest(BaseTest):
@classmethod
def setUpClass(cls):
cls.request_factory = RequestFactory()
super().setUpClass()
def test_extended_request(self):
token_request_data = {
"grant_type": "client_credentials",
}
auth_headers = get_basic_auth_header(self.application.client_id, CLEARTEXT_SECRET)
response = self.client.post(reverse("oauth2_provider:token"), data=token_request_data, **auth_headers)
self.assertEqual(response.status_code, 200)
content = json.loads(response.content.decode("utf-8"))
access_token = content["access_token"]
# use token to access the resource
auth_headers = {
"HTTP_AUTHORIZATION": "Bearer " + access_token,
}
request = self.request_factory.get("/fake-req", **auth_headers)
request.user = "fake"
test_view = ExampleView()
self.assertIsInstance(test_view.get_server(), BackendApplicationServer)
valid, r = test_view.verify_request(request)
self.assertTrue(valid)
self.assertIsNone(r.user)
self.assertEqual(r.client, self.application)
self.assertEqual(r.scopes, ["read", "write"])
def test_raises_error_with_invalid_hex_in_query_params(self):
request = self.request_factory.get("/fake-req?auth_token=%%7A")
with pytest.raises(SuspiciousOperation):
ExampleView().verify_request(request)
@patch("oauth2_provider.views.mixins.OAuthLibMixin.get_oauthlib_core")
def test_reraises_value_errors_as_is(self, patched_core):
patched_core.return_value.verify_request.side_effect = ValueError("Generic error")
request = self.request_factory.get("/fake-req")
with pytest.raises(ValueError):
ExampleView().verify_request(request)
class TestClientResourcePasswordBased(BaseTest):
def test_client_resource_password_based(self):
"""
Request an access token using Resource Owner Password Based flow
"""
self.application.delete()
self.application = Application.objects.create(
name="test_client_credentials_app",
user=self.dev_user,
client_type=Application.CLIENT_CONFIDENTIAL,
authorization_grant_type=Application.GRANT_PASSWORD,
client_secret=CLEARTEXT_SECRET,
)
token_request_data = {"grant_type": "password", "username": "test_user", "password": "123456"}
auth_headers = get_basic_auth_header(self.application.client_id, CLEARTEXT_SECRET)
response = self.client.post(reverse("oauth2_provider:token"), data=token_request_data, **auth_headers)
self.assertEqual(response.status_code, 200)
content = json.loads(response.content.decode("utf-8"))
access_token = content["access_token"]
# use token to access the resource
auth_headers = {
"HTTP_AUTHORIZATION": "Bearer " + access_token,
}
request = self.factory.get("/fake-resource", **auth_headers)
request.user = self.test_user
view = ResourceView.as_view()
response = view(request)
self.assertEqual(response, "This is a protected resource")
|