Repository navigation
Expand file tree
/
Copy pathpassword.go
More file actions
173 lines (151 loc) · 4.32 KB
/
Copy pathpassword.go
File metadata and controls
173 lines (151 loc) · 4.32 KB
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
168
169
170
171
172
173
package web
import (
"context"
"errors"
"net/http"
"time"
"github.com/gorilla/csrf"
"github.com/mailgun/mailgun-go/v4"
"github.com/resonatecoop/id/log"
"github.com/resonatecoop/id/session"
pass "github.com/resonatecoop/id/util/password"
"github.com/resonatecoop/id/util/response"
"github.com/resonatecoop/user-api/model"
)
var (
ErrPasswordMismatch = errors.New("Password confirmation mismatch")
ErrInvalidPassword = errors.New("Invalid password")
)
func (s *Service) passwordUpdate(w http.ResponseWriter, r *http.Request) {
sessionService, _, user, err := s.passwordCommon(r)
if err != nil {
if r.Header.Get("Accept") == "application/json" {
response.Error(w, err.Error(), http.StatusBadRequest)
return
}
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// verify current password
if pass.VerifyPassword(user.Password.String, r.Form.Get("password")) != nil {
if r.Header.Get("Accept") == "application/json" {
response.Error(w, ErrInvalidPassword.Error(), http.StatusBadRequest)
return
}
err = sessionService.SetFlashMessage(&session.Flash{
Type: "Error",
Message: "Invalid password",
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
redirectWithQueryString("/web/account-settings", r.URL.Query(), w, r)
return
}
// compare new password and password confirmation
if r.Form.Get("password_new") != r.Form.Get("password_confirm") {
if r.Header.Get("Accept") == "application/json" {
response.Error(w, ErrPasswordMismatch.Error(), http.StatusBadRequest)
return
}
err = sessionService.SetFlashMessage(&session.Flash{
Type: "Error",
Message: ErrPasswordMismatch.Error(),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
redirectWithQueryString("/web/account-settings", r.URL.Query(), w, r)
return
}
w.Header().Set("X-CSRF-Token", csrf.Token(r))
// set new password
if s.oauthService.SetPassword(user, r.Form.Get("password_new")); err != nil {
if r.Header.Get("Accept") == "application/json" {
response.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = sessionService.SetFlashMessage(&session.Flash{
Type: "Error",
Message: err.Error(),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
redirectWithQueryString("/web/account-settings", r.URL.Query(), w, r)
return
}
if r.Header.Get("Accept") == "application/json" {
response.WriteJSON(w, map[string]interface{}{
"message": "Your password has been successfully changed",
"status": http.StatusOK,
}, http.StatusOK)
return
}
err = sessionService.SetFlashMessage(&session.Flash{
Type: "Info",
Message: "Your password has been successfully changed",
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Inform user by email password was changed
mg := mailgun.NewMailgun(s.cnf.Mailgun.Domain, s.cnf.Mailgun.Key)
sender := s.cnf.Mailgun.Sender
body := ""
email := model.NewOauthEmail(
user.Username,
"Password changed",
"password-changed",
)
subject := email.Subject
recipient := email.Recipient
message := mg.NewMessage(sender, subject, body, recipient)
message.SetTemplate(email.Template) // set mailgun template
err = message.AddTemplateVariable("email", recipient)
if err != nil {
log.ERROR.Print(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
// Send the message with a 10 second timeout
_, _, err = mg.Send(ctx, message)
if err != nil {
log.ERROR.Print(err)
}
redirectWithQueryString("/web/account-settings", r.URL.Query(), w, r)
}
func (s *Service) passwordCommon(r *http.Request) (
session.ServiceInterface,
*model.Client,
*model.User,
error,
) {
// Get the session service from the request context
sessionService, err := getSessionService(r)
if err != nil {
return nil, nil, nil, err
}
// Get the client from the request context
client, err := getClient(r)
if err != nil {
return nil, nil, nil, err
}
// Get the user session
userSession, err := sessionService.GetUserSession()
if err != nil {
return nil, nil, nil, err
}
// Fetch the user
user, err := s.oauthService.FindUserByUsername(
userSession.Username,
)
if err != nil {
return nil, nil, nil, err
}
return sessionService, client, user, nil
}