-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_user_password.go
76 lines (61 loc) · 2 KB
/
validate_user_password.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package usecase
import (
"context"
"github.com/tiagompalte/golang-clean-arch-template/internal/app/entity"
"github.com/tiagompalte/golang-clean-arch-template/internal/app/repository"
errPkg "github.com/tiagompalte/golang-clean-arch-template/internal/pkg/errors"
"github.com/tiagompalte/golang-clean-arch-template/pkg/crypto"
"github.com/tiagompalte/golang-clean-arch-template/pkg/errors"
)
type ValidateUserPasswordUseCase interface {
Execute(ctx context.Context, input ValidateUserPasswordInput) (entity.User, error)
}
type ValidateUserPasswordInput struct {
Email string
Password string
}
func (i ValidateUserPasswordInput) Validate() error {
aggrErr := errors.NewAggregatedError()
if i.Email == "" {
aggrErr.Add(errPkg.NewEmptyParameterError("email"))
}
if i.Password == "" {
aggrErr.Add(errPkg.NewEmptyParameterError("password"))
}
if aggrErr.Len() > 0 {
return errors.Wrap(aggrErr)
}
return nil
}
type ValidateUserPasswordUseCaseImpl struct {
userRepository repository.UserRepository
crypto crypto.Crypto
}
func NewValidateUserPasswordUseCaseImpl(userRepository repository.UserRepository, crypto crypto.Crypto) ValidateUserPasswordUseCase {
return ValidateUserPasswordUseCaseImpl{
userRepository: userRepository,
crypto: crypto,
}
}
func (u ValidateUserPasswordUseCaseImpl) Execute(ctx context.Context, input ValidateUserPasswordInput) (entity.User, error) {
err := input.Validate()
if err != nil {
return entity.User{}, errors.Wrap(err)
}
passEncrypted, err := u.userRepository.GetPassEncryptedByEmail(ctx, input.Email)
if err != nil {
return entity.User{}, errors.Wrap(errPkg.NewInvalidLoginError())
}
isValid, err := u.crypto.VerifyHash(ctx, input.Password, passEncrypted)
if err != nil {
return entity.User{}, errors.Wrap(err)
}
if !isValid {
return entity.User{}, errors.Wrap(errPkg.NewInvalidLoginError())
}
user, err := u.userRepository.FindByEmail(ctx, input.Email)
if err != nil {
return entity.User{}, errors.Wrap(err)
}
return user, nil
}