add user password hasher and depebdency

This commit is contained in:
2024-04-01 01:12:01 +03:00
parent 7eaa082b41
commit 4e6aee8c3a
22 changed files with 310 additions and 366 deletions

View File

@@ -30,8 +30,7 @@ def create_engine(
def create_session_maker(
engine: Annotated[AsyncEngine, Depends(Stub(AsyncEngine))],
) -> async_sessionmaker[AsyncSession]:
maker = async_sessionmaker(engine, expire_on_commit=False)
return maker
return async_sessionmaker(engine, expire_on_commit=False)
async def new_session(

View File

@@ -0,0 +1,6 @@
from api.application.protocols.password_hasher import PasswordHasher
from api.infrastructure.security.password_hasher import Pbkdf2PasswordHasher
def get_password_hasher() -> PasswordHasher:
return Pbkdf2PasswordHasher()

View File

@@ -3,13 +3,15 @@ from typing import Annotated
from fastapi import Depends
from api.application.abstractions.uow import UnitOfWork
from api.application.usecase.create_user import CreateUser
from api.application.protocols.password_hasher import PasswordHasher
from api.application.usecase.user.create_user import CreateUser
from api.domain.user.repository import UserRepository
from api.infrastructure.dependencies.stub import Stub
def provide_create_user(
user_repository: Annotated[UserRepository, Depends(Stub(UserRepository))],
uow: Annotated[UnitOfWork, Depends()],
uow: Annotated[UnitOfWork, Depends(Stub(UnitOfWork))],
password_hasher: Annotated[PasswordHasher, Depends(Stub(PasswordHasher))],
) -> CreateUser:
return CreateUser(uow=uow, user_repository=user_repository)
return CreateUser(uow=uow, user_repository=user_repository, password_hasher=password_hasher)

View File

@@ -11,10 +11,10 @@ class SqlAlchemyUserRepository(UserRepository):
async def create_user(self, user: User) -> None:
stmt = insert(UserModel).values(
id=user.id,
name=user.name,
email=user.email,
hashed_password=user.password,
id=user.id.value,
name=user.name.value,
email=user.email.value,
hashed_password=user.hashed_password,
)
await self.session.execute(stmt)

View File

View File

@@ -0,0 +1,13 @@
from passlib.handlers.pbkdf2 import pbkdf2_sha256
from api.application.protocols.password_hasher import PasswordHasher
class Pbkdf2PasswordHasher(PasswordHasher):
@staticmethod
def hash_password(password: str) -> str:
return pbkdf2_sha256.hash(password)
@staticmethod
def verify_password(password: str, hashed_password: str) -> bool:
return pbkdf2_sha256.verify(password, hashed_password)