mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-14 18:58:10 +08:00
20 lines
465 B
Python
20 lines
465 B
Python
from datetime import datetime, timedelta
|
|
from typing import Optional
|
|
|
|
import jwt
|
|
|
|
JWT_ALGORITHM = "HS256"
|
|
|
|
|
|
def generate_jwt(
|
|
data: dict,
|
|
secret: str,
|
|
lifetime_seconds: Optional[int] = None,
|
|
algorithm: str = JWT_ALGORITHM,
|
|
) -> str:
|
|
payload = data.copy()
|
|
if lifetime_seconds:
|
|
expire = datetime.utcnow() + timedelta(seconds=lifetime_seconds)
|
|
payload["exp"] = expire
|
|
return jwt.encode(payload, secret, algorithm=algorithm)
|