Skip to content

Commit cbe5d0e

Browse files
committed
Merge branch 'fix-urlencode-clientid-secret' of github.com:lucacorti/django-oauth-toolkit into luca-fix
2 parents cb0abee + c1e2e14 commit cbe5d0e

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

oauth2_provider/compat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
from urllib.parse import urlparse, parse_qs, urlunparse
1616

1717
try:
18-
from urllib import urlencode
18+
from urllib import urlencode, unquote_plus
1919
except ImportError:
20-
from urllib.parse import urlencode
20+
from urllib.parse import urlencode, unquote_plus
2121

2222
# Django 1.5 add support for custom auth user model
2323
if django.VERSION >= (1, 5):

oauth2_provider/oauth2_validators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from django.contrib.auth import authenticate
99
from oauthlib.oauth2 import RequestValidator
1010

11+
from .compat import unquote_plus
1112
from .models import Grant, AccessToken, RefreshToken, get_application_model
1213
from .settings import oauth2_settings
1314

@@ -44,7 +45,7 @@ def _authenticate_basic_auth(self, request):
4445
client_id, client_secret = auth_string_decoded.split(':', 1)
4546

4647
try:
47-
request.client = Application.objects.get(client_id=client_id, client_secret=client_secret)
48+
request.client = Application.objects.get(client_id=unquote_plus(client_id), client_secret=unquote_plus(client_secret))
4849
return True
4950

5051
except Application.DoesNotExist:

oauth2_provider/tests/test_client_credential.py

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

0 commit comments

Comments
 (0)