refac: password and code hashing & checking go through a domain hasher interface used by the use cases instead of bcrypt in the user repository and the auth handler

This commit is contained in:
fsylvain committed 2026-09-23 19:29:20 +02:00
1 parent 8460a078e3
commit ff8cd5f0ec
16 files changed
+725 -88

No files matched your search

+6 -4
View File
@@ -7,6 +7,7 @@ import (
validator "RenewCMS/api/pkg/validator"
domainArticle "RenewCMS/internal/domain/article"
domainUser "RenewCMS/internal/domain/user"
"RenewCMS/internal/infrastructure/hashing"
"RenewCMS/internal/infrastructure/persistence/models"
persistence "RenewCMS/internal/infrastructure/persistence/repositories"
"RenewCMS/internal/infrastructure/useCases"
@@ -20,7 +21,6 @@ import (
"github.com/glebarez/sqlite"
"github.com/go-chi/chi/v5"
"github.com/go-chi/jwtauth/v5"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
@@ -130,11 +130,13 @@ func newNotFoundErrorsRouter(articleRepo *mockArticleRepo, userRepo *mockUserRep
useCases.NewListArticlesUseCase(articleRepo),
useCases.NewDeleteArticleUseCase(articleRepo),
)
hasher := hashing.NewBcryptHasher()
authHandler := auth.NewHandler(
token,
useCases.NewGetUserUseCase(userRepo),
useCases.NewListUsersUseCase(userRepo),
useCases.NewCreateUserUseCase(userRepo),
useCases.NewCreateUserUseCase(userRepo, hasher),
useCases.NewAuthenticateUserUseCase(userRepo, hasher),
)
r := chi.NewRouter()
@@ -235,11 +237,11 @@ func TestNotFoundErrorsHttp(t *testing.T) {
newAliceUserRepo := func() *mockUserRepo {
repo := newMockUserRepo()
hashed, err := bcrypt.GenerateFromPassword([]byte("hunter2example"), 12)
hashed, err := hashing.NewBcryptHasher().Hash("hunter2example")
if err != nil {
t.Fatalf("failed to hash password: %v", err)
}
repo.users[1] = domainUser.User{ID: 1, Username: "alice", Password: string(hashed)}
repo.users[1] = domainUser.User{ID: 1, Username: "alice", Password: hashed}
return repo
}