Skip to content

Commit f245e8e

Browse files
author
Luca Corti
committed
Implement Client ID / Client Secrect urlencoding with test
1 parent 0e71410 commit f245e8e

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

oauth2_provider/oauth2_validators.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from __future__ import unicode_literals
22

33
import base64
4+
try:
5+
import urllib.parse as urllib
6+
except ImportError:
7+
import urllib
48
import logging
59
from datetime import timedelta
610

@@ -44,7 +48,7 @@ def _authenticate_basic_auth(self, request):
4448
client_id, client_secret = auth_string_decoded.split(':', 1)
4549

4650
try:
47-
request.client = Application.objects.get(client_id=client_id, client_secret=client_secret)
51+
request.client = Application.objects.get(client_id=urllib.unquote_plus(client_id), client_secret=urllib.unquote_plus(client_secret))
4852
return True
4953

5054
except Application.DoesNotExist:

oauth2_provider/tests/test_client_credential.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from __future__ import unicode_literals
22

33
import json
4+
import base64
5+
try:
6+
import urllib.parse as urllib
7+
except ImportError:
8+
import urllib
49

510
from django.core.urlresolvers import reverse
611
from django.test import TestCase, RequestFactory
@@ -58,7 +63,6 @@ def test_client_credential_access_allowed(self):
5863
'grant_type': 'client_credentials',
5964
}
6065
auth_headers = self.get_basic_auth_header(self.application.client_id, self.application.client_secret)
61-
6266
response = self.client.post(reverse('oauth2_provider:token'), data=token_request_data, **auth_headers)
6367
self.assertEqual(response.status_code, 200)
6468

@@ -116,3 +120,44 @@ def get_scopes(self):
116120
self.assertEqual(r.user, self.dev_user)
117121
self.assertEqual(r.client, self.application)
118122
self.assertEqual(r.scopes, ['read', 'write'])
123+
124+
125+
class TestClientResourcePasswordBased(BaseTest):
126+
def test_client_resource_password_based(self):
127+
"""
128+
Request an access token using Resource Owner Password Based flow
129+
"""
130+
131+
self.application.delete()
132+
self.application = Application(
133+
name="test_client_credentials_app",
134+
user=self.dev_user,
135+
client_type=Application.CLIENT_CONFIDENTIAL,
136+
authorization_grant_type=Application.GRANT_PASSWORD,
137+
)
138+
self.application.save()
139+
140+
token_request_data = {
141+
'grant_type': 'password',
142+
'username': 'test_user',
143+
'password': '123456'
144+
}
145+
auth_headers = self.get_basic_auth_header(urllib.quote_plus(self.application.client_id), urllib.quote_plus(self.application.client_secret))
146+
response = self.client.post(reverse('oauth2_provider:token'), data=token_request_data, **auth_headers)
147+
self.assertEqual(response.status_code, 200)
148+
149+
content = json.loads(response.content.decode("utf-8"))
150+
access_token = content['access_token']
151+
152+
# use token to access the resource
153+
auth_headers = {
154+
'HTTP_AUTHORIZATION': 'Bearer ' + access_token,
155+
}
156+
request = self.factory.get("/fake-resource", **auth_headers)
157+
request.user = self.test_user
158+
159+
view = ResourceView.as_view()
160+
response = view(request)
161+
self.assertEqual(response, "This is a protected resource")
162+
163+

0 commit comments

Comments
 (0)