Continue reset form on bad entry

This commit is contained in:
nemunaire 2018-12-31 01:00:11 +01:00
parent 7226e9f1e2
commit f819320f5a
2 changed files with 21 additions and 12 deletions

View File

@ -12,20 +12,24 @@ func resetPassword(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "lost", http.StatusFound)
}
base := map[string]interface{}{
"login": r.URL.Query().Get("l"),
"token": strings.Replace(r.URL.Query().Get("t"), " ", "+", -1),
}
if r.Method != "POST" {
displayTmpl(w, "reset.html", map[string]interface{}{
"login": r.URL.Query().Get("l"),
"token": strings.Replace(r.URL.Query().Get("t"), " ", "+", -1),
})
displayTmpl(w, "reset.html", base)
return
}
// Check the two new passwords are identical
if r.PostFormValue("newpassword") != r.PostFormValue("new2password") {
displayTmplError(w, http.StatusNotAcceptable, "reset.html", map[string]interface{}{"error": "New passwords are not identical. Please retry."})
base["error"] = "New passwords are not identical. Please retry."
displayTmplError(w, http.StatusNotAcceptable, "reset.html", base)
return
} else if err := checkPasswdConstraint(r.PostFormValue("newpassword")); err != nil {
displayTmplError(w, http.StatusNotAcceptable, "reset.html", map[string]interface{}{"error": "The password you chose doesn't respect all constraints: " + err.Error()})
base["error"] = "The password you chose doesn't respect all constraints: " + err.Error()
displayTmplError(w, http.StatusNotAcceptable, "reset.html", base)
return
}
@ -33,7 +37,8 @@ func resetPassword(w http.ResponseWriter, r *http.Request) {
conn, err := myLDAP.Connect()
if err != nil || conn == nil {
log.Println(err)
displayTmplError(w, http.StatusInternalServerError, "reset.html", map[string]interface{}{"error": err.Error()})
base["error"] = err.Error()
displayTmplError(w, http.StatusInternalServerError, "reset.html", base)
return
}
@ -41,7 +46,8 @@ func resetPassword(w http.ResponseWriter, r *http.Request) {
err = conn.ServiceBind()
if err != nil {
log.Println(err)
displayTmplError(w, http.StatusInternalServerError, "reset.html", map[string]interface{}{"error": err.Error()})
base["error"] = err.Error()
displayTmplError(w, http.StatusInternalServerError, "reset.html", base)
return
}
@ -49,20 +55,23 @@ func resetPassword(w http.ResponseWriter, r *http.Request) {
dn, err := conn.SearchDN(r.PostFormValue("login"))
if err != nil {
log.Println(err)
displayTmplError(w, http.StatusInternalServerError, "reset.html", map[string]interface{}{"error": err.Error()})
base["error"] = err.Error()
displayTmplError(w, http.StatusInternalServerError, "reset.html", base)
return
}
// Check token validity (allow current token + last one)
if conn.genToken(dn, false) != r.PostFormValue("token") && conn.genToken(dn, true) != r.PostFormValue("token") {
displayTmplError(w, http.StatusNotAcceptable, "reset.html", map[string]interface{}{"error": "Token invalid, please retry the lost password procedure. Please note that our token expires after 1 hour."})
base["error"] = "Token invalid, please retry the lost password procedure. Please note that our token expires after 1 hour."
displayTmplError(w, http.StatusNotAcceptable, "reset.html", base)
return
}
// Replace the password by the new given
if err := conn.ChangePassword(dn, r.PostFormValue("newpassword")); err != nil {
log.Println(err)
displayTmplError(w, http.StatusInternalServerError, "reset.html", map[string]interface{}{"error": err.Error()})
base["error"] = err.Error()
displayTmplError(w, http.StatusInternalServerError, "reset.html", base)
return
}

View File

@ -1,7 +1,7 @@
{{template "header"}}
<h1 class="display-4">Forgot your password? <small class="text-muted">Define a new one!</small></h1>
<form method="post" action="reset">
<form method="post" action="reset?l={{ .login }}&t={{ .token }}">
{{if .error}}<div class="alert alert-danger" role="alert">{{.error}}</div>{{end}}
<div class="form-group">
<input required="" class="form-control" id="input_0" type="text" placeholder="Email" value="{{ .login }}" disabled="">