diff --git a/cms/envs/common.py b/cms/envs/common.py index 26165aa9e0..87c3a31aee 100644 --- a/cms/envs/common.py +++ b/cms/envs/common.py @@ -43,6 +43,7 @@ When refering to XBlocks, we use the entry-point name. For example, import importlib.util import os import sys +from corsheaders.defaults import default_headers as corsheaders_default_headers from datetime import timedelta import lms.envs.common # Although this module itself may not use these imported variables, other dependent modules may. @@ -660,8 +661,15 @@ MIDDLEWARE = [ 'openedx.core.djangoapps.header_control.middleware.HeaderControlMiddleware', 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.sites.middleware.CurrentSiteMiddleware', + + # CORS and CSRF + 'django.middleware.csrf.CsrfViewMiddleware', + 'corsheaders.middleware.CorsMiddleware', + 'openedx.core.djangoapps.cors_csrf.middleware.CorsCSRFMiddleware', + 'openedx.core.djangoapps.cors_csrf.middleware.CsrfCrossDomainCookieMiddleware', + + # JWT auth 'edx_rest_framework_extensions.auth.jwt.middleware.JwtAuthCookieMiddleware', # Allows us to define redirects via Django admin @@ -2079,9 +2087,17 @@ FINANCIAL_REPORTS = { 'ROOT_PATH': 'sandbox', } +############# CORS headers for cross-domain requests ################# +if FEATURES.get('ENABLE_CORS_HEADERS'): + CORS_ALLOW_CREDENTIALS = True + CORS_ALLOW_HEADERS = corsheaders_default_headers + ( + 'use-jwt-cookie', + ) + CORS_ORIGIN_WHITELIST = [] CORS_ORIGIN_ALLOW_ALL = False + LOGIN_REDIRECT_WHITELIST = [] DEPRECATED_ADVANCED_COMPONENT_TYPES = [] diff --git a/cms/envs/devstack.py b/cms/envs/devstack.py index 84f5c6cd54..5552a140ba 100644 --- a/cms/envs/devstack.py +++ b/cms/envs/devstack.py @@ -5,6 +5,7 @@ Specific overrides to the base prod settings to make development easier. import logging from os.path import abspath, dirname, join +from corsheaders.defaults import default_headers as corsheaders_default_headers from .production import * # pylint: disable=wildcard-import, unused-wildcard-import @@ -213,3 +214,11 @@ SECRET_KEY = '85920908f28904ed733fe576320db18cabd7b6cd' # See if the developer has any local overrides. if os.path.isfile(join(dirname(abspath(__file__)), 'private.py')): from .private import * # pylint: disable=import-error,wildcard-import + +############# CORS headers for cross-domain requests ################# +FEATURES['ENABLE_CORS_HEADERS'] = True +CORS_ALLOW_CREDENTIALS = True +CORS_ORIGIN_ALLOW_ALL = True +CORS_ALLOW_HEADERS = corsheaders_default_headers + ( + 'use-jwt-cookie', +) diff --git a/cms/envs/production.py b/cms/envs/production.py index 0882aeef48..935052f977 100644 --- a/cms/envs/production.py +++ b/cms/envs/production.py @@ -12,6 +12,7 @@ import copy import os import yaml +from corsheaders.defaults import default_headers as corsheaders_default_headers from django.core.exceptions import ImproperlyConfigured from django.urls import reverse_lazy from path import Path as path @@ -543,3 +544,13 @@ plugin_settings.add_plugins(__name__, plugin_constants.ProjectType.CMS, plugin_c ########################## Derive Any Derived Settings ####################### derive_settings(__name__) + +############# CORS headers for cross-domain requests ################# +if FEATURES.get('ENABLE_CORS_HEADERS'): + CORS_ALLOW_CREDENTIALS = True + CORS_ORIGIN_WHITELIST = ENV_TOKENS.get('CORS_ORIGIN_WHITELIST', ()) + CORS_ORIGIN_ALLOW_ALL = ENV_TOKENS.get('CORS_ORIGIN_ALLOW_ALL', False) + CORS_ALLOW_INSECURE = ENV_TOKENS.get('CORS_ALLOW_INSECURE', False) + CORS_ALLOW_HEADERS = corsheaders_default_headers + ( + 'use-jwt-cookie', + )