Huaxu Sentinel Active Safety Platform with embedded algorithm code, Docker Compose setup, and vendored dataset scaffolds for clone-and-run. Co-authored-by: Cursor <cursoragent@cursor.com>
30 lines
793 B
Python
30 lines
793 B
Python
"""JWT 令牌。"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
from typing import Any
|
|
|
|
from jose import JWTError, jwt
|
|
|
|
from as_platform.config import JWT_EXPIRE_HOURS, JWT_SECRET
|
|
|
|
ALGORITHM = "HS256"
|
|
|
|
|
|
def create_access_token(user_id: int, extra: dict[str, Any] | None = None) -> str:
|
|
payload = {
|
|
"sub": str(user_id),
|
|
"exp": datetime.now(timezone.utc) + timedelta(hours=JWT_EXPIRE_HOURS),
|
|
"iat": datetime.now(timezone.utc),
|
|
}
|
|
if extra:
|
|
payload.update(extra)
|
|
return jwt.encode(payload, JWT_SECRET, algorithm=ALGORITHM)
|
|
|
|
|
|
def decode_access_token(token: str) -> dict[str, Any] | None:
|
|
try:
|
|
return jwt.decode(token, JWT_SECRET, algorithms=[ALGORITHM])
|
|
except JWTError:
|
|
return None
|