Skip to content

Commit 31c14b4

Browse files
akanstantsinaudopry
authored andcommitted
Code and docs cleanup
1 parent 7d71f2d commit 31c14b4

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

docs/tutorial/tutorial_01.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ point your browser to http://localhost:8000/o/applications/ and add an Applicati
9191
specifies one of the verified redirection uris. For this tutorial, paste verbatim the value
9292
`https://www.getpostman.com/oauth2/callback`
9393

94-
* `Allowed origins`: Web applications use Cross-Origin Resource Sharing (CORS) to request resources from origins other
95-
than their own. You can provide list of origins of web applications that will have access to the token endpoint
94+
* `Allowed origins`: Browser-based clients use Cross-Origin Resource Sharing (CORS) to request resources from origins other
95+
than their own. You can provide list of origins that will have access to the token endpoint
9696
of :term:`Authorization Server`. This setting controls only token endpoint and it is not related
9797
with Django CORS Headers settings.
9898

oauth2_provider/oauth2_backends.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ def extract_headers(self, request):
7575
del headers["wsgi.errors"]
7676
if "HTTP_AUTHORIZATION" in headers:
7777
headers["Authorization"] = headers["HTTP_AUTHORIZATION"]
78-
# Add Access-Control-Allow-Origin header to the token endpoint response for authentication code grant, if the origin is allowed by RequestValidator.is_origin_allowed.
78+
# Add Access-Control-Allow-Origin header to the token endpoint response for authentication code grant,
79+
# if the origin is allowed by RequestValidator.is_origin_allowed.
7980
# https://github.com/oauthlib/oauthlib/pull/791
8081
if "HTTP_ORIGIN" in headers:
8182
headers["Origin"] = headers["HTTP_ORIGIN"]

oauth2_provider/oauth2_validators.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,11 @@ def get_additional_claims(self, request):
960960
return {}
961961

962962
def is_origin_allowed(self, client_id, origin, request, *args, **kwargs):
963-
if request.client is None or not request.client.client_id:
964-
return False
965-
application = Application.objects.filter(client_id=request.client.client_id).first()
966-
if application:
967-
return application.origin_allowed(origin)
968-
else:
969-
return False
963+
"""Indicate if the given origin is allowed to access the token endpoint
964+
via Cross-Origin Resource Sharing (CORS). CORS is used by browser-based
965+
clients, such as Single-Page Applications, to perform the Authorization
966+
Code Grant.
967+
968+
Verifies if request's origin is within Application's allowed origins list.
969+
"""
970+
return request.client.origin_allowed(origin)

tests/test_cors.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
# CORS is allowed for https only
2121
CLIENT_URI = "https://example.org"
2222

23+
CLIENT_URI_HTTP = "http://example.org"
24+
2325

2426
@pytest.mark.usefixtures("oauth2_settings")
2527
@pytest.mark.oauth2_settings(presets.DEFAULT_SCOPES_RW)
@@ -39,7 +41,7 @@ def setUp(self):
3941

4042
self.application = Application.objects.create(
4143
name="Test Application",
42-
redirect_uris=(CLIENT_URI),
44+
redirect_uris=CLIENT_URI,
4345
user=self.dev_user,
4446
client_type=Application.CLIENT_CONFIDENTIAL,
4547
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
@@ -85,6 +87,26 @@ def test_cors_header(self):
8587
self.assertEqual(response.status_code, 200)
8688
self.assertEqual(response["Access-Control-Allow-Origin"], CLIENT_URI)
8789

90+
def test_cors_header_no_https(self):
91+
"""
92+
Test that CORS is not allowed if origin uri does not have https:// schema
93+
"""
94+
authorization_code = self._get_authorization_code()
95+
96+
# exchange authorization code for a valid access token
97+
token_request_data = {
98+
"grant_type": "authorization_code",
99+
"code": authorization_code,
100+
"redirect_uri": CLIENT_URI,
101+
}
102+
103+
auth_headers = get_basic_auth_header(self.application.client_id, CLEARTEXT_SECRET)
104+
auth_headers["HTTP_ORIGIN"] = CLIENT_URI_HTTP
105+
response = self.client.post(reverse("oauth2_provider:token"), data=token_request_data, **auth_headers)
106+
107+
self.assertEqual(response.status_code, 200)
108+
self.assertFalse(response.has_header("Access-Control-Allow-Origin"))
109+
88110
def test_no_cors_header_origin_not_allowed(self):
89111
"""
90112
Test that /token endpoint does not have Access-Control-Allow-Origin

0 commit comments

Comments
 (0)