forked from G-Node/gin-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset.go
235 lines (203 loc) · 6.68 KB
/
reset.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) 2016, German Neuroinformatics Node (G-Node)
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted under the terms of the BSD License. See
// LICENSE file in the root of the Project.
package web
import (
"fmt"
"net/http"
"github.com/G-Node/gin-auth/conf"
"github.com/G-Node/gin-auth/data"
"github.com/G-Node/gin-auth/util"
)
type credentialData struct {
Credential string
ErrMessage string
}
// ResetInitPage provides an input form for resetting an account password
func ResetInitPage(w http.ResponseWriter, r *http.Request) {
credData := &credentialData{}
tmpl := conf.MakeTemplate("resetinit.html")
w.Header().Add("Cache-Control", "no-store")
w.Header().Add("Content-Type", "text/html")
err := tmpl.ExecuteTemplate(w, "layout", credData)
if err != nil {
panic(err)
}
}
// ResetInit checks whether a provided login or e-mail address
// belongs to a non-disabled account. If this is the case, the corresponding
// account is updated with a password reset code and an email containing
// the code is sent to the e-mail address of the account.
func ResetInit(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "no-store")
w.Header().Add("Content-Type", "text/html")
credData := &credentialData{}
err := util.ReadFormIntoStruct(r, credData, true)
if err != nil {
panic(err)
}
if credData.Credential == "" {
credData.ErrMessage = "Please enter your login or e-mail address"
tmpl := conf.MakeTemplate("resetinit.html")
w.Header().Add("Warning", credData.ErrMessage)
err = tmpl.ExecuteTemplate(w, "layout", credData)
if err != nil {
panic(err)
}
return
}
account, ok := data.SetPasswordReset(credData.Credential)
if !ok {
credData.ErrMessage = "Invalid login or e-mail address"
tmpl := conf.MakeTemplate("resetinit.html")
w.Header().Add("Warning", credData.ErrMessage)
err = tmpl.ExecuteTemplate(w, "layout", credData)
if err != nil {
panic(err)
}
return
}
tmplFields := &struct {
From string
To string
Subject string
BaseUrl string
Code string
}{}
tmplFields.From = conf.GetSmtpCredentials().From
tmplFields.To = account.Email
tmplFields.Subject = "Your GIN Account Password Reset Request"
tmplFields.BaseUrl = conf.GetServerConfig().BaseURL
tmplFields.Code = account.ResetPWCode.String
content := util.MakeEmailTemplate("emailreset.txt", tmplFields)
email := &data.Email{}
err = email.Create(util.NewStringSet(account.Email), content.Bytes())
if err != nil {
msg := "An error occurred trying to send password reset e-mail. Please try again later."
PrintErrorHTML(w, r, msg, http.StatusInternalServerError)
return
}
head := "Success!"
message := "An e-mail with a password reset token has been sent to your e-mail address. "
message += "Please follow the contained link to reset your password. "
message += "Please note that your account will stay deactivated until your password reset has been completed."
info := struct {
Header string
Message string
}{head, message}
tmpl := conf.MakeTemplate("success.html")
err = tmpl.ExecuteTemplate(w, "layout", info)
if err != nil {
panic(err)
}
}
// ResetPage checks whether a password reset code submitted by request URI query exists and is still valid.
// Display enter password form if valid, an error message otherwise.
func ResetPage(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
PrintErrorHTML(w, r, "Request was malformed", http.StatusBadRequest)
return
}
code := r.Form.Get("reset_code")
if code == "" {
PrintErrorHTML(w, r, "Request was malformed", http.StatusBadRequest)
return
}
_, exists := data.GetAccountByResetPWCode(code)
if !exists {
PrintErrorHTML(w, r, "Your request is invalid or outdated. Please request a new reset code.",
http.StatusNotFound)
return
}
hidden := &struct {
ResetCode string
*util.ValidationError
}{code, &util.ValidationError{}}
tmpl := conf.MakeTemplate("reset.html")
w.Header().Add("Cache-Control", "no-store")
w.Header().Add("Content-Type", "text/html")
err = tmpl.ExecuteTemplate(w, "layout", hidden)
if err != nil {
panic(err)
}
}
// Reset checks whether a submitted password reset code exists and is still valid. It further checks,
// whether posted password and confirm password are identical and updates the account associated with
// the password reset code with the new password. This update further removes any existing
// password reset and account activation codes rendering the account active.
func Reset(w http.ResponseWriter, r *http.Request) {
formData := &struct {
ResetCode string
Password string
PasswordControl string
*util.ValidationError
}{}
err := util.ReadFormIntoStruct(r, formData, true)
if err != nil {
panic(err)
}
account, exists := data.GetAccountByResetPWCode(formData.ResetCode)
if !exists {
PrintErrorHTML(w, r, "Your request is invalid or outdated. Please request a new reset code.",
http.StatusNotFound)
return
}
formData.ValidationError = &util.ValidationError{FieldErrors: make(map[string]string)}
if formData.Password != formData.PasswordControl {
formData.FieldErrors["password"] = "Provided password did not match password control"
formData.Message = formData.FieldErrors["password"]
}
if formData.Password == "" || formData.PasswordControl == "" {
formData.FieldErrors["password"] = "Please enter password and password control"
formData.Message = formData.FieldErrors["password"]
}
if len(formData.Password) > 512 || len(formData.PasswordControl) > 512 {
formData.FieldErrors["password"] =
fmt.Sprintf("Entry too long, please shorten to %d characters", 512)
formData.Message = formData.FieldErrors["password"]
}
if formData.FieldErrors["password"] != "" {
formData.Password = ""
formData.PasswordControl = ""
tmpl := conf.MakeTemplate("reset.html")
w.Header().Add("Cache-Control", "no-store")
w.Header().Add("Content-Type", "text/html")
w.Header().Add("Warning", formData.Message)
err := tmpl.ExecuteTemplate(w, "layout", formData)
if err != nil {
panic(err)
}
return
}
err = account.UpdatePassword(formData.Password)
if err != nil {
panic(err)
}
account.ResetPWCode.Valid = false
err = account.Update()
if err != nil {
panic(err)
}
err = account.RemoveActivationCode()
if err != nil {
panic(err)
}
head := "Your password has been reset!"
message := "You can now login using your new password."
info := struct {
Header string
Message string
}{head, message}
tmpl := conf.MakeTemplate("success.html")
w.Header().Add("Cache-Control", "no-store")
w.Header().Add("Content-Type", "text/html")
err = tmpl.ExecuteTemplate(w, "layout", info)
if err != nil {
panic(err)
}
}