42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
from typing import Any
|
|
from jose import JWTError, jwt
|
|
from passlib.context import CryptContext
|
|
from app.config import get_settings
|
|
|
|
settings = get_settings()
|
|
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
|
|
def hash_password(password: str) -> str:
|
|
return pwd_context.hash(password)
|
|
|
|
|
|
def verify_password(plain: str, hashed: str) -> bool:
|
|
return pwd_context.verify(plain, hashed)
|
|
|
|
|
|
def _create_token(data: dict[str, Any], expires_delta: timedelta) -> str:
|
|
payload = data.copy()
|
|
payload["exp"] = datetime.now(timezone.utc) + expires_delta
|
|
return jwt.encode(payload, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
|
|
|
|
|
|
def create_access_token(user_id: int, role: str) -> str:
|
|
return _create_token(
|
|
{"sub": str(user_id), "role": role, "type": "access"},
|
|
timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES),
|
|
)
|
|
|
|
|
|
def create_refresh_token(user_id: int) -> str:
|
|
return _create_token(
|
|
{"sub": str(user_id), "type": "refresh"},
|
|
timedelta(days=settings.REFRESH_TOKEN_EXPIRE_DAYS),
|
|
)
|
|
|
|
|
|
def decode_token(token: str) -> dict[str, Any]:
|
|
return jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
|