Files
mealie/mealie/core/security/hasher.py
Hayden c988de1921 fix: group creation (#1126)
* fix: unify group creation - closes #1100

* tests: disable password hashing during testing

* tests: fix email config tests
2022-04-02 19:33:15 -08:00

44 lines
965 B
Python

from functools import lru_cache
from typing import Protocol
from passlib.context import CryptContext
from mealie.core.config import get_app_settings
class Hasher(Protocol):
def hash(self, password: str) -> str:
...
def verify(self, password: str, hashed: str) -> bool:
...
class FakeHasher:
def hash(self, password: str) -> str:
return password
def verify(self, password: str, hashed: str) -> bool:
return password == hashed
class PasslibHasher:
def __init__(self) -> None:
self.ctx = CryptContext(schemes=["bcrypt"], deprecated="auto")
def hash(self, password: str) -> str:
return self.ctx.hash(password)
def verify(self, password: str, hashed: str) -> bool:
return self.ctx.verify(password, hashed)
@lru_cache(maxsize=1)
def get_hasher() -> Hasher:
settings = get_app_settings()
if settings.TESTING:
return FakeHasher()
return PasslibHasher()