Skip to content

Commit

Permalink
Merge pull request #118 from mitama-org/feature/session
Browse files Browse the repository at this point in the history
Feature/session
  • Loading branch information
boke0 authored Nov 25, 2020
2 parents b57295b + 6286926 commit 0a2c355
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
17 changes: 15 additions & 2 deletions mitama/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions mitama/app/http/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added tests/test_apps/uwsgi.ini
Empty file.
14 changes: 14 additions & 0 deletions tests/test_apps/uwsgi.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0a2c355

Please sign in to comment.