ldap login logic, subsonic api hex encrypted password

This commit is contained in:
2025-09-21 08:02:10 +02:00
parent e5c192c83c
commit 6fba25ca0b
3 changed files with 28 additions and 7 deletions

No files matched your search

+6 -4
View File
@@ -25,13 +25,15 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Println("Failed to check LDAP credentials:", err)
sessAddFlashW(session, []string{"failed to check LDAP credentials"})
sessLogSave(session, w, r)
http.Redirect(w, r, r.Referer(), http.StatusSeeOther)
return
} else if !ok {
sessAddFlashW(session, []string{"invalid username / password"})
sessLogSave(session, w, r)
http.Redirect(w, r, r.Referer(), http.StatusSeeOther)
return
}
sessLogSave(session, w, r)
http.Redirect(w, r, r.Referer(), http.StatusSeeOther)
return
} else if user == nil || user.Password != password {
sessAddFlashW(session, []string{"invalid username / password"})
sessLogSave(session, w, r)
+20 -2
View File
@@ -10,6 +10,7 @@ import (
"io"
"log"
"net/http"
"strings"
"time"
"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)
// ignoring errors here, a middleware has already ensured they exist
username, _ := params.Get("u")
password, _ := params.Get("p")
passwordHex, _ := params.Get("p")
token, _ := params.Get("t")
salt, _ := params.Get("s")
passwordAuth := token == "" && salt == ""
tokenAuth := password == ""
tokenAuth := passwordHex == ""
if tokenAuth == passwordAuth {
_ = writeResp(w, r, spec.NewError(10,
"please provide `t` and `s`, or just `p`"))
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)
if ldapConfig.IsSetup() {