27 lines
757 B
Python
27 lines
757 B
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
from typing import Any
|
|
|
|
import jwt
|
|
|
|
from api.config import settings
|
|
|
|
|
|
def create_access_token(*, user_id: int, profile: dict[str, Any]) -> str:
|
|
now = datetime.now(timezone.utc)
|
|
payload = {
|
|
"sub": str(user_id),
|
|
"uid": user_id,
|
|
"fn": profile.get("first_name"),
|
|
"ln": profile.get("last_name"),
|
|
"un": profile.get("username"),
|
|
"iat": now,
|
|
"exp": now + timedelta(seconds=settings.jwt_ttl_seconds),
|
|
}
|
|
return jwt.encode(payload, settings.session_secret, algorithm="HS256")
|
|
|
|
|
|
def decode_access_token(token: str) -> dict[str, Any]:
|
|
return jwt.decode(token, settings.session_secret, algorithms=["HS256"])
|