-
Notifications
You must be signed in to change notification settings - Fork 809
Add CORS Middleware #1150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add CORS Middleware #1150
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
24ee096
Approach for adding a CORS-middleware
cheif cafd471
Update tutorial to include cors-middleware
cheif e8e0148
Merge branch 'master' into cors-header-middleware
rcmurphy 4bac4f9
Update CORS middleware and tests
rcmurphy f51a741
Optimize CorsMiddleware
rcmurphy 1281382
Remove errant merge artifact
rcmurphy 8db6b45
Complete PR Checklist
rcmurphy 42222aa
Merge branch 'master' into cors-header-middleware
rcmurphy 509df8c
Merge branch 'master' into cors-header-middleware
n2ygk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from datetime import timedelta | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.test import Client, TestCase, override_settings | ||
from django.utils import timezone | ||
|
||
from oauth2_provider.models import AccessToken, get_application_model | ||
|
||
|
||
Application = get_application_model() | ||
UserModel = get_user_model() | ||
|
||
|
||
@override_settings( | ||
AUTHENTICATION_BACKENDS=("oauth2_provider.backends.OAuth2Backend",), | ||
MIDDLEWARE_CLASSES=( | ||
"oauth2_provider.middleware.OAuth2TokenMiddleware", | ||
"oauth2_provider.middleware.CorsMiddleware", | ||
), | ||
) | ||
class TestCORSMiddleware(TestCase): | ||
def setUp(self): | ||
self.user = UserModel.objects.create_user("test_user", "[email protected]") | ||
self.application = Application.objects.create( | ||
name="Test Application", | ||
redirect_uris="https://foo.bar", | ||
user=self.user, | ||
client_type=Application.CLIENT_CONFIDENTIAL, | ||
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE, | ||
) | ||
|
||
self.access_token = AccessToken.objects.create( | ||
user=self.user, | ||
scope="read write", | ||
expires=timezone.now() + timedelta(seconds=300), | ||
token="secret-access-token-key", | ||
application=self.application, | ||
) | ||
|
||
auth_header = "Bearer {0}".format(self.access_token.token) | ||
self.client = Client(HTTP_AUTHORIZATION=auth_header) | ||
|
||
def test_cors_successful(self): | ||
"""Ensure that we get cors-headers according to our oauth-app""" | ||
resp = self.client.post("/cors-test/", HTTP_ORIGIN="https://foo.bar") | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertEqual(resp["Access-Control-Allow-Origin"], "https://foo.bar") | ||
self.assertEqual(resp["Access-Control-Allow-Credentials"], "true") | ||
|
||
def test_cors_no_auth(self): | ||
"""Ensure that CORS-headers are sent non-authenticated requests""" | ||
client = Client() | ||
resp = client.post("/cors-test/", HTTP_ORIGIN="https://foo.bar") | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertEqual(resp["Access-Control-Allow-Origin"], "https://foo.bar") | ||
self.assertEqual(resp["Access-Control-Allow-Credentials"], "true") | ||
|
||
def test_cors_wrong_origin(self): | ||
"""Ensure that CORS-headers aren't sent to requests from wrong origin""" | ||
resp = self.client.post("/cors-test/", HTTP_ORIGIN="https://bar.foo") | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertFalse(resp.has_header("Access-Control-Allow-Origin")) | ||
|
||
def test_cors_200_preflight(self): | ||
"""Ensure that preflight always get 200 responses""" | ||
resp = self.client.options( | ||
"/cors-test/", HTTP_ACCESS_CONTROL_REQUEST_METHOD="GET", HTTP_ORIGIN="https://foo.bar" | ||
) | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertEqual(resp["Access-Control-Allow-Origin"], "https://foo.bar") | ||
self.assertTrue(resp.has_header("Access-Control-Allow-Headers")) | ||
self.assertTrue(resp.has_header("Access-Control-Allow-Methods")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
from django.contrib import admin | ||
from django.urls import include, path | ||
|
||
from .views import MockView | ||
|
||
|
||
admin.autodiscover() | ||
|
||
|
||
urlpatterns = [ | ||
path("o/", include("oauth2_provider.urls", namespace="oauth2_provider")), | ||
path("admin/", admin.site.urls), | ||
path("cors-test/", MockView.as_view()), | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.http import HttpResponse | ||
from django.views.generic import View | ||
|
||
|
||
class MockView(View): | ||
def post(self, request): | ||
return HttpResponse() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.