This commit is contained in:
roma-dxunvrs
2025-11-30 12:38:46 +03:00
commit 27466a255f
17 changed files with 250 additions and 0 deletions

30
app/utils/jwt_utlis.py Normal file
View File

@@ -0,0 +1,30 @@
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