Skip to content

Commit 3960084

Browse files
committed
Merge branch 'oauthlib_server_class'
2 parents 13eff3b + f4d27ca commit 3960084

File tree

6 files changed

+22
-12
lines changed

6 files changed

+22
-12
lines changed

docs/settings.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ CLIENT_SECRET_GENERATOR_LENGTH
5959
The length of the generated secrets, in characters. If this value is too low,
6060
secrets may become subject to bruteforce guessing.
6161

62+
OAUTH2_SERVER_CLASS
63+
~~~~~~~~~~~~~~~~~~~~
64+
The import string for the ``server_class`` (or ``oauthlib.oauth2.Server`` subclass)
65+
used in the ``OAuthLibMixin`` that implements OAuth2 grant types.
66+
6267
OAUTH2_VALIDATOR_CLASS
6368
~~~~~~~~~~~~~~~~~~~~~~
6469
The import string of the ``oauthlib.oauth2.RequestValidator`` subclass that

oauth2_provider/oauth2_backends.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, server=None):
1818
"""
1919
:params server: An instance of oauthlib.oauth2.Server class
2020
"""
21-
self.server = server or oauth2.Server(oauth2_settings.OAUTH2_VALIDATOR_CLASS())
21+
self.server = server or oauth2_settings.OAUTH2_SERVER_CLASS(oauth2_settings.OAUTH2_VALIDATOR_CLASS())
2222

2323
def _get_escaped_full_path(self, request):
2424
"""
@@ -191,7 +191,6 @@ def get_oauthlib_core():
191191
Utility function that take a request and returns an instance of
192192
`oauth2_provider.backends.OAuthLibCore`
193193
"""
194-
from oauthlib.oauth2 import Server
195-
196-
server = Server(oauth2_settings.OAUTH2_VALIDATOR_CLASS())
194+
validator = oauth2_settings.OAUTH2_VALIDATOR_CLASS()
195+
server = oauth2_settings.OAUTH2_SERVER_CLASS(validator)
197196
return oauth2_settings.OAUTH2_BACKEND_CLASS(server)

oauth2_provider/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'CLIENT_ID_GENERATOR_CLASS': 'oauth2_provider.generators.ClientIdGenerator',
3434
'CLIENT_SECRET_GENERATOR_CLASS': 'oauth2_provider.generators.ClientSecretGenerator',
3535
'CLIENT_SECRET_GENERATOR_LENGTH': 128,
36+
'OAUTH2_SERVER_CLASS': 'oauthlib.oauth2.Server',
3637
'OAUTH2_VALIDATOR_CLASS': 'oauth2_provider.oauth2_validators.OAuth2Validator',
3738
'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.OAuthLibCore',
3839
'SCOPES': {"read": "Reading scope", "write": "Writing scope"},
@@ -52,6 +53,7 @@
5253
MANDATORY = (
5354
'CLIENT_ID_GENERATOR_CLASS',
5455
'CLIENT_SECRET_GENERATOR_CLASS',
56+
'OAUTH2_SERVER_CLASS',
5557
'OAUTH2_VALIDATOR_CLASS',
5658
'OAUTH2_BACKEND_CLASS',
5759
'SCOPES',
@@ -62,6 +64,7 @@
6264
IMPORT_STRINGS = (
6365
'CLIENT_ID_GENERATOR_CLASS',
6466
'CLIENT_SECRET_GENERATOR_CLASS',
67+
'OAUTH2_SERVER_CLASS',
6568
'OAUTH2_VALIDATOR_CLASS',
6669
'OAUTH2_BACKEND_CLASS',
6770
)

oauth2_provider/tests/test_oauth2_backends.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22
import mock
33

44
from django.test import TestCase, RequestFactory
5+
from django.test.utils import override_settings
56

67
from ..backends import get_oauthlib_core
78
from ..oauth2_backends import OAuthLibCore, JSONOAuthLibCore
89

910

1011
class TestOAuthLibCoreBackend(TestCase):
12+
1113
def setUp(self):
1214
self.factory = RequestFactory()
1315
self.oauthlib_core = OAuthLibCore()
1416

17+
def test_swappable_serer_class(self):
18+
with mock.patch('oauth2_provider.oauth2_backends.oauth2_settings.OAUTH2_SERVER_CLASS'):
19+
oauthlib_core = OAuthLibCore()
20+
self.assertTrue(isinstance(oauthlib_core.server, mock.MagicMock))
21+
1522
def test_form_urlencoded_extract_params(self):
1623
payload = "grant_type=password&username=john&password=123456"
1724
request = self.factory.post("/o/token/", payload, content_type="application/x-www-form-urlencoded")

oauth2_provider/views/base.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from django.utils import timezone
77
from django.utils.decorators import method_decorator
88

9-
from oauthlib.oauth2 import Server
10-
119
from braces.views import LoginRequiredMixin, CsrfExemptMixin
1210

1311
from ..settings import oauth2_settings
@@ -71,7 +69,7 @@ class AuthorizationView(BaseAuthorizationView, FormView):
7169
template_name = 'oauth2_provider/authorize.html'
7270
form_class = AllowForm
7371

74-
server_class = Server
72+
server_class = oauth2_settings.OAUTH2_SERVER_CLASS
7573
validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
7674
oauthlib_backend_class = oauth2_settings.OAUTH2_BACKEND_CLASS
7775

@@ -163,7 +161,7 @@ class TokenView(CsrfExemptMixin, OAuthLibMixin, View):
163161
* Password
164162
* Client credentials
165163
"""
166-
server_class = Server
164+
server_class = oauth2_settings.OAUTH2_SERVER_CLASS
167165
validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
168166
oauthlib_backend_class = oauth2_settings.OAUTH2_BACKEND_CLASS
169167

@@ -181,7 +179,7 @@ class RevokeTokenView(CsrfExemptMixin, OAuthLibMixin, View):
181179
"""
182180
Implements an endpoint to revoke access or refresh tokens
183181
"""
184-
server_class = Server
182+
server_class = oauth2_settings.OAUTH2_SERVER_CLASS
185183
validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
186184
oauthlib_backend_class = oauth2_settings.OAUTH2_BACKEND_CLASS
187185

oauth2_provider/views/generic.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from django.views.generic import View
22

3-
from oauthlib.oauth2 import Server
4-
53
from ..settings import oauth2_settings
64
from .mixins import ProtectedResourceMixin, ScopedResourceMixin, ReadWriteScopedResourceMixin
75

@@ -10,7 +8,7 @@ class ProtectedResourceView(ProtectedResourceMixin, View):
108
"""
119
Generic view protecting resources by providing OAuth2 authentication out of the box
1210
"""
13-
server_class = Server
11+
server_class = oauth2_settings.OAUTH2_SERVER_CLASS
1412
validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
1513
oauthlib_backend_class = oauth2_settings.OAUTH2_BACKEND_CLASS
1614

0 commit comments

Comments
 (0)