2019-05-13 09:19:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-05-22 14:24:53 +02:00
|
|
|
"crypto/rand"
|
2019-05-13 09:19:33 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2019-08-05 14:22:52 +02:00
|
|
|
"forge.cadoles.com/Cadoles/cadoles-profile/cmd/server/config"
|
|
|
|
"forge.cadoles.com/Cadoles/cadoles-profile/ldap"
|
2019-05-13 09:19:33 +02:00
|
|
|
"forge.cadoles.com/wpetit/goweb/middleware/container"
|
|
|
|
"forge.cadoles.com/wpetit/goweb/service/session"
|
|
|
|
"forge.cadoles.com/wpetit/goweb/service/template"
|
|
|
|
"forge.cadoles.com/wpetit/goweb/static"
|
|
|
|
"forge.cadoles.com/wpetit/goweb/template/html"
|
|
|
|
"github.com/go-chi/chi"
|
2019-05-22 14:24:53 +02:00
|
|
|
"github.com/gorilla/csrf"
|
2019-05-13 09:19:33 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
ldapv3 "gopkg.in/ldap.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func mountRoutes(r *chi.Mux, config *config.Config) {
|
2019-05-22 14:24:53 +02:00
|
|
|
|
|
|
|
csrfSecret, err := generateRandomBytes(32)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while generating CSRF secret"))
|
|
|
|
}
|
|
|
|
csrfMiddleware := csrf.Protect(
|
|
|
|
csrfSecret,
|
|
|
|
csrf.Secure(false),
|
|
|
|
)
|
|
|
|
|
|
|
|
r.Use(csrfMiddleware)
|
|
|
|
|
2019-05-13 09:19:33 +02:00
|
|
|
r.Get("/login", serveLoginPage)
|
|
|
|
r.Post("/login", handleLoginForm)
|
2019-05-22 14:24:53 +02:00
|
|
|
|
2019-05-13 09:19:33 +02:00
|
|
|
r.Get("/logout", handleLogout)
|
|
|
|
r.Group(func(r chi.Router) {
|
|
|
|
r.Use(authMiddleware)
|
|
|
|
r.Get("/", serveHomePage)
|
|
|
|
r.Get("/profile", serveProfilePage)
|
2019-05-22 14:24:53 +02:00
|
|
|
r.Post("/profile/password", handlePasswordChange)
|
2019-05-13 09:19:33 +02:00
|
|
|
})
|
2019-05-22 14:24:53 +02:00
|
|
|
|
2019-05-13 09:19:33 +02:00
|
|
|
r.Get("/*", static.Dir(config.HTTP.PublicDir, "", r.NotFoundHandler()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveHomePage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, "/profile", http.StatusTemporaryRedirect)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveLoginPage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctn := container.Must(r.Context())
|
|
|
|
tmpl := template.Must(ctn)
|
2019-05-22 14:24:53 +02:00
|
|
|
data := template.Data{
|
|
|
|
csrf.TemplateTag: csrf.TemplateField(r),
|
|
|
|
}
|
|
|
|
if err := tmpl.RenderPage(w, "login.html.tmpl", data); err != nil {
|
2019-05-13 09:19:33 +02:00
|
|
|
panic(errors.Wrap(err, "error while rendering page"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleLogout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctn := container.Must(r.Context())
|
|
|
|
sess, err := session.Must(ctn).Get(w, r)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while retrieving session"))
|
|
|
|
}
|
|
|
|
if err := sess.Delete(w, r); err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while deleting session"))
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleLoginForm(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while parsing form"))
|
|
|
|
}
|
|
|
|
|
|
|
|
username := r.Form.Get("username")
|
|
|
|
password := r.Form.Get("password")
|
|
|
|
|
|
|
|
ctn := container.Must(r.Context())
|
|
|
|
ldapSrv := ldap.Must(ctn)
|
|
|
|
tmplSrv := template.Must(ctn)
|
|
|
|
conf := config.Must(ctn)
|
|
|
|
|
|
|
|
sess, err := session.Must(ctn).Get(w, r)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while retrieving session"))
|
|
|
|
}
|
|
|
|
|
|
|
|
renderInvalidCredentials := func() {
|
|
|
|
sess.AddFlash(session.FlashError, "Identifiants invalides.")
|
|
|
|
data := extendTemplateData(w, r, template.Data{
|
2019-05-22 14:24:53 +02:00
|
|
|
"Username": username,
|
|
|
|
csrf.TemplateTag: csrf.TemplateField(r),
|
2019-05-13 09:19:33 +02:00
|
|
|
})
|
|
|
|
if err := tmplSrv.RenderPage(w, "login.html.tmpl", data); err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while rendering page"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 14:24:53 +02:00
|
|
|
if username == "" || password == "" {
|
|
|
|
renderInvalidCredentials()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:19:33 +02:00
|
|
|
results, err := ldapSrv.Search(
|
|
|
|
ldap.EscapeFilter(conf.LDAP.UserSearchFilterPattern, username),
|
|
|
|
ldap.WithBaseDN(conf.LDAP.BaseDN),
|
|
|
|
ldap.WithScope(ldapv3.ScopeWholeSubtree),
|
|
|
|
ldap.WithSizeLimit(1),
|
|
|
|
ldap.WithAttributes("dn"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while searching ldap entry"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(results.Entries) == 0 {
|
|
|
|
renderInvalidCredentials()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userDN := results.Entries[0].DN
|
|
|
|
|
|
|
|
log.Printf("authenticating user '%s' with DN '%s'", username, userDN)
|
|
|
|
|
|
|
|
if err := ldapSrv.Bind(userDN, password); err != nil {
|
|
|
|
|
|
|
|
// If the provided credentials are invalid, add flash message and rerender
|
|
|
|
// the page
|
|
|
|
if ldapv3.IsErrorWithCode(errors.Cause(err), ldapv3.LDAPResultInvalidCredentials) {
|
|
|
|
renderInvalidCredentials()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ldapv3.IsErrorWithCode(errors.Cause(err), ldapv3.LDAPResultNoSuchObject) {
|
|
|
|
renderInvalidCredentials()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(errors.Wrap(err, "error while binding ldap connection"))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("successful authentication for user with DN '%s'", userDN)
|
|
|
|
|
|
|
|
sess.Set("password", password)
|
|
|
|
sess.Set("dn", userDN)
|
|
|
|
sess.AddFlash(session.FlashSuccess, "Bienvenue !")
|
|
|
|
|
|
|
|
if err := sess.Save(w, r); err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while saving session"))
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveProfilePage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctn := container.Must(r.Context())
|
|
|
|
|
|
|
|
sess, err := session.Must(ctn).Get(w, r)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while retrieving session"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ldapSrv := ldap.Must(ctn)
|
|
|
|
|
|
|
|
conn, err := ldapSrv.Connect()
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while connecting to ldap server"))
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
userDN := sess.Get("dn").(string)
|
|
|
|
password := sess.Get("password").(string)
|
|
|
|
|
|
|
|
if err := ldapSrv.BindConn(conn, userDN, password); err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while binding ldap connection"))
|
|
|
|
}
|
|
|
|
|
|
|
|
results, err := ldapSrv.Search(
|
|
|
|
"(&)",
|
|
|
|
ldap.WithBaseDN(userDN),
|
|
|
|
ldap.WithScope(ldapv3.ScopeBaseObject),
|
|
|
|
ldap.WithSizeLimit(1),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while searching ldap entry"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(results.Entries) == 0 {
|
|
|
|
panic(errors.Errorf("could not retrieve ldap entry '%s'", userDN))
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl := template.Must(ctn)
|
2019-05-31 13:46:17 +02:00
|
|
|
config := config.Must(ctn)
|
2019-05-13 09:19:33 +02:00
|
|
|
|
|
|
|
data := extendTemplateData(w, r, template.Data{
|
|
|
|
"EntryAttributes": results.Entries[0].Attributes,
|
2019-05-22 14:24:53 +02:00
|
|
|
csrf.TemplateTag: csrf.TemplateField(r),
|
2019-05-31 13:46:17 +02:00
|
|
|
"Contact": config.Contact,
|
2019-05-13 09:19:33 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err := tmpl.RenderPage(w, "profile.html.tmpl", data); err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while rendering page"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 14:24:53 +02:00
|
|
|
func handlePasswordChange(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
ctn := container.Must(r.Context())
|
|
|
|
tmpl := template.Must(ctn)
|
|
|
|
|
|
|
|
sess, err := session.Must(ctn).Get(w, r)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while retrieving session"))
|
|
|
|
}
|
|
|
|
|
|
|
|
renderError := func(message string) {
|
|
|
|
sess.AddFlash(session.FlashError, message)
|
|
|
|
data := extendTemplateData(w, r, template.Data{
|
|
|
|
csrf.TemplateTag: csrf.TemplateField(r),
|
|
|
|
})
|
|
|
|
if err := tmpl.RenderPage(w, "profile.html.tmpl", data); err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while rendering page"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while parsing form"))
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPassword := r.Form.Get("currentPassword")
|
|
|
|
|
|
|
|
if currentPassword == "" {
|
|
|
|
renderError("Vous devez renseigner votre mot de passe actuel.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentPassword == "" {
|
|
|
|
renderError("Vous devez renseigner votre mot de passe actuel.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
password := sess.Get("password").(string)
|
|
|
|
if currentPassword != password {
|
|
|
|
renderError("Votre mot de passe est invalide.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newPassword := r.Form.Get("newPassword")
|
|
|
|
newPasswordConfirm := r.Form.Get("newPasswordConfirm")
|
|
|
|
|
|
|
|
if newPassword == "" {
|
|
|
|
renderError("Vous devez renseigner un nouveau mot de passe.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if newPassword != newPasswordConfirm {
|
|
|
|
renderError("La confirmation de votre mot de passe n'est pas identique à votre nouveau mot de passe.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ldapSrv := ldap.Must(ctn)
|
|
|
|
|
|
|
|
conn, err := ldapSrv.Connect()
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while connecting to ldap server"))
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
userDN := sess.Get("dn").(string)
|
|
|
|
|
|
|
|
if err := ldapSrv.BindConn(conn, userDN, password); err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while binding ldap connection"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ldapSrv.ModifyPasswordConn(conn, userDN, password, newPassword); err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while modifying password"))
|
|
|
|
}
|
|
|
|
|
|
|
|
sess.Set("password", newPassword)
|
|
|
|
sess.AddFlash(session.FlashSuccess, "Votre mot de passe a été modifié.")
|
|
|
|
|
|
|
|
if err := sess.Save(w, r); err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while saving session"))
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/profile", http.StatusSeeOther)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:19:33 +02:00
|
|
|
func authMiddleware(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctn := container.Must(r.Context())
|
|
|
|
sess, err := session.Must(ctn).Get(w, r)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while retrieving session"))
|
|
|
|
}
|
|
|
|
dn, ok := sess.Get("dn").(string)
|
|
|
|
if !ok || dn == "" {
|
|
|
|
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Data) template.Data {
|
|
|
|
ctn := container.Must(r.Context())
|
|
|
|
data, err := template.Extend(data,
|
|
|
|
html.WithFlashes(w, r, ctn),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "error while extending template data"))
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
2019-05-22 14:24:53 +02:00
|
|
|
|
|
|
|
func generateRandomBytes(n int) ([]byte, error) {
|
|
|
|
b := make([]byte, n)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|