LoginSignup
1
1

More than 3 years have passed since last update.

aiohttp_session で EncryptedCookieStorage を使う際のシークレットキー生成

Last updated at Posted at 2020-04-13

備忘録。

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)])
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1