Files
AuthMicroservice/app/utils/jwt_utlis.py
roma-dxunvrs 27466a255f Demo
2025-11-30 12:38:46 +03:00

30 lines
1.0 KiB
Python

from datetime import timedelta, datetime
import jwt
from app.core.config import settings
def encode_jwt(payload: dict,
private_key: str = settings.auth_jwt.private_key_path.read_text(),
algorithm=settings.auth_jwt.algorithm,
expire_timedelta: timedelta | None = None,
expire_minutes: int = settings.auth_jwt.access_token_expire_minutes):
to_encode = payload.copy()
now = datetime.utcnow()
if expire_timedelta:
expire = now + timedelta
else:
expire = now + timedelta(minutes=expire_minutes)
to_encode.update(
exp=expire,
iat=now
)
encoded = jwt.encode(to_encode,
private_key,
algorithm=algorithm)
return encoded
def decode_jwt(token: str | bytes,
public_key: str = settings.auth_jwt.public_key_path.read_text(),
algorithm: str = settings.auth_jwt.algorithm):
decoded = jwt.decode(token, public_key, algorithms=[algorithm])
return decoded