Skip to content

Commit 453808d

Browse files
author
Massimiliano Pippi
committed
optimized application loading from db
1 parent 4e27048 commit 453808d

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

oauth2_provider/oauth2_validators.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ def _authenticate_basic_auth(self, request):
5454
encoding = request.encoding or 'utf-8'
5555

5656
auth_string_decoded = base64.b64decode(auth_string).decode(encoding)
57-
client_id, client_secret = auth_string_decoded.split(':', 1)
57+
client_id, client_secret = map(unquote_plus, auth_string_decoded.split(':', 1))
5858

59-
try:
60-
request.client = Application.objects.get(client_id=unquote_plus(client_id),
61-
client_secret=unquote_plus(client_secret))
62-
return True
63-
64-
except Application.DoesNotExist:
65-
log.debug("Failed basic auth: Application %s do not exists" % unquote_plus(client_id))
59+
if self._load_application(client_id, request) is None:
60+
log.debug("Failed basic auth: Application %s do not exists" % client_id)
61+
return False
62+
elif request.client.client_secret != client_secret:
63+
log.debug("Failed basic auth: wrong client secret %s" % client_secret)
6664
return False
65+
else:
66+
return True
6767

6868
def _authenticate_request_body(self, request):
6969
"""
@@ -73,20 +73,21 @@ def _authenticate_request_body(self, request):
7373
Remember that this method is NOT RECOMMENDED and SHOULD be limited to clients unable to
7474
directly utilize the HTTP Basic authentication scheme. See rfc:`2.3.1` for more details.
7575
"""
76+
#TODO: check if oauthlib has already unquoted client_id and client_secret
7677
client_id = request.client_id
7778
client_secret = request.client_secret
7879

7980
if not client_id or not client_secret:
8081
return False
8182

82-
try:
83-
request.client = Application.objects.get(client_id=client_id,
84-
client_secret=client_secret)
85-
return True
86-
87-
except Application.DoesNotExist:
88-
log.debug("Failed body authentication: Application %s do not exists" % client_id)
83+
if self._load_application(client_id, request) is None:
84+
log.debug("Failed body auth: Application %s does not exists" % client_id)
8985
return False
86+
elif request.client.client_secret != client_secret:
87+
log.debug("Failed body auth: wrong client secret %s" % client_secret)
88+
return False
89+
else:
90+
return True
9091

9192
def _load_application(self, client_id, request):
9293
"""
@@ -95,8 +96,10 @@ def _load_application(self, client_id, request):
9596
"""
9697
try:
9798
request.client = request.client or Application.objects.get(client_id=client_id)
99+
return request.client
98100
except Application.DoesNotExist:
99-
log.debug("Application %s do not exists" % client_id)
101+
log.debug("Failed body authentication: Application %s do not exists" % client_id)
102+
return None
100103

101104
def client_authentication_required(self, request, *args, **kwargs):
102105
"""
@@ -151,15 +154,10 @@ def authenticate_client_id(self, client_id, request, *args, **kwargs):
151154
proceed only if the client exists and it's not of type 'Confidential'.
152155
Also assign Application instance to request.client.
153156
"""
154-
155-
try:
156-
request.client = Application.objects.get(client_id=client_id)
157+
if self._load_application(client_id, request) is not None:
157158
log.debug("Application %s has type %s" % (client_id, request.client.client_type))
158159
return request.client.client_type != Application.CLIENT_CONFIDENTIAL
159-
160-
except Application.DoesNotExist:
161-
log.debug("Application %s do not exists" % client_id)
162-
return False
160+
return False
163161

164162
def confirm_redirect_uri(self, client_id, code, redirect_uri, client, *args, **kwargs):
165163
"""
@@ -180,8 +178,7 @@ def validate_client_id(self, client_id, request, *args, **kwargs):
180178
Ensure an Application exists with given client_id. If it exists, it's assigned to
181179
request.client.
182180
"""
183-
self._load_application(client_id, request)
184-
return request.client is not None
181+
return self._load_application(client_id, request) is not None
185182

186183
def get_default_redirect_uri(self, client_id, request, *args, **kwargs):
187184
return request.client.default_redirect_uri

oauth2_provider/tests/test_oauth2_validators.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from django.test import TestCase
2-
from django.contrib.auth import get_user_model
32

43
import mock
4+
from oauthlib.common import Request
55

66
from ..oauth2_validators import OAuth2Validator
77
from ..models import get_application_model
8+
from ..compat import get_user_model
89

910
UserModel = get_user_model()
1011
AppModel = get_application_model()
@@ -13,7 +14,8 @@
1314
class TestOAuth2Validator(TestCase):
1415
def setUp(self):
1516
self.user = UserModel.objects.create_user("user", "[email protected]", "123456")
16-
self.request = mock.MagicMock()
17+
self.request = mock.MagicMock(wraps=Request)
18+
self.request.client = None
1719
self.validator = OAuth2Validator()
1820
self.application = AppModel.objects.create(
1921
client_id='client_id', client_secret='client_secret', user=self.user,
@@ -43,6 +45,8 @@ def test_extract_basic_auth(self):
4345

4446
def test_authenticate_client_id(self):
4547
self.assertTrue(self.validator.authenticate_client_id('client_id', self.request))
48+
49+
def test_authenticate_client_id_fail(self):
4650
self.application.client_type = AppModel.CLIENT_CONFIDENTIAL
4751
self.application.save()
4852
self.assertFalse(self.validator.authenticate_client_id('client_id', self.request))

requirements/optional.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
-r base.txt
2-
djangorestframework>=2.3
2+
djangorestframework>=2.3
3+
mock

0 commit comments

Comments
 (0)