mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-15 19:30:47 +08:00
37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
from typing import Optional, Protocol, Tuple
|
|
|
|
from passlib import pwd
|
|
from passlib.context import CryptContext
|
|
|
|
|
|
class PasswordHelperProtocol(Protocol):
|
|
def verify_and_update(
|
|
self, plain_password: str, hashed_password: str
|
|
) -> Tuple[bool, str]:
|
|
... # pragma: no cover
|
|
|
|
def hash(self, password: str) -> str:
|
|
... # pragma: no cover
|
|
|
|
def generate(self) -> str:
|
|
... # pragma: no cover
|
|
|
|
|
|
class PasswordHelper(PasswordHelperProtocol):
|
|
def __init__(self, context: Optional[CryptContext] = None) -> None:
|
|
if context is None:
|
|
self.context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
else:
|
|
self.context = context # pragma: no cover
|
|
|
|
def verify_and_update(
|
|
self, plain_password: str, hashed_password: str
|
|
) -> Tuple[bool, str]:
|
|
return self.context.verify_and_update(plain_password, hashed_password)
|
|
|
|
def hash(self, password: str) -> str:
|
|
return self.context.hash(password)
|
|
|
|
def generate(self) -> str:
|
|
return pwd.genword()
|