add user password hasher and depebdency
This commit is contained in:
@@ -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(
|
||||
|
6
api/infrastructure/dependencies/protocols.py
Normal file
6
api/infrastructure/dependencies/protocols.py
Normal 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()
|
@@ -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)
|
||||
|
@@ -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)
|
||||
|
||||
|
0
api/infrastructure/security/__init__.py
Normal file
0
api/infrastructure/security/__init__.py
Normal file
13
api/infrastructure/security/password_hasher.py
Normal file
13
api/infrastructure/security/password_hasher.py
Normal 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)
|
Reference in New Issue
Block a user