Merge branch 'ldap_auth'

This commit is contained in:
2026-02-17 15:49:42 +01:00
10 changed files with 352 additions and 14 deletions

No files matched your search

+53 -8
View File
@@ -10,6 +10,7 @@ import (
"io"
"log"
"net/http"
"strings"
"time"
"go.senan.xyz/gonic/db"
@@ -18,6 +19,7 @@ import (
"go.senan.xyz/gonic/infocache/artistinfocache"
"go.senan.xyz/gonic/jukebox"
"go.senan.xyz/gonic/lastfm"
"go.senan.xyz/gonic/ldap"
"go.senan.xyz/gonic/playlist"
"go.senan.xyz/gonic/podcast"
"go.senan.xyz/gonic/scanner"
@@ -73,7 +75,7 @@ type Controller struct {
resolveProxyPath ProxyPathResolver
}
func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, defaultMusicPath string, podcastsPath string, cacheAudioPath string, cacheCoverPath string, jukebox *jukebox.Jukebox, playlistStore *playlist.Store, scrobblers []scrobble.Scrobbler, podcasts *podcast.Podcasts, transcoder transcode.Transcoder, lastFMClient *lastfm.Client, artistInfoCache *artistinfocache.ArtistInfoCache, albumInfoCache *albuminfocache.AlbumInfoCache, tagReader tags.Reader, resolveProxyPath ProxyPathResolver) (*Controller, error) {
func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, defaultMusicPath string, podcastsPath string, cacheAudioPath string, cacheCoverPath string, jukebox *jukebox.Jukebox, playlistStore *playlist.Store, scrobblers []scrobble.Scrobbler, podcasts *podcast.Podcasts, transcoder transcode.Transcoder, lastFMClient *lastfm.Client, artistInfoCache *artistinfocache.ArtistInfoCache, albumInfoCache *albuminfocache.AlbumInfoCache, tagReader tags.Reader, resolveProxyPath ProxyPathResolver, ldapConfig ldap.Config, ldapStore ldap.LDAPStore) (*Controller, error) {
c := Controller{
ServeMux: http.NewServeMux(),
@@ -100,7 +102,7 @@ func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, defaultMus
chain := handlerutil.Chain(
withParams,
withRequiredParams,
withUser(dbc),
withUser(dbc, ldapConfig, ldapStore),
)
chainRaw := handlerutil.Chain(
chain,
@@ -231,39 +233,82 @@ func withRequiredParams(next http.Handler) http.Handler {
})
}
func withUser(dbc *db.DB) handlerutil.Middleware {
func withUser(dbc *db.DB, ldapConfig ldap.Config, ldapStore ldap.LDAPStore) handlerutil.Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
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 user == nil {
_ = writeResp(w, r, spec.NewError(40,
"invalid username %q", username))
if ldapConfig.IsSetup() {
// Complete auth using LDAP
log.Println("Authenticating using LDAP ...")
ok, err := ldap.CheckLDAPcreds(username, password, dbc, ldapConfig, ldapStore)
if err != nil {
log.Println("Failed to check LDAP creds:", err)
_ = writeResp(w, r, spec.NewError(40, "invalid password"))
return
}
if !ok {
_ = writeResp(w, r, spec.NewError(40, "invalid password"))
return
}
withUser := context.WithValue(r.Context(), CtxUser, user)
next.ServeHTTP(w, r.WithContext(withUser))
return
}
log.Println("Authenticating using built-in ...")
if user == nil {
_ = writeResp(w, r, spec.NewError(40, "invalid password"))
return
}
var credsOk bool
if tokenAuth {
credsOk = checkCredsToken(user.Password, token, salt)
} else {
credsOk = checkCredsBasic(user.Password, password)
}
if !credsOk {
_ = writeResp(w, r, spec.NewError(40, "invalid password"))
return
}
withUser := context.WithValue(r.Context(), CtxUser, user)
next.ServeHTTP(w, r.WithContext(withUser))
})