備忘録。
aiohttp_session の EncryptedCookieStorage を使う際のシークレットキーは、32バイトの bytes-like object もしくはそれを base64 エンコードした文字列でなくてはならない。
$ pip install cryptography
from cryptography.fernet import Fernet
print(Fernet.generate_key().decode())
aiohttp では以下のように設定する。
(生成したシークレットキーは環境変数 SESSION_SECRET
に入れている)
from aiohttp import web
from aiohttp_session import session_middleware
from aiohttp_session.cookie_storage import EncryptedCookieStorage
session_storage = EncryptedCookieStorage(os.environ.get('SESSION_SECRET'))
app = web.Application(middlewares=[session_middleware(session_storage)])