Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move important JWT settings to settings.py
Maciej Korycinski877810e83da7
diff --git a/saleor/core/jwt.py b/saleor/core/jwt.pyindex 35f04b72db..36c4893f2c 100644--- a/saleor/core/jwt.py+++ b/saleor/core/jwt.py@@ -1,14 +1,12 @@-from datetime import datetime, timedelta+from datetime import datetimefrom typing import List, Optionalimport grapheneimport jwt-from saleor.account.models import User+from ..account.models import User+from ..settings import JWT_EXPIRATION_DELTA, JWT_REFRESH_EXPIRATION_DELTA, JWT_SECRET-JWT_EXPIRATION_DELTA = timedelta(seconds=100)-JWT_REFRESH_EXPIRATION_DELTA = timedelta(hours=100)-JWT_SECRET = "ABC"JWT_ALGORITHM = "HS256"JWT_AUTH_HEADER = "HTTP_AUTHORIZATION"JWT_AUTH_HEADER_PREFIX = "JWT"diff --git a/saleor/core/middleware.py b/saleor/core/middleware.pyindex f05349c226..8c06d38301 100644--- a/saleor/core/middleware.py+++ b/saleor/core/middleware.py@@ -12,7 +12,7 @@ from django_countries.fields import Countryfrom ..discount.utils import fetch_discountsfrom ..plugins.manager import get_plugins_managerfrom . import analytics-from .jwt import JWT_REFRESH_EXPIRATION_DELTA, JWT_REFRESH_TOKEN_COOKIE_NAME+from .jwt import JWT_REFRESH_TOKEN_COOKIE_NAMEfrom .utils import get_client_ip, get_country_by_ip, get_currency_for_countrylogger = logging.getLogger(__name__)@@ -126,7 +126,7 @@ def jwt_refresh_token_middleware(get_response):response = get_response(request)jwt_refresh_token = getattr(request, "refresh_token", None)if jwt_refresh_token:- expires = datetime.utcnow() + JWT_REFRESH_EXPIRATION_DELTA+ expires = datetime.utcnow() + settings.JWT_REFRESH_EXPIRATION_DELTAresponse.set_cookie(JWT_REFRESH_TOKEN_COOKIE_NAME,jwt_refresh_token,diff --git a/saleor/settings.py b/saleor/settings.pyindex 662d14063f..cfb1418224 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -1,6 +1,7 @@import astimport os.pathimport warnings+from datetime import timedeltaimport dj_database_urlimport dj_email_url@@ -547,3 +548,16 @@ REDIS_URL = os.environ.get("REDIS_URL")if REDIS_URL:CACHE_URL = os.environ.setdefault("CACHE_URL", REDIS_URL)CACHES = {"default": django_cache_url.config()}++JWT_EXPIRATION_DELTA = timedelta(+ seconds=os.environ.get("JWT_EXPIRATION_DELTA", 5 * 60) # type: ignore+)+++JWT_REFRESH_EXPIRATION_DELTA = timedelta( # 30 days+ seconds=os.environ.get( # type: ignore+ "JWT_REFRESH_EXPIRATION_DELTA", 60 * 60 * 24 * 30+ )+)++JWT_SECRET = os.environ.get("JWT_SECRET", SECRET_KEY)diff --git a/tests/api/test_account_mutations_jwt.py b/tests/api/test_account_mutations_jwt.pyindex 22ef05781d..78a156f35e 100644--- a/tests/api/test_account_mutations_jwt.py+++ b/tests/api/test_account_mutations_jwt.py@@ -8,8 +8,6 @@ from saleor.account.error_codes import AccountErrorCodefrom saleor.core.jwt import (JWT_ACCESS_TYPE,JWT_ALGORITHM,- JWT_EXPIRATION_DELTA,- JWT_REFRESH_EXPIRATION_DELTA,JWT_REFRESH_TOKEN_COOKIE_NAME,JWT_REFRESH_TYPE,JWT_SECRET,@@ -41,7 +39,7 @@ MUTATION_CREATE_TOKEN = """@freeze_time("2020-03-18 12:00:00")-def test_create_token(api_client, customer_user):+def test_create_token(api_client, customer_user, settings):variables = {"email": customer_user.email, "password": customer_user._password}response = api_client.post_graphql(MUTATION_CREATE_TOKEN, variables)content = get_graphql_content(response)@@ -59,27 +57,29 @@ def test_create_token(api_client, customer_user):assert payload["email"] == customer_user.emailassert payload["user_id"] == graphene.Node.to_global_id("User", customer_user.id)assert datetime.fromtimestamp(payload["iat"]) == datetime.utcnow()- expected_expiration_datetime = datetime.utcnow() + JWT_EXPIRATION_DELTA+ expected_expiration_datetime = datetime.utcnow() + settings.JWT_EXPIRATION_DELTAassert datetime.fromtimestamp(payload["exp"]) == expected_expiration_datetimeassert payload["type"] == JWT_ACCESS_TYPEpayload = decode(refreshToken, JWT_SECRET, algorithms=JWT_ALGORITHM)assert payload["email"] == customer_user.emailassert datetime.fromtimestamp(payload["iat"]) == datetime.utcnow()- expected_expiration_datetime = datetime.utcnow() + JWT_REFRESH_EXPIRATION_DELTA+ expected_expiration_datetime = (+ datetime.utcnow() + settings.JWT_REFRESH_EXPIRATION_DELTA+ )assert datetime.fromtimestamp(payload["exp"]) == expected_expiration_datetimeassert payload["type"] == JWT_REFRESH_TYPE@freeze_time("2020-03-18 12:00:00")-def test_create_token_sets_cookie(api_client, customer_user):+def test_create_token_sets_cookie(api_client, customer_user, settings):variables = {"email": customer_user.email, "password": customer_user._password}response = api_client.post_graphql(MUTATION_CREATE_TOKEN, variables)expected_refresh_token = create_refresh_token(customer_user)refresh_token = response.cookies["refreshToken"]assert refresh_token.value == expected_refresh_token- expected_expires = datetime.utcnow() + JWT_REFRESH_EXPIRATION_DELTA+ expected_expires = datetime.utcnow() + settings.JWT_REFRESH_EXPIRATION_DELTAexpected_expires += timedelta(seconds=1)expires = datetime.strptime(refresh_token["expires"], "%a, %d %b %Y %H:%M:%S %Z")assert expires == expected_expires@@ -157,9 +157,7 @@ MUTATION_TOKEN_REFRESH = """@freeze_time("2020-03-18 12:00:00")-def test_refresh_token_get_token_from_cookie(- api_client, customer_user,-):+def test_refresh_token_get_token_from_cookie(api_client, customer_user, settings):refresh_token = create_refresh_token(customer_user)variables = {"token": None}api_client.cookies[JWT_REFRESH_TOKEN_COOKIE_NAME] = refresh_token@@ -178,13 +176,13 @@ def test_refresh_token_get_token_from_cookie(assert datetime.fromtimestamp(payload["iat"]) == datetime.utcnow()assert (datetime.fromtimestamp(payload["exp"])- == datetime.utcnow() + JWT_EXPIRATION_DELTA+ == datetime.utcnow() + settings.JWT_EXPIRATION_DELTA)assert payload["type"] == JWT_ACCESS_TYPE@freeze_time("2020-03-18 12:00:00")-def test_refresh_token_get_token_from_input(api_client, customer_user):+def test_refresh_token_get_token_from_input(api_client, customer_user, settings):refresh_token = create_refresh_token(customer_user)variables = {"token": refresh_token}response = api_client.post_graphql(MUTATION_TOKEN_REFRESH, variables)@@ -201,7 +199,7 @@ def test_refresh_token_get_token_from_input(api_client, customer_user):assert datetime.fromtimestamp(payload["iat"]) == datetime.utcnow()assert (datetime.fromtimestamp(payload["exp"])- == datetime.utcnow() + JWT_EXPIRATION_DELTA+ == datetime.utcnow() + settings.JWT_EXPIRATION_DELTA)assert payload["type"] == JWT_ACCESS_TYPE