28 lines
836 B
Python
28 lines
836 B
Python
|
|
from datetime import datetime, timedelta, timezone
|
||
|
|
|
||
|
|
import bcrypt
|
||
|
|
from jose import jwt
|
||
|
|
|
||
|
|
from app.core.config import settings
|
||
|
|
|
||
|
|
|
||
|
|
def hash_password(password: str) -> str:
|
||
|
|
return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
||
|
|
|
||
|
|
|
||
|
|
def verify_password(plain: str, hashed: str) -> bool:
|
||
|
|
return bcrypt.checkpw(plain.encode(), hashed.encode())
|
||
|
|
|
||
|
|
|
||
|
|
def create_access_token(data: dict, expires_delta: timedelta | None = None) -> str:
|
||
|
|
to_encode = data.copy()
|
||
|
|
expire = datetime.now(timezone.utc) + (
|
||
|
|
expires_delta or timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||
|
|
)
|
||
|
|
to_encode["exp"] = expire
|
||
|
|
return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
|
||
|
|
|
||
|
|
|
||
|
|
def decode_token(token: str) -> dict:
|
||
|
|
return jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
|