31 lines
1.0 KiB
Python
31 lines
1.0 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
from jose import jwt
|
|
from passlib.context import CryptContext
|
|
from app.core.config import get_settings
|
|
|
|
settings = get_settings()
|
|
pwd_context = CryptContext(schemes=["argon2"], deprecated="auto")
|
|
|
|
|
|
def hash_password(password: str) -> str:
|
|
return pwd_context.hash(password)
|
|
|
|
|
|
def verify_password(password: str, password_hash: str) -> bool:
|
|
return pwd_context.verify(password, password_hash)
|
|
|
|
|
|
def create_token(subject: str, token_type: str, expires_minutes: int) -> str:
|
|
now = datetime.now(timezone.utc)
|
|
exp = now + timedelta(minutes=expires_minutes)
|
|
payload = {"sub": subject, "type": token_type, "iat": int(now.timestamp()), "exp": int(exp.timestamp())}
|
|
return jwt.encode(payload, settings.jwt_secret_key, algorithm=settings.jwt_algorithm)
|
|
|
|
|
|
def create_access_token(subject: str) -> str:
|
|
return create_token(subject, "access", settings.jwt_access_token_minutes)
|
|
|
|
|
|
def create_refresh_token(subject: str) -> str:
|
|
return create_token(subject, "refresh", settings.jwt_refresh_token_minutes)
|