6d626ff266
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
37 lines
973 B
Python
37 lines
973 B
Python
from datetime import datetime, timedelta, timezone
|
|
|
|
import bcrypt
|
|
from jose import jwt
|
|
|
|
from app.core.config import settings
|
|
|
|
|
|
_BCRYPT_ROUNDS = 13 # ~300 ms on modern hardware; increase over time as CPUs get faster
|
|
|
|
|
|
def hash_password(password: str) -> str:
|
|
return bcrypt.hashpw(password.encode(), bcrypt.gensalt(rounds=_BCRYPT_ROUNDS)).decode()
|
|
|
|
|
|
def verify_password(plain: str, hashed: str) -> bool:
|
|
return bcrypt.checkpw(plain.encode(), hashed.encode())
|
|
|
|
|
|
def create_access_token(subject: str) -> str:
|
|
now = datetime.now(timezone.utc)
|
|
expire = now + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
return jwt.encode(
|
|
{"sub": subject, "exp": expire, "iat": now},
|
|
settings.JWT_PRIVATE_KEY,
|
|
algorithm=settings.ALGORITHM,
|
|
)
|
|
|
|
|
|
def decode_access_token(token: str) -> str:
|
|
payload = jwt.decode(
|
|
token,
|
|
settings.JWT_PUBLIC_KEY,
|
|
algorithms=[settings.ALGORITHM],
|
|
)
|
|
return payload["sub"]
|