diff --git a/ldap/ldap.go b/ldap/ldap.go index 720b9d4..06405ee 100644 --- a/ldap/ldap.go +++ b/ldap/ldap.go @@ -129,6 +129,7 @@ func createUserFromLDAP(username string, dbc *db.DB, config Config, l *ldap.Conn } isAdmin := doesLDAPAdminExist(username, config, l) + log.Println(username, isAdmin) if doesLDAPUserExist(username, config, l) && !isAdmin { return errors.New("no such user") diff --git a/server/ctrladmin/handlers_raw.go b/server/ctrladmin/handlers_raw.go index 14fe2b7..af41633 100644 --- a/server/ctrladmin/handlers_raw.go +++ b/server/ctrladmin/handlers_raw.go @@ -1,6 +1,7 @@ package ctrladmin import ( + "log" "net/http" "github.com/gorilla/sessions" @@ -11,27 +12,39 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) { session := r.Context().Value(CtxSession).(*sessions.Session) username := r.FormValue("username") password := r.FormValue("password") + user := c.dbc.GetUserByName(username) if username == "" || password == "" { sessAddFlashW(session, []string{"please provide username and password"}) sessLogSave(session, w, r) http.Redirect(w, r, r.Referer(), http.StatusSeeOther) return } - user := c.dbc.GetUserByName(username) - if user == nil || password != user.Password { - // Because internal authentication failed, we can now try to use LDAP, if - // it was enabled by the user. + + if c.ldapConfig.IsSetup() { ok, err := ldap.CheckLDAPcreds(username, password, c.dbc, c.ldapConfig) if err != nil { - sessAddFlashW(session, []string{err.Error()}) + log.Println("Failed to check LDAP credentials:", err) + sessAddFlashW(session, []string{"failed to check LDAP credentials"}) } else if !ok { sessAddFlashW(session, []string{"invalid username / password"}) - sessLogSave(session, w, r) http.Redirect(w, r, r.Referer(), http.StatusSeeOther) return } + } else if user == nil { + sessAddFlashW(session, []string{"invalid username / password"}) + sessLogSave(session, w, r) + http.Redirect(w, r, r.Referer(), http.StatusSeeOther) + return + } else if user.Password != password { + sessAddFlashW(session, []string{"invalid username / password"}) + sessLogSave(session, w, r) + http.Redirect(w, r, r.Referer(), http.StatusSeeOther) + return } + + user = c.dbc.GetUserByName(username) + // put the user name into the session. future endpoints after this one // are wrapped with WithUserSession() which will get the name from the // session and put the row into the request context