-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler_set_password.go
45 lines (36 loc) · 1.01 KB
/
handler_set_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
package auth
import "github.com/boreq/errors"
type SetPassword struct {
Username string
Password string
}
type SetPasswordHandler struct {
passwordHasher PasswordHasher
transactionProvider TransactionProvider
}
func NewSetPasswordHandler(
passwordHasher PasswordHasher,
transactionProvider TransactionProvider,
) *SetPasswordHandler {
return &SetPasswordHandler{
passwordHasher: passwordHasher,
transactionProvider: transactionProvider,
}
}
func (h *SetPasswordHandler) Execute(cmd SetPassword) error {
if err := validate(cmd.Username, cmd.Password); err != nil {
return errors.Wrap(err, "invalid parameters")
}
passwordHash, err := h.passwordHasher.Hash(cmd.Password)
if err != nil {
return errors.Wrap(err, "hashing the password failed")
}
return h.transactionProvider.Write(func(r *TransactableRepositories) error {
u, err := r.Users.Get(cmd.Username)
if err != nil {
return errors.Wrap(err, "could not get the user")
}
u.Password = passwordHash
return r.Users.Put(*u)
})
}