Skip to content

Commit ec3688c

Browse files
committed
oauthlib_backend_class is now pluggable through oauth2_settings.OAUTH2_BACKEND_CLASS
1 parent 6bdee6d commit ec3688c

File tree

7 files changed

+31
-7
lines changed

7 files changed

+31
-7
lines changed

oauth2_provider/oauth2_backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,4 @@ def get_oauthlib_core():
144144
from oauthlib.oauth2 import Server
145145

146146
server = Server(oauth2_settings.OAUTH2_VALIDATOR_CLASS())
147-
return OAuthLibCore(server)
147+
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
@@ -34,6 +34,7 @@
3434
'CLIENT_SECRET_GENERATOR_CLASS': 'oauth2_provider.generators.ClientSecretGenerator',
3535
'CLIENT_SECRET_GENERATOR_LENGTH': 128,
3636
'OAUTH2_VALIDATOR_CLASS': 'oauth2_provider.oauth2_validators.OAuth2Validator',
37+
'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.OAuthLibCore',
3738
'SCOPES': {"read": "Reading scope", "write": "Writing scope"},
3839
'READ_SCOPE': 'read',
3940
'WRITE_SCOPE': 'write',
@@ -52,6 +53,7 @@
5253
'CLIENT_ID_GENERATOR_CLASS',
5354
'CLIENT_SECRET_GENERATOR_CLASS',
5455
'OAUTH2_VALIDATOR_CLASS',
56+
'OAUTH2_BACKEND_CLASS',
5557
'SCOPES',
5658
'ALLOWED_REDIRECT_URI_SCHEMES',
5759
)
@@ -61,6 +63,7 @@
6163
'CLIENT_ID_GENERATOR_CLASS',
6264
'CLIENT_SECRET_GENERATOR_CLASS',
6365
'OAUTH2_VALIDATOR_CLASS',
66+
'OAUTH2_BACKEND_CLASS',
6467
)
6568

6669

oauth2_provider/tests/test_client_credential.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from oauthlib.oauth2 import BackendApplicationServer
1515

1616
from ..models import get_application_model, AccessToken
17+
from ..oauth2_backends import OAuthLibCore
1718
from ..oauth2_validators import OAuth2Validator
1819
from ..settings import oauth2_settings
1920
from ..views import ProtectedResourceView
@@ -114,6 +115,7 @@ def test_extended_request(self):
114115
class TestView(OAuthLibMixin, View):
115116
server_class = BackendApplicationServer
116117
validator_class = OAuth2Validator
118+
oauthlib_backend_class = OAuthLibCore
117119

118120
def get_scopes(self):
119121
return ['read', 'write']

oauth2_provider/tests/test_mixins.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from oauthlib.oauth2 import Server
99

1010
from ..views.mixins import OAuthLibMixin, ScopedResourceMixin, ProtectedResourceMixin
11+
from ..oauth2_backends import OAuthLibCore
1112
from ..oauth2_validators import OAuth2Validator
1213

1314

@@ -18,9 +19,19 @@ def setUpClass(cls):
1819

1920

2021
class TestOAuthLibMixin(BaseTest):
22+
def test_missing_oauthlib_backend_class(self):
23+
class TestView(OAuthLibMixin, View):
24+
server_class = Server
25+
validator_class = OAuth2Validator
26+
27+
test_view = TestView()
28+
29+
self.assertRaises(ImproperlyConfigured, test_view.get_oauthlib_backend_class)
30+
2131
def test_missing_server_class(self):
2232
class TestView(OAuthLibMixin, View):
2333
validator_class = OAuth2Validator
34+
oauthlib_backend_class = OAuthLibCore
2435

2536
test_view = TestView()
2637

@@ -29,6 +40,7 @@ class TestView(OAuthLibMixin, View):
2940
def test_missing_validator_class(self):
3041
class TestView(OAuthLibMixin, View):
3142
server_class = Server
43+
oauthlib_backend_class = OAuthLibCore
3244

3345
test_view = TestView()
3446

@@ -38,6 +50,7 @@ def test_correct_server(self):
3850
class TestView(OAuthLibMixin, View):
3951
server_class = Server
4052
validator_class = OAuth2Validator
53+
oauthlib_backend_class = OAuthLibCore
4154

4255
request = self.request_factory.get("/fake-req")
4356
request.user = "fake"

oauth2_provider/views/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class AuthorizationView(BaseAuthorizationView, FormView):
7373

7474
server_class = Server
7575
validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
76+
oauthlib_backend_class = oauth2_settings.OAUTH2_BACKEND_CLASS
7677

7778
skip_authorization_completely = False
7879

@@ -164,6 +165,7 @@ class TokenView(CsrfExemptMixin, OAuthLibMixin, View):
164165
"""
165166
server_class = Server
166167
validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
168+
oauthlib_backend_class = oauth2_settings.OAUTH2_BACKEND_CLASS
167169

168170
@method_decorator(sensitive_post_parameters('password'))
169171
def post(self, request, *args, **kwargs):
@@ -181,6 +183,7 @@ class RevokeTokenView(CsrfExemptMixin, OAuthLibMixin, View):
181183
"""
182184
server_class = Server
183185
validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
186+
oauthlib_backend_class = oauth2_settings.OAUTH2_BACKEND_CLASS
184187

185188
def post(self, request, *args, **kwargs):
186189
url, headers, body, status = self.create_revocation_response(request)

oauth2_provider/views/generic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ProtectedResourceView(ProtectedResourceMixin, View):
1212
"""
1313
server_class = Server
1414
validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
15+
oauthlib_backend_class = oauth2_settings.OAUTH2_BACKEND_CLASS
1516

1617

1718
class ScopedProtectedResourceView(ScopedResourceMixin, ProtectedResourceView):

oauth2_provider/views/mixins.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from django.core.exceptions import ImproperlyConfigured
66
from django.http import HttpResponseForbidden
77

8-
from ..oauth2_backends import OAuthLibCore
98
from ..exceptions import FatalClientError
109
from ..settings import oauth2_settings
1110

@@ -30,6 +29,7 @@ class OAuthLibMixin(object):
3029
"""
3130
server_class = None
3231
validator_class = None
32+
oauthlib_backend_class = None
3333

3434
@classmethod
3535
def get_server_class(cls):
@@ -58,12 +58,14 @@ def get_validator_class(cls):
5858
@classmethod
5959
def get_oauthlib_backend_class(cls):
6060
"""
61-
Return the OAuthLibCore implementation class to use, silently
62-
defaults to OAuthLibCore class from oauth2_provider package
61+
Return the OAuthLibCore implementation class to use
6362
"""
64-
if not hasattr(cls, 'oauthlib_backend_class'):
65-
return OAuthLibCore
66-
return cls.oauthlib_backend_class
63+
if cls.oauthlib_backend_class is None:
64+
raise ImproperlyConfigured(
65+
"OAuthLibMixin requires either a definition of 'oauthlib_backend_class'"
66+
" or an implementation of 'get_oauthlib_backend_class()'")
67+
else:
68+
return cls.oauthlib_backend_class
6769

6870
@classmethod
6971
def get_server(cls):

0 commit comments

Comments
 (0)