-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.go
91 lines (70 loc) · 1.77 KB
/
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package models
import (
"errors"
"github.com/eirka/eirka-libs/config"
e "github.com/eirka/eirka-libs/errors"
"github.com/eirka/eirka-libs/user"
"github.com/eirka/eirka-libs/validate"
)
// PasswordModel contains information for initial account creation
type PasswordModel struct {
UID uint
Name string
OldPw string
NewPw string
NewHashed []byte
}
// IsValid will check struct validity
func (m *PasswordModel) IsValid() bool {
if m.UID == 0 || m.UID == 1 {
return false
}
if m.Name == "" {
return false
}
if m.OldPw == "" {
return false
}
if m.NewPw == "" {
return false
}
if m.NewHashed == nil {
return false
}
return true
}
// Validate will check the provided password
func (m *PasswordModel) Validate() (err error) {
// Validate new password input
newpassword := validate.Validate{Input: m.NewPw, Max: config.Settings.Limits.PasswordMaxLength, Min: config.Settings.Limits.PasswordMinLength}
if newpassword.IsEmpty() {
return e.ErrPasswordEmpty
} else if newpassword.MinLength() {
return e.ErrPasswordShort
} else if newpassword.MaxLength() {
return e.ErrPasswordLong
}
// Validate old password input
oldpassword := validate.Validate{Input: m.OldPw, Max: config.Settings.Limits.PasswordMaxLength, Min: config.Settings.Limits.PasswordMinLength}
if oldpassword.IsEmpty() {
return e.ErrPasswordEmpty
} else if oldpassword.MinLength() {
return e.ErrPasswordShort
} else if oldpassword.MaxLength() {
return e.ErrPasswordLong
}
return
}
// Update will update the password model
func (m *PasswordModel) Update() (err error) {
// check model validity
if !m.IsValid() {
return errors.New("PasswordModel is not valid")
}
// update user password
user.UpdatePassword(m.NewHashed, m.UID)
if err != nil {
return
}
return
}