Files
loon-backend/loon/web/utils/jwt.py
T
ciomek c479862c0e
Build and Push Docker Image / build-and-push (push) Successful in 16s
JWT Fix
2026-08-24 22:21:06 +02:00

32 lines
735 B
Python

import time
from typing import Literal
import jwt
from config import settings
JWT_SECRET = settings.jwt.secret
JWT_ALGORITHM = "HS256"
def _encode_jwt(user_id: int, ttl: int, typ: Literal["ws", "web"]) -> str:
payload = {
"user_id": user_id,
"exp": int(time.time()) + ttl,
"typ": typ,
}
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
def generate_token(user_id: int) -> str:
return _encode_jwt(user_id, settings.jwt.access_ttl, "web")
def generate_ws_token(user_id: int) -> str:
return _encode_jwt(user_id, settings.jwt.ws_ttl, "ws")
def decode_token(token: str) -> dict:
payload = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
return payload