Add captcha support

This commit is contained in:
Nick Penkov
2018-06-16 18:58:13 +02:00
parent 6216de78e4
commit 0ff7ac96c8
7 changed files with 72 additions and 15 deletions

View File

@ -8,6 +8,8 @@ import (
"html/template"
"github.com/dchest/captcha"
"regexp"
"net/http"
@ -46,9 +48,10 @@ func (h *RegexpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
type pageData struct {
Title string
Username string
Alerts map[string]string
Title string
Username string
Alerts map[string]string
CaptchaId string
}
// ServeAssets : Serves the static assets
@ -58,7 +61,7 @@ func ServeAssets(w http.ResponseWriter, req *http.Request) {
// ServeIndex : Serves index page on GET request
func ServeIndex(w http.ResponseWriter, req *http.Request) {
p := &pageData{Title: getTitle()}
p := &pageData{Title: getTitle(), CaptchaId: captcha.New()}
t, e := template.ParseFiles(path.Join("templates", "index.html"))
if e != nil {
log.Printf("Error parsing file %v\n", e)
@ -75,27 +78,36 @@ func ChangePassword(w http.ResponseWriter, req *http.Request) {
oldPassword := req.Form["old-password"]
newPassword := req.Form["new-password"]
confirmPassword := req.Form["confirm-password"]
captchaID := req.Form["captchaId"]
captchaSolution := req.Form["captchaSolution"]
alerts := map[string]string{}
if len(username) < 1 || username[0] == "" {
alerts["error"] = "Username not specified.<br/>"
alerts["error"] = "Username not specified."
} else {
un = username[0]
}
if len(oldPassword) < 1 || oldPassword[0] == "" {
alerts["error"] = alerts["error"] + "Old password not specified.<br/>"
alerts["error"] = "Old password not specified."
}
if len(newPassword) < 1 || newPassword[0] == "" {
alerts["error"] = alerts["error"] + "New password not specified.<br/>"
alerts["error"] = "New password not specified."
}
if len(confirmPassword) < 1 || confirmPassword[0] == "" {
alerts["error"] = alerts["error"] + "Confirmation password not specified.<br/>"
alerts["error"] = "Confirmation password not specified."
}
if len(confirmPassword) >= 1 && len(newPassword) >= 1 && strings.Compare(newPassword[0], confirmPassword[0]) != 0 {
alerts["error"] = alerts["error"] + "New and confirmation passwords does not match.<br/>"
alerts["error"] = "New and confirmation passwords does not match. "
}
if len(captchaID) < 1 || captchaID[0] == "" ||
len(captchaSolution) < 1 || captchaSolution[0] == "" ||
!captcha.VerifyString(captchaID[0], captchaSolution[0]) {
alerts["error"] = "Wrong captcha."
}
if len(alerts) == 0 {
client := NewLDAPClient()
if err := client.ModifyPassword(un, oldPassword[0], newPassword[0]); err != nil {
@ -105,7 +117,7 @@ func ChangePassword(w http.ResponseWriter, req *http.Request) {
}
}
p := &pageData{Title: getTitle(), Alerts: alerts, Username: un}
p := &pageData{Title: getTitle(), Alerts: alerts, Username: un, CaptchaId: captcha.New()}
t, e := template.ParseFiles(path.Join("templates", "index.html"))
if e != nil {