34 lines
761 B
Python
34 lines
761 B
Python
from datetime import UTC, datetime, timedelta
|
|
|
|
from jose import jwt
|
|
from pwdlib import PasswordHash
|
|
|
|
from app.core.config import settings
|
|
|
|
password_hash = PasswordHash.recommended()
|
|
|
|
ALGORITHM = "HS256"
|
|
ACCESS_TOKEN_EXPIRE_MINUTES = 60
|
|
|
|
|
|
def hash_password(password: str) -> str:
|
|
return password_hash.hash(password)
|
|
|
|
|
|
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
|
return password_hash.verify(plain_password, hashed_password)
|
|
|
|
|
|
def create_access_token(subject: str) -> str:
|
|
expire = datetime.now(UTC) + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
|
|
payload = {
|
|
"sub": subject,
|
|
"exp": expire,
|
|
}
|
|
|
|
return jwt.encode(
|
|
payload,
|
|
settings.secret_key,
|
|
algorithm=ALGORITHM,
|
|
)
|