Coverage for src/lexigram/admin/auth/services/_cookie_config.py: 0%
12 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-24 23:18 +0800
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-24 23:18 +0800
1"""Typed helper for building session cookie keyword arguments from AdminAuthConfig."""
3from __future__ import annotations
5from typing import Literal, TypedDict
7from lexigram.admin.config import AdminAuthConfig
10class SessionCookieKwargs(TypedDict):
11 """Keyword arguments for Starlette SessionMiddleware."""
13 secret_key: str
14 https_only: bool
15 same_site: Literal["lax", "strict", "none"]
16 max_age: int | None
19def build_session_cookie_kwargs(cfg: AdminAuthConfig) -> SessionCookieKwargs:
20 """Build SessionMiddleware kwargs from AdminAuthConfig.
22 In production the cookie is Secure, HttpOnly (implied by SessionMiddleware),
23 and SameSite=strict. In development, https_only is relaxed so local dev
24 without HTTPS still works.
25 """
26 is_prod = cfg.env == "production"
27 return SessionCookieKwargs(
28 secret_key=cfg.session_secret.get_secret_value(),
29 https_only=is_prod,
30 same_site="strict" if is_prod else "lax",
31 max_age=cfg.session_lifetime,
32 )
35__all__ = [
36 "SessionCookieKwargs",
37 "build_session_cookie_kwargs",
38]