Skip to content

Commit 52a7dc6

Browse files
committed
OAuthLibCore now has internal methods to parse request headers and body
1 parent ec3688c commit 52a7dc6

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

oauth2_provider/oauth2_backends.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,39 @@ def _get_escaped_full_path(self, request):
3030

3131
return urlunparse(parsed)
3232

33-
def _extract_params(self, request):
33+
def _extract_headers(self, request):
3434
"""
35-
Extract parameters from the Django request object. Such parameters will then be passed to
36-
OAuthLib to build its own Request object
35+
Extracts headers from the Django request object
36+
:param request: The current django.http.HttpRequest object
37+
:return: a dictionary with OAuthLib needed headers
3738
"""
38-
uri = self._get_escaped_full_path(request)
39-
http_method = request.method
4039
headers = request.META.copy()
4140
if 'wsgi.input' in headers:
4241
del headers['wsgi.input']
4342
if 'wsgi.errors' in headers:
4443
del headers['wsgi.errors']
4544
if 'HTTP_AUTHORIZATION' in headers:
4645
headers['Authorization'] = headers['HTTP_AUTHORIZATION']
47-
body = urlencode(request.POST.items())
46+
47+
return headers
48+
49+
def _extract_body(self, request):
50+
"""
51+
Extracts the POST body from the Django request object
52+
:param request: The current django.http.HttpRequest object
53+
:return: provided POST parameters
54+
"""
55+
return request.POST.items()
56+
57+
def _extract_params(self, request):
58+
"""
59+
Extract parameters from the Django request object. Such parameters will then be passed to
60+
OAuthLib to build its own Request object. The body should be encoded using OAuthLib urlencoded
61+
"""
62+
uri = self._get_escaped_full_path(request)
63+
http_method = request.method
64+
headers = self._extract_headers(request)
65+
body = urlencode(self._extract_body(request))
4866
return uri, http_method, body, headers
4967

5068
def validate_authorization_request(self, request):

oauth2_provider/tests/test_oauth2_backends.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
11
from django.test import TestCase, RequestFactory
2-
2+
from django.test.utils import override_settings
33

44
from ..backends import get_oauthlib_core
55

66

7+
@override_settings(OAUTH2_BACKEND_CLASS='oauth2_provider.oauth2_backends.OAuthLibCore')
8+
class TestOAuthLibCoreBackend(TestCase):
9+
def setUp(self):
10+
self.factory = RequestFactory()
11+
self.oauthlib_core = get_oauthlib_core()
12+
13+
def test_form_urlencoded_extract_params(self):
14+
payload = "grant_type=password&username=john&password=123456"
15+
request = self.factory.post("/o/token/", payload, content_type="application/x-www-form-urlencoded")
16+
17+
uri, http_method, body, headers = self.oauthlib_core._extract_params(request)
18+
self.assertIn("grant_type=password", body)
19+
self.assertIn("username=john", body)
20+
self.assertIn("password=123456", body)
21+
22+
def test_application_json_extract_params(self):
23+
payload = json.dumps({
24+
"grant_type": "password",
25+
"username": "john",
26+
"password": "123456",
27+
})
28+
request = self.factory.post("/o/token/", payload, content_type="application/json")
29+
30+
uri, http_method, body, headers = self.oauthlib_core._extract_params(request)
31+
self.assertNotIn("grant_type=password", body)
32+
self.assertNotIn("username=john", body)
33+
self.assertNotIn("password=123456", body)
34+
35+
736
class TestOAuthLibCore(TestCase):
837
def setUp(self):
938
self.factory = RequestFactory()

0 commit comments

Comments
 (0)