security.py 755 B

1234567891011121314151617181920212223242526
  1. from datetime import datetime, timedelta
  2. from typing import Any
  3. from app.config import settings
  4. from jose import jwt
  5. from passlib.context import CryptContext
  6. pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
  7. ALGORITHM = "HS256"
  8. def create_access_token(subject: str | Any, expires_delta: timedelta) -> str:
  9. expire = datetime.utcnow() + expires_delta
  10. to_encode = {"exp": expire, "sub": str(subject)}
  11. encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=ALGORITHM)
  12. return encoded_jwt
  13. def verify_password(plain_password: str, hashed_password: str) -> bool:
  14. return pwd_context.verify(plain_password, hashed_password)
  15. def get_password_hash(password: str) -> str:
  16. return pwd_context.hash(password)