FastAPI + Claude Code CLI + vanilla JS чат. - JWT авторизация, SSE streaming - Загрузка файлов (Excel, Word, PDF, CSV, TXT) - Каждая вкладка — отдельная Claude-сессия - Системный промпт: русский язык, работа с файлами, уточняющие вопросы - Деплой: systemd + nginx + Let's Encrypt на smeta.kazin.ru Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
33 lines
988 B
Python
33 lines
988 B
Python
"""JWT authentication — single user from env vars."""
|
|
|
|
import os
|
|
import time
|
|
|
|
from jose import jwt
|
|
|
|
SECRET = os.environ.get("SESSION_SECRET", "change-me-in-production")
|
|
ALGORITHM = "HS256"
|
|
TOKEN_EXPIRE = 86400 * 7 # 7 days
|
|
|
|
VALID_USER = os.environ.get("SMETA_USER", "smeta")
|
|
VALID_PASS = os.environ.get("SMETA_PASS", "")
|
|
|
|
|
|
def authenticate(username: str, password: str) -> str | None:
|
|
"""Return JWT token if credentials are valid, else None."""
|
|
if not VALID_PASS:
|
|
return None # password must be set
|
|
if username == VALID_USER and password == VALID_PASS:
|
|
payload = {"sub": username, "exp": int(time.time()) + TOKEN_EXPIRE}
|
|
return jwt.encode(payload, SECRET, algorithm=ALGORITHM)
|
|
return None
|
|
|
|
|
|
def verify_token(token: str) -> str | None:
|
|
"""Return username if token is valid, else None."""
|
|
try:
|
|
payload = jwt.decode(token, SECRET, algorithms=[ALGORITHM])
|
|
return payload.get("sub")
|
|
except Exception:
|
|
return None
|