From def6a6f0f74be30774ee53cf4baa7ae42ecda586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E7=93=9C=E4=B8=B8?= Date: Wed, 25 Nov 2020 21:39:56 +0900 Subject: [PATCH 1/4] [update] --- mitama/app/app.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mitama/app/app.py b/mitama/app/app.py index 2008a0f..a237426 100644 --- a/mitama/app/app.py +++ b/mitama/app/app.py @@ -154,8 +154,18 @@ def _session_middleware(): from mitama.app import Middleware from mitama.app.http.session import EncryptedCookieStorage - class SessionMiddleware(Middleware): + if "MITAMA_SESSION_KEY" in os.environ: + fernet_key = os.environ["MITAMA_SESSION_KEY"] + elif os.path.exists(".tmp/MITAMA_SESSION_KEY"): + with open(".tmp/MITAMA_SESSION_KEY") as f: + fernet_key = f.read() + else: fernet_key = fernet.Fernet.generate_key() + os.mkdir(".tmp") + with open(".tmp/MITAMA_SESSION_KEY") as f: + f.write(fernet_key) + class SessionMiddleware(Middleware): + fernet_key = fernet_key def __init__(self): secret_key = base64.urlsafe_b64decode(self.fernet_key) From 7374ea57725418ec5abf6daac928e11cf8a2047b Mon Sep 17 00:00:00 2001 From: boke0 Date: Wed, 25 Nov 2020 23:50:04 +0900 Subject: [PATCH 2/4] [fix] --- mitama/app/app.py | 23 ++++++++++++++--------- mitama/app/http/session.py | 4 ++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/mitama/app/app.py b/mitama/app/app.py index a237426..ac785f6 100644 --- a/mitama/app/app.py +++ b/mitama/app/app.py @@ -155,20 +155,25 @@ def _session_middleware(): from mitama.app.http.session import EncryptedCookieStorage if "MITAMA_SESSION_KEY" in os.environ: - fernet_key = os.environ["MITAMA_SESSION_KEY"] + session_key = os.environ["MITAMA_SESSION_KEY"] elif os.path.exists(".tmp/MITAMA_SESSION_KEY"): - with open(".tmp/MITAMA_SESSION_KEY") as f: - fernet_key = f.read() + with open(".tmp/MITAMA_SESSION_KEY", "r") as f: + session_key = f.read() else: - fernet_key = fernet.Fernet.generate_key() - os.mkdir(".tmp") - with open(".tmp/MITAMA_SESSION_KEY") as f: - f.write(fernet_key) + 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_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..b6700c5 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 From 9bd96260561e0921277754df36fb2d0589e0266c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E7=93=9C=E4=B8=B8?= Date: Wed, 25 Nov 2020 23:50:27 +0900 Subject: [PATCH 3/4] [fix] --- mitama/app/app.py | 6 ++---- tests/test_apps/uwsgi.ini | 0 tests/test_apps/uwsgi.py | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 tests/test_apps/uwsgi.ini create mode 100644 tests/test_apps/uwsgi.py diff --git a/mitama/app/app.py b/mitama/app/app.py index ac785f6..4eb28e3 100644 --- a/mitama/app/app.py +++ b/mitama/app/app.py @@ -161,9 +161,7 @@ def _session_middleware(): session_key = f.read() else: key = fernet.Fernet.generate_key() - session_key = base64.urlsafe_b64encode( - key - ).decode("utf-8") + 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: @@ -173,7 +171,7 @@ class SessionMiddleware(Middleware): fernet_key = session_key def __init__(self): - secret_key = base64.urlsafe_b64decode(self.fernet_key.encode('utf-8')) + secret_key = base64.urlsafe_b64decode(self.fernet_key.encode("utf-8")) cookie_storage = EncryptedCookieStorage(secret_key) self.storage = cookie_storage 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) From 6286926019c853cfbcf4d699da67be7d80046ed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E7=93=9C=E4=B8=B8?= Date: Wed, 25 Nov 2020 23:56:18 +0900 Subject: [PATCH 4/4] [fix] --- mitama/app/http/session.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mitama/app/http/session.py b/mitama/app/http/session.py index b6700c5..4cb5137 100644 --- a/mitama/app/http/session.py +++ b/mitama/app/http/session.py @@ -106,7 +106,7 @@ def __init__( self._encoder = encoder self._decoder = decoder if isinstance(secret_key, str): - secret_key = secret_key.encode('utf-8') + secret_key = secret_key.encode("utf-8") elif isinstance(secret_key, (bytes, bytearray)): pass self._fernet = fernet.Fernet(secret_key)