2018-01-26 12:38:19 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"html/template"
|
|
|
|
|
2018-06-16 18:58:13 +02:00
|
|
|
"github.com/dchest/captcha"
|
|
|
|
|
2018-01-26 12:38:19 +01:00
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type route struct {
|
|
|
|
pattern *regexp.Regexp
|
|
|
|
verb string
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2018-01-27 20:19:44 +01:00
|
|
|
// RegexpHandler is used for http handler to bind using regular expressions
|
2018-01-26 12:38:19 +01:00
|
|
|
type RegexpHandler struct {
|
|
|
|
routes []*route
|
|
|
|
}
|
|
|
|
|
2018-01-27 20:19:44 +01:00
|
|
|
// Handler binds http handler on RegexpHandler
|
2018-01-26 12:38:19 +01:00
|
|
|
func (h *RegexpHandler) Handler(pattern *regexp.Regexp, verb string, handler http.Handler) {
|
|
|
|
h.routes = append(h.routes, &route{pattern, verb, handler})
|
|
|
|
}
|
|
|
|
|
2018-01-27 20:19:44 +01:00
|
|
|
// HandleFunc binds http handler function on RegexpHandler
|
2018-01-26 12:38:19 +01:00
|
|
|
func (h *RegexpHandler) HandleFunc(r string, v string, handler func(http.ResponseWriter, *http.Request)) {
|
|
|
|
re := regexp.MustCompile(r)
|
|
|
|
h.routes = append(h.routes, &route{re, v, http.HandlerFunc(handler)})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RegexpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
for _, route := range h.routes {
|
|
|
|
if route.pattern.MatchString(r.URL.Path) && route.verb == r.Method {
|
|
|
|
route.handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
type pageData struct {
|
2018-06-16 18:58:13 +02:00
|
|
|
Title string
|
|
|
|
Username string
|
|
|
|
Alerts map[string]string
|
|
|
|
CaptchaId string
|
2018-01-26 12:38:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeAssets : Serves the static assets
|
|
|
|
func ServeAssets(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.ServeFile(w, req, path.Join("static", req.URL.Path[1:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeIndex : Serves index page on GET request
|
|
|
|
func ServeIndex(w http.ResponseWriter, req *http.Request) {
|
2018-06-16 18:58:13 +02:00
|
|
|
p := &pageData{Title: getTitle(), CaptchaId: captcha.New()}
|
2018-01-26 12:38:19 +01:00
|
|
|
t, e := template.ParseFiles(path.Join("templates", "index.html"))
|
|
|
|
if e != nil {
|
|
|
|
log.Printf("Error parsing file %v\n", e)
|
|
|
|
} else {
|
|
|
|
t.Execute(w, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangePassword : Serves index page on POST request - executes the change
|
|
|
|
func ChangePassword(w http.ResponseWriter, req *http.Request) {
|
|
|
|
req.ParseForm()
|
|
|
|
un := ""
|
|
|
|
username := req.Form["username"]
|
|
|
|
oldPassword := req.Form["old-password"]
|
|
|
|
newPassword := req.Form["new-password"]
|
|
|
|
confirmPassword := req.Form["confirm-password"]
|
2018-06-16 18:58:13 +02:00
|
|
|
captchaID := req.Form["captchaId"]
|
|
|
|
captchaSolution := req.Form["captchaSolution"]
|
2018-01-26 12:38:19 +01:00
|
|
|
|
|
|
|
alerts := map[string]string{}
|
|
|
|
|
|
|
|
if len(username) < 1 || username[0] == "" {
|
2018-06-16 18:58:13 +02:00
|
|
|
alerts["error"] = "Username not specified."
|
2018-01-26 12:38:19 +01:00
|
|
|
} else {
|
|
|
|
un = username[0]
|
|
|
|
}
|
|
|
|
if len(oldPassword) < 1 || oldPassword[0] == "" {
|
2018-06-16 18:58:13 +02:00
|
|
|
alerts["error"] = "Old password not specified."
|
2018-01-26 12:38:19 +01:00
|
|
|
}
|
|
|
|
if len(newPassword) < 1 || newPassword[0] == "" {
|
2018-06-16 18:58:13 +02:00
|
|
|
alerts["error"] = "New password not specified."
|
2018-01-26 12:38:19 +01:00
|
|
|
}
|
|
|
|
if len(confirmPassword) < 1 || confirmPassword[0] == "" {
|
2018-06-16 18:58:13 +02:00
|
|
|
alerts["error"] = "Confirmation password not specified."
|
2018-01-26 12:38:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(confirmPassword) >= 1 && len(newPassword) >= 1 && strings.Compare(newPassword[0], confirmPassword[0]) != 0 {
|
2018-06-16 18:58:13 +02:00
|
|
|
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."
|
2018-01-26 12:38:19 +01:00
|
|
|
}
|
2018-06-16 18:58:13 +02:00
|
|
|
|
2018-01-26 12:38:19 +01:00
|
|
|
if len(alerts) == 0 {
|
|
|
|
client := NewLDAPClient()
|
|
|
|
if err := client.ModifyPassword(un, oldPassword[0], newPassword[0]); err != nil {
|
|
|
|
alerts["error"] = fmt.Sprintf("%v", err)
|
|
|
|
} else {
|
|
|
|
alerts["success"] = "Password successfuly changed"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 18:58:13 +02:00
|
|
|
p := &pageData{Title: getTitle(), Alerts: alerts, Username: un, CaptchaId: captcha.New()}
|
2018-01-26 12:38:19 +01:00
|
|
|
|
|
|
|
t, e := template.ParseFiles(path.Join("templates", "index.html"))
|
|
|
|
if e != nil {
|
|
|
|
log.Printf("Error parsing file %v\n", e)
|
|
|
|
} else {
|
|
|
|
t.Execute(w, p)
|
|
|
|
}
|
|
|
|
}
|