diff --git a/mitama/app/app.py b/mitama/app/app.py index 2008a0f..4eb28e3 100644 --- a/mitama/app/app.py +++ b/mitama/app/app.py @@ -154,11 +154,24 @@ def _session_middleware(): from mitama.app import Middleware from mitama.app.http.session import EncryptedCookieStorage + if "MITAMA_SESSION_KEY" in os.environ: + session_key = os.environ["MITAMA_SESSION_KEY"] + elif os.path.exists(".tmp/MITAMA_SESSION_KEY"): + with open(".tmp/MITAMA_SESSION_KEY", "r") as f: + session_key = f.read() + else: + key = fernet.Fernet.generate_key() + session_key = base64.urlsafe_b64encode(key).decode("utf-8") + if not os.path.exists(".tmp"): + os.mkdir(".tmp") + with open(".tmp/MITAMA_SESSION_KEY", "w") as f: + f.write(session_key) + class SessionMiddleware(Middleware): - fernet_key = fernet.Fernet.generate_key() + fernet_key = session_key def __init__(self): - secret_key = base64.urlsafe_b64decode(self.fernet_key) + secret_key = base64.urlsafe_b64decode(self.fernet_key.encode("utf-8")) cookie_storage = EncryptedCookieStorage(secret_key) self.storage = cookie_storage diff --git a/mitama/app/http/session.py b/mitama/app/http/session.py index 5130ecb..4cb5137 100644 --- a/mitama/app/http/session.py +++ b/mitama/app/http/session.py @@ -106,9 +106,9 @@ def __init__( self._encoder = encoder self._decoder = decoder if isinstance(secret_key, str): - pass + secret_key = secret_key.encode("utf-8") elif isinstance(secret_key, (bytes, bytearray)): - secret_key = base64.urlsafe_b64encode(secret_key) + pass self._fernet = fernet.Fernet(secret_key) @property diff --git a/tests/test_apps/uwsgi.ini b/tests/test_apps/uwsgi.ini new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_apps/uwsgi.py b/tests/test_apps/uwsgi.py new file mode 100644 index 0000000..7923ba2 --- /dev/null +++ b/tests/test_apps/uwsgi.py @@ -0,0 +1,14 @@ +#!/usr/bin/python + +""" +uwsgi --http=0.0.0.0:8080 --wsgi-file=/path/to/this --callable=app.wsgi +""" +import os + +from mitama.app import AppRegistry, _MainApp + +PROJECT_DIR = os.path.dirname(__file__) +os.chdir(PROJECT_DIR) +app_registry = AppRegistry() +app_registry.load_config() +app = _MainApp(app_registry)