-
Notifications
You must be signed in to change notification settings - Fork 1
/
password.go
153 lines (126 loc) · 4.42 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
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
package auth
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
"github.com/kidstuff/auth/authmodel"
"net/http"
"strings"
)
// OverridePassword document: http://kidstuff.github.io/swagger/#!/default/users_user_id_password_override_put
func OverridePassword(authCtx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
return updatePassword(authCtx, req, true, false)
}
// ChangePassword document: http://kidstuff.github.io/swagger/#!/default/users_user_id_password_put
func ChangePassword(authCtx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
return updatePassword(authCtx, req, false, false)
}
// ResetPassword document: http://kidstuff.github.io/swagger/#!/default/users_user_id_password_reset_put
func ResetPassword(authCtx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
return updatePassword(authCtx, req, false, true)
}
func updatePassword(authCtx *AuthContext, req *http.Request, adminDoingThis, isResetting bool) (int, error) {
pwd := struct{ OldPwd, NewPwd, NewPwdRepeat, ResetCode string }{}
err := json.NewDecoder(req.Body).Decode(&pwd)
if err != nil {
return http.StatusBadRequest, err
}
req.Body.Close()
if pwd.NewPwd != pwd.NewPwdRepeat {
return http.StatusBadRequest, errors.New("kidstuff/auth: passsword mismatch")
}
u, stt, err := findUser(authCtx, req)
if err != nil {
return stt, err
}
if !adminDoingThis {
if isResetting {
if len(pwd.ResetCode) == 0 {
return http.StatusPreconditionFailed, ErrInvalidResetCode
}
if !u.ValidConfirmCode("password_reset", pwd.ResetCode, false, true) {
return http.StatusPreconditionFailed, ErrInvalidResetCode
}
} else {
if len(pwd.OldPwd) == 0 {
return http.StatusPreconditionFailed, ErrInvalidCredential
}
err := authCtx.Auth.ComparePassword(pwd.OldPwd, u.Pwd)
if err != nil {
return http.StatusPreconditionFailed, ErrInvalidCredential
}
}
}
err = authCtx.Auth.UpdateUserDetail(*u.Id, &pwd.NewPwd, nil, nil, u.ConfirmCodes, nil, nil)
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
func sendResetMail(ctx *AuthContext, id, email, code string) error {
mailSettings, err := ctx.Settings.GetMulti([]string{
"auth_full_path",
"auth_reset_email_subject",
"auth_reset_email_message",
"auth_email_from",
})
if err != nil {
return err
}
activeURL := fmt.Sprintf("%s/password/reset/%s?code=%s", mailSettings["auth_full_path"], id, code)
return ctx.Notifications.SendMail(ctx, mailSettings["auth_reset_email_subject"],
fmt.Sprintf(mailSettings["auth_reset_email_message"], activeURL),
mailSettings["auth_email_from"], email)
}
func CreatePasswordResetIssue(ctx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
inf := struct {
Resend bool
Email string
}{}
err := json.NewDecoder(req.Body).Decode(&inf)
req.Body.Close()
if err != nil || len(inf.Email) == 0 {
return http.StatusBadRequest, err
}
u, err := ctx.Auth.FindUserByEmail(inf.Email)
if err == authmodel.ErrNotFound {
return http.StatusPreconditionFailed, err
} else if err != nil {
return http.StatusInternalServerError, err
}
if len(u.ConfirmCodes["password_reset"]) > 0 && !inf.Resend {
return http.StatusNotAcceptable, errors.New("kidstuff/auth: password reset has been request")
}
if u.ConfirmCodes == nil {
u.ConfirmCodes = map[string]string{}
}
u.ConfirmCodes["password_reset"] = strings.Trim(base64.URLEncoding.
EncodeToString(securecookie.GenerateRandomKey(64)), "=")
err = ctx.Auth.UpdateUserDetail(*u.Id, nil, nil, nil, u.ConfirmCodes, nil, nil)
if err != nil {
return http.StatusInternalServerError, err
}
err = sendResetMail(ctx, *u.Id, *u.Email, u.ConfirmCodes["password_reset"])
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
func RedirectPasswordReset(ctx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
sid := mux.Vars(req)["user_id"]
if len(sid) == 0 || len(req.FormValue("code")) == 0 {
return http.StatusBadRequest, ErrInvalidCredential
}
mailSettings, err := ctx.Settings.GetMulti([]string{
"auth_reset_redirect",
})
if err != nil {
return http.StatusInternalServerError, err
}
urlStr := fmt.Sprintf(mailSettings["auth_reset_redirect"], sid, req.FormValue("code"))
http.Redirect(rw, req, urlStr, http.StatusSeeOther)
return http.StatusSeeOther, nil
}