44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
from uuid import uuid4
|
|
|
|
from jose import jwt
|
|
from passlib.context import CryptContext
|
|
|
|
from app.core.config import get_settings
|
|
|
|
pwd_context = CryptContext(schemes=["pbkdf2_sha256"], deprecated="auto")
|
|
|
|
|
|
def hash_password(password: str) -> str:
|
|
return pwd_context.hash(password)
|
|
|
|
|
|
def verify_password(password: str, hashed_password: str) -> bool:
|
|
return pwd_context.verify(password, hashed_password)
|
|
|
|
|
|
def create_access_token(user_id: str) -> str:
|
|
settings = get_settings()
|
|
now = datetime.now(tz=timezone.utc)
|
|
payload = {
|
|
"sub": user_id,
|
|
"type": "access",
|
|
"iat": int(now.timestamp()),
|
|
"exp": int((now + timedelta(minutes=settings.app_access_token_expire_min)).timestamp()),
|
|
"jti": str(uuid4()),
|
|
}
|
|
return jwt.encode(payload, settings.app_secret_key, algorithm="HS256")
|
|
|
|
|
|
def create_refresh_token(user_id: str) -> str:
|
|
settings = get_settings()
|
|
now = datetime.now(tz=timezone.utc)
|
|
payload = {
|
|
"sub": user_id,
|
|
"type": "refresh",
|
|
"iat": int(now.timestamp()),
|
|
"exp": int((now + timedelta(days=settings.app_refresh_token_expire_days)).timestamp()),
|
|
"jti": str(uuid4()),
|
|
}
|
|
return jwt.encode(payload, settings.app_secret_key, algorithm="HS256")
|