ldap login logic, subsonic api hex encrypted password
This commit is contained in:
No files matched your search
+2
-1
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/go-ldap/ldap/v3"
|
"github.com/go-ldap/ldap/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//nolint:gochecknoglobals
|
||||||
var ldapStore = make(LDAPStore)
|
var ldapStore = make(LDAPStore)
|
||||||
|
|
||||||
// LDAPStore maps users to a cached password
|
// LDAPStore maps users to a cached password
|
||||||
@@ -128,7 +129,7 @@ func createUserFromLDAP(username string, dbc *db.DB, config Config, l *ldap.Conn
|
|||||||
isAdmin := doesLDAPAdminExist(username, config, l)
|
isAdmin := doesLDAPAdminExist(username, config, l)
|
||||||
log.Println(username, isAdmin)
|
log.Println(username, isAdmin)
|
||||||
|
|
||||||
if doesLDAPUserExist(username, config, l) && !isAdmin {
|
if !doesLDAPUserExist(username, config, l) {
|
||||||
return errors.New("no such user")
|
return errors.New("no such user")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,13 +25,15 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to check LDAP credentials:", err)
|
log.Println("Failed to check LDAP credentials:", err)
|
||||||
sessAddFlashW(session, []string{"failed to check LDAP credentials"})
|
sessAddFlashW(session, []string{"failed to check LDAP credentials"})
|
||||||
} else if !ok {
|
|
||||||
sessAddFlashW(session, []string{"invalid username / password"})
|
|
||||||
}
|
|
||||||
|
|
||||||
sessLogSave(session, w, r)
|
sessLogSave(session, w, r)
|
||||||
http.Redirect(w, r, r.Referer(), http.StatusSeeOther)
|
http.Redirect(w, r, r.Referer(), http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
|
} 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 || user.Password != password {
|
} else if user == nil || user.Password != password {
|
||||||
sessAddFlashW(session, []string{"invalid username / password"})
|
sessAddFlashW(session, []string{"invalid username / password"})
|
||||||
sessLogSave(session, w, r)
|
sessLogSave(session, w, r)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.senan.xyz/gonic/db"
|
"go.senan.xyz/gonic/db"
|
||||||
@@ -231,18 +232,35 @@ func withUser(dbc *db.DB, ldapConfig ldap.Config) handlerutil.Middleware {
|
|||||||
params := r.Context().Value(CtxParams).(params.Params)
|
params := r.Context().Value(CtxParams).(params.Params)
|
||||||
// ignoring errors here, a middleware has already ensured they exist
|
// ignoring errors here, a middleware has already ensured they exist
|
||||||
username, _ := params.Get("u")
|
username, _ := params.Get("u")
|
||||||
password, _ := params.Get("p")
|
passwordHex, _ := params.Get("p")
|
||||||
token, _ := params.Get("t")
|
token, _ := params.Get("t")
|
||||||
salt, _ := params.Get("s")
|
salt, _ := params.Get("s")
|
||||||
|
|
||||||
passwordAuth := token == "" && salt == ""
|
passwordAuth := token == "" && salt == ""
|
||||||
tokenAuth := password == ""
|
tokenAuth := passwordHex == ""
|
||||||
if tokenAuth == passwordAuth {
|
if tokenAuth == passwordAuth {
|
||||||
_ = writeResp(w, r, spec.NewError(10,
|
_ = writeResp(w, r, spec.NewError(10,
|
||||||
"please provide `t` and `s`, or just `p`"))
|
"please provide `t` and `s`, or just `p`"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var password string
|
||||||
|
if passwordAuth && passwordHex != "" {
|
||||||
|
if strings.HasPrefix(passwordHex, "enc:") {
|
||||||
|
raw := strings.TrimPrefix(passwordHex, "enc:")
|
||||||
|
decoded, err := hex.DecodeString(raw)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed to decode hex password:", err)
|
||||||
|
_ = writeResp(w, r, spec.NewError(40, "invalid password encoding"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
password = string(decoded)
|
||||||
|
} else {
|
||||||
|
// if not prefixed with "enc:", treat as plain text
|
||||||
|
password = passwordHex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
user := dbc.GetUserByName(username)
|
user := dbc.GetUserByName(username)
|
||||||
|
|
||||||
if ldapConfig.IsSetup() {
|
if ldapConfig.IsSetup() {
|
||||||
|
|||||||
Reference in New Issue
Block a user