Files
EnuivTools/backend/admin/session.py
T

37 lines
856 B
Python

"""管理员浏览器会话。只表示当前是管理员,不写入普通用户会话。"""
from __future__ import annotations
from fastapi import Response
from euniv.crypto import decrypt_secret, encrypt_secret
from euniv.session import MAX_AGE
COOKIE_NAME = "euniv_admin_session"
_MARK = "euniv-admin"
def admin_session_active(cookies) -> bool:
raw = cookies.get(COOKIE_NAME)
if not raw:
return False
try:
return decrypt_secret(raw) == _MARK
except Exception:
return False
def write_admin_session(response: Response) -> None:
response.set_cookie(
COOKIE_NAME,
encrypt_secret(_MARK),
max_age=MAX_AGE,
httponly=True,
samesite="lax",
path="/",
)
def clear_admin_session(response: Response) -> None:
response.delete_cookie(COOKIE_NAME, path="/")