forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_api_reset_password.go
167 lines (155 loc) · 3.7 KB
/
d_api_reset_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package uadmin
import (
"net/http"
"time"
)
func dAPIResetPasswordHandler(w http.ResponseWriter, r *http.Request, s *Session) {
// Get parameters
uid := r.FormValue("uid")
email := r.FormValue("email")
otp := r.FormValue("otp")
password := r.FormValue("password")
// check if there is an email or a username
if email == "" && uid == "" {
w.WriteHeader(400)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "No email or uid",
})
// log the request
go func() {
log := &Log{}
if password != "" {
r.Form.Set("password", "*****")
}
log.PasswordReset("", log.Action.PasswordResetDenied(), r)
log.Save()
}()
return
}
// get user
user := User{}
if email != "" {
Get(&user, "email = ? AND active = ?", email, true)
} else {
Get(&user, "id = ? AND active = ?", uid, true)
}
// log the request
go func() {
log := &Log{}
if password != "" {
r.Form.Set("password", "*****")
}
log.PasswordReset(user.Username, log.Action.PasswordResetRequest(), r)
log.Save()
}()
// check if the user exists and active
if user.ID == 0 || (user.ExpiresOn != nil && user.ExpiresOn.After(time.Now())) {
w.WriteHeader(404)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "email or uid do not match any active user",
})
// log the request
go func() {
log := &Log{}
if password != "" {
r.Form.Set("password", "*****")
}
log.PasswordReset(user.Username, log.Action.PasswordResetDenied(), r)
log.Save()
}()
return
}
// If there is no otp, then we assume this is a request to send a password
// reset email
if otp == "" {
err := forgotPasswordHandler(&user, r, CustomResetPasswordLink, ResetPasswordMessage)
if err != nil {
w.WriteHeader(403)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": err.Error(),
})
// log the request
go func() {
log := &Log{}
if password != "" {
r.Form.Set("password", "*****")
}
log.PasswordReset(user.Username, log.Action.PasswordResetDenied(), r)
log.Save()
}()
return
}
// log the request
w.WriteHeader(http.StatusAccepted)
go func() {
log := &Log{}
if password != "" {
r.Form.Set("password", "*****")
}
r.Form.Set("reset-status", "Email was sent with the OTP")
log.PasswordReset(user.Username, log.Action.PasswordResetSuccessful(), r)
log.Save()
}()
ReturnJSON(w, r, map[string]interface{}{
"status": "ok",
})
return
}
// Since there is an OTP, we can check it and reset the password
// Check if there is a a new password
if password == "" {
w.WriteHeader(400)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "missing password",
})
// log the request
go func() {
log := &Log{}
if password != "" {
r.Form.Set("password", "*****")
}
log.PasswordReset("", log.Action.PasswordResetDenied(), r)
log.Save()
}()
return
}
// check OTP
if !user.VerifyOTPAtPasswordReset(otp) {
incrementInvalidLogins(r)
w.WriteHeader(401)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "invalid or expired OTP",
})
// log the request
go func() {
log := &Log{}
if password != "" {
r.Form.Set("password", "*****")
}
log.PasswordReset("", log.Action.PasswordResetDenied(), r)
log.Save()
}()
return
}
// reset the password
user.Password = password
user.Save()
// log the request
go func() {
log := &Log{}
if password != "" {
r.Form.Set("password", "*****")
}
r.Form.Set("reset-status", "Successfully changed the password")
log.PasswordReset("", log.Action.PasswordResetSuccessful(), r)
log.Save()
}()
ReturnJSON(w, r, map[string]interface{}{
"status": "ok",
})
}