35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
from passlib.context import CryptContext
|
|
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
|
"""
|
|
Verifies a plain text password against a hashed password.
|
|
This is used by FastAPI-Users internally, but also exposed here for custom authentication flows
|
|
if needed.
|
|
|
|
Args:
|
|
plain_password: The password attempt.
|
|
hashed_password: The stored hash from the database.
|
|
|
|
Returns:
|
|
True if the password matches the hash, False otherwise.
|
|
"""
|
|
try:
|
|
return pwd_context.verify(plain_password, hashed_password)
|
|
except Exception:
|
|
return False
|
|
|
|
def hash_password(password: str) -> str:
|
|
"""
|
|
Hashes a plain text password using the configured context (bcrypt).
|
|
This is used by FastAPI-Users internally, but also exposed here for
|
|
custom user creation or password reset flows if needed.
|
|
|
|
Args:
|
|
password: The plain text password to hash.
|
|
|
|
Returns:
|
|
The resulting hash string.
|
|
"""
|
|
return pwd_context.hash(password) |