Skip to content

Commit ab1f7ab

Browse files
committed
added JSONOAuthLibCore to parse correctly application/json requests
1 parent 52a7dc6 commit ab1f7ab

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

oauth2_provider/oauth2_backends.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import unicode_literals
22

3+
import json
4+
35
from oauthlib import oauth2
46
from oauthlib.common import urlencode, urlencoded, quote
57

@@ -154,6 +156,24 @@ def verify_request(self, request, scopes):
154156
return valid, r
155157

156158

159+
class JSONOAuthLibCore(OAuthLibCore):
160+
"""
161+
Extends the default OAuthLibCore to parse correctly requests with application/json Content-Type
162+
"""
163+
def _extract_body(self, request):
164+
"""
165+
Extracts the JSON body from the Django request object
166+
:param request: The current django.http.HttpRequest object
167+
:return: provided POST parameters "urlencodable"
168+
"""
169+
try:
170+
body = json.loads(request.body.decode('utf-8')).items()
171+
except ValueError:
172+
body = ""
173+
174+
return body
175+
176+
157177
def get_oauthlib_core():
158178
"""
159179
Utility function that take a request and returns an instance of

oauth2_provider/tests/test_oauth2_backends.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import json
2+
13
from django.test import TestCase, RequestFactory
2-
from django.test.utils import override_settings
34

45
from ..backends import get_oauthlib_core
6+
from ..oauth2_backends import OAuthLibCore, JSONOAuthLibCore
57

68

7-
@override_settings(OAUTH2_BACKEND_CLASS='oauth2_provider.oauth2_backends.OAuthLibCore')
89
class TestOAuthLibCoreBackend(TestCase):
910
def setUp(self):
1011
self.factory = RequestFactory()
11-
self.oauthlib_core = get_oauthlib_core()
12+
self.oauthlib_core = OAuthLibCore()
1213

1314
def test_form_urlencoded_extract_params(self):
1415
payload = "grant_type=password&username=john&password=123456"
@@ -33,6 +34,25 @@ def test_application_json_extract_params(self):
3334
self.assertNotIn("password=123456", body)
3435

3536

37+
class TestJSONOAuthLibCoreBackend(TestCase):
38+
def setUp(self):
39+
self.factory = RequestFactory()
40+
self.oauthlib_core = JSONOAuthLibCore()
41+
42+
def test_application_json_extract_params(self):
43+
payload = json.dumps({
44+
"grant_type": "password",
45+
"username": "john",
46+
"password": "123456",
47+
})
48+
request = self.factory.post("/o/token/", payload, content_type="application/json")
49+
50+
uri, http_method, body, headers = self.oauthlib_core._extract_params(request)
51+
self.assertIn("grant_type=password", body)
52+
self.assertIn("username=john", body)
53+
self.assertIn("password=123456", body)
54+
55+
3656
class TestOAuthLibCore(TestCase):
3757
def setUp(self):
3858
self.factory = RequestFactory()

0 commit comments

Comments
 (0)