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

@@ -0,0 +1,28 @@
package useCases
import (
"RenewCMS/internal/domain/hash"
"RenewCMS/internal/domain/user"
)
type AuthenticateUserUseCase struct {
userRepository user.Repository
hasher hash.Hasher
}
func NewAuthenticateUserUseCase(userRepository user.Repository, hasher hash.Hasher) *AuthenticateUserUseCase {
return &AuthenticateUserUseCase{userRepository, hasher}
}
func (a *AuthenticateUserUseCase) Authenticate(username string, password string) (user.User, error) {
dbUser, err := a.userRepository.GetByUsername(username)
if err != nil {
return user.User{}, err
}
if err := a.hasher.Compare(dbUser.Password, password); err != nil {
return user.User{}, user.ErrInvalidCredentials
}
return dbUser, nil
}
+16 -4
View File
@@ -1,6 +1,7 @@
package useCases
import (
"RenewCMS/internal/domain/hash"
"RenewCMS/internal/domain/user"
"time"
@@ -9,6 +10,7 @@ import (
type CreateUserUseCase struct {
userRepository user.Repository
hasher hash.Hasher
}
type CreateUserCommand struct {
@@ -17,20 +19,30 @@ type CreateUserCommand struct {
Email string
}
func NewCreateUserUseCase(userRepository user.Repository) *CreateUserUseCase {
return &CreateUserUseCase{userRepository}
func NewCreateUserUseCase(userRepository user.Repository, hasher hash.Hasher) *CreateUserUseCase {
return &CreateUserUseCase{userRepository, hasher}
}
func (g *CreateUserUseCase) CreateUser(createUser CreateUserCommand) (user.User, error) {
rawUuid := uuid.NewString()
hashedPassword, err := g.hasher.Hash(createUser.Password)
if err != nil {
return user.User{}, err
}
hashedVerificationCode, err := g.hasher.Hash(rawUuid)
if err != nil {
return user.User{}, err
}
createdUser, err := g.userRepository.Create(user.User{
Username: createUser.Username,
Password: createUser.Password,
Password: hashedPassword,
PasswordResetCode: "",
Email: createUser.Email,
IsVerified: false,
VerificationCode: rawUuid,
VerificationCode: hashedVerificationCode,
VerificationExpiration: time.Now().Add(2 * time.Hour),
})
if err != nil {
+17 -5
View File
@@ -1,13 +1,17 @@
package useCases
import "RenewCMS/internal/domain/user"
import (
"RenewCMS/internal/domain/hash"
"RenewCMS/internal/domain/user"
)
type UpdateUserUseCase struct {
userRepository user.Repository
hasher hash.Hasher
}
func NewUpdateUserUseCase(userRepository user.Repository) *UpdateUserUseCase {
return &UpdateUserUseCase{userRepository}
func NewUpdateUserUseCase(userRepository user.Repository, hasher hash.Hasher) *UpdateUserUseCase {
return &UpdateUserUseCase{userRepository, hasher}
}
func (g *UpdateUserUseCase) UpdateVerificationStatus(userId uint32, isVerified bool) (user.User, error) {
@@ -15,9 +19,17 @@ func (g *UpdateUserUseCase) UpdateVerificationStatus(userId uint32, isVerified b
}
func (g *UpdateUserUseCase) UpdatePasswordResetCode(userId uint32, code string) (user.User, error) {
return g.userRepository.UpdatePasswordResetCode(userId, code)
hashedCode, err := g.hasher.Hash(code)
if err != nil {
return user.User{}, err
}
return g.userRepository.UpdatePasswordResetCode(userId, hashedCode)
}
func (g *UpdateUserUseCase) UpdatePassword(userId uint32, password string) (user.User, error) {
return g.userRepository.UpdatePassword(userId, password)
hashedPassword, err := g.hasher.Hash(password)
if err != nil {
return user.User{}, err
}
return g.userRepository.UpdatePassword(userId, hashedPassword)
}