Skip to content

Commit e9abadf

Browse files
committed
Improve flask cors and update settings
1 parent 7f0286c commit e9abadf

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

api/base/settings/defaults.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@
217217
# Set dynamically on app init
218218
ORIGINS_WHITELIST = ()
219219

220+
# CORS settings for flask app
221+
FLASK_CORS_ORIGIN_WHITELIST = CORS_ORIGIN_WHITELIST
222+
FLASK_CORS_ALLOW_CREDENTIALS = CORS_ALLOW_CREDENTIALS
223+
220224
MIDDLEWARE = (
221225
'api.base.middleware.DjangoGlobalMiddleware',
222226
'api.base.middleware.CeleryTaskMiddleware',

api/base/settings/local-dist.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
ENABLE_VARNISH = False
88
ENABLE_ESI = False
99
CORS_ORIGIN_ALLOW_ALL = True
10+
FLASK_CORS_ORIGIN_WHITELIST += (osf_settings.LOCAL_ANGULAR_DOMAIN.rstrip('/'),)
11+
FLASK_CORS_ALLOW_CREDENTIALS = True
1012

1113
# Uncomment to get real tracebacks while testing
1214
# DEBUG_PROPAGATE_EXCEPTIONS = True

website/app.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask_cors import CORS
1+
from flask_cors import CORS as FLASK_CORS
22

33
import framework
44
import importlib
@@ -9,7 +9,7 @@
99

1010
import django
1111

12-
from api.base.settings import CORS_ORIGIN_WHITELIST
12+
from api.base.settings import FLASK_CORS_ALLOW_CREDENTIALS, FLASK_CORS_ORIGIN_WHITELIST
1313
from api.caching import listeners # noqa
1414
from django.apps import apps
1515
from framework.addons.utils import render_addon_capabilities
@@ -93,7 +93,9 @@ def init_app(settings_module='website.settings', set_backends=True, routes=True,
9393
if app.config.get('IS_INITIALIZED', False) is True:
9494
return app
9595

96-
CORS(app, supports_credentials=True, origins=CORS_ORIGIN_WHITELIST)
96+
# Setup CORS for flask app
97+
FLASK_CORS(app, supports_credentials=FLASK_CORS_ALLOW_CREDENTIALS, origins=FLASK_CORS_ORIGIN_WHITELIST)
98+
9799
setup_django()
98100

99101
# The settings module

website/settings/local-dist.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
DOMAIN = PROTOCOL + 'localhost:5000/'
2020
INTERNAL_DOMAIN = DOMAIN
2121
API_DOMAIN = PROTOCOL + 'localhost:8000/'
22+
LOCAL_ANGULAR_DOMAIN = PROTOCOL + 'localhost:4200/'
2223

2324
#WATERBUTLER_URL = 'http://localhost:7777'
2425
#WATERBUTLER_INTERNAL_URL = WATERBUTLER_URL

0 commit comments

Comments
 (0)