From 9e331dd459723c525b761143b92e555a3c67405a Mon Sep 17 00:00:00 2001 From: yaemiku Date: Sun, 15 Feb 2026 05:01:16 +0100 Subject: [PATCH] Move LDAP store out of the global variable --- cmd/gonic/gonic.go | 5 +++-- ldap/ldap.go | 9 +++------ server/ctrladmin/ctrl.go | 4 +++- server/ctrladmin/handlers_raw.go | 2 +- server/ctrlsubsonic/ctrl.go | 8 ++++---- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cmd/gonic/gonic.go b/cmd/gonic/gonic.go index fbb0831..fcc03b5 100644 --- a/cmd/gonic/gonic.go +++ b/cmd/gonic/gonic.go @@ -85,6 +85,7 @@ func main() { confExcludePattern := flag.String("exclude-pattern", "", "regex pattern to exclude files from scan (optional)") var ldapConfig ldap.Config + var ldapStore = make(ldap.LDAPStore) flag.StringVar(&ldapConfig.BindUser, "ldap-bind-user", "", "the bind user to bind to LDAP with (required for LDAP)") flag.StringVar(&ldapConfig.BindPass, "ldap-bind-pass", "", "the password of the LDAP bind user (required for LDAP)") flag.StringVar(&ldapConfig.BaseDN, "ldap-base-dn", "", "the base DN for LDAP objects (required for LDAP)") @@ -271,11 +272,11 @@ func main() { return url.String() } - ctrlAdmin, err := ctrladmin.New(dbc, sessDB, scannr, podcast, lastfmClient, resolveProxyPath, ldapConfig) + ctrlAdmin, err := ctrladmin.New(dbc, sessDB, scannr, podcast, lastfmClient, resolveProxyPath, ldapConfig, ldapStore) if err != nil { log.Panicf("error creating admin controller: %v\n", err) } - ctrlSubsonic, err := ctrlsubsonic.New(dbc, scannr, musicPaths, *confPodcastPath, cacheDirAudio, cacheDirCovers, jukebx, playlistStore, scrobblers, podcast, transcoder, lastfmClient, artistInfoCache, albumInfoCache, tagReader, resolveProxyPath, ldapConfig) + ctrlSubsonic, err := ctrlsubsonic.New(dbc, scannr, musicPaths, *confPodcastPath, cacheDirAudio, cacheDirCovers, jukebx, playlistStore, scrobblers, podcast, transcoder, lastfmClient, artistInfoCache, albumInfoCache, tagReader, resolveProxyPath, ldapConfig, ldapStore) if err != nil { log.Panicf("error creating subsonic controller: %v\n", err) } diff --git a/ldap/ldap.go b/ldap/ldap.go index 945535c..acb4f43 100644 --- a/ldap/ldap.go +++ b/ldap/ldap.go @@ -11,9 +11,6 @@ import ( "github.com/go-ldap/ldap/v3" ) -//nolint:gochecknoglobals -var ldapStore = make(LDAPStore) - // LDAPStore maps users to a cached password type LDAPStore map[string]CachedLDAPpassword @@ -71,12 +68,12 @@ func (c Config) IsSetup() bool { return c.FQDN != "" } -func CheckLDAPcreds(username string, password string, dbc *db.DB, config Config) (bool, error) { +func CheckLDAPcreds(username string, password string, dbc *db.DB, config Config, store LDAPStore) (bool, error) { if !config.IsSetup() { return false, nil } - if ldapStore.IsValid(username, password) { + if store.IsValid(username, password) { log.Println("Password authenticated via cache!") return true, nil } @@ -107,7 +104,7 @@ func CheckLDAPcreds(username string, password string, dbc *db.DB, config Config) if err == nil { // Authentication was OK - ldapStore.Add(username, password) + store.Add(username, password) return true, nil } diff --git a/server/ctrladmin/ctrl.go b/server/ctrladmin/ctrl.go index b1de928..e174a5f 100644 --- a/server/ctrladmin/ctrl.go +++ b/server/ctrladmin/ctrl.go @@ -50,11 +50,12 @@ type Controller struct { lastfmClient *lastfm.Client resolveProxyPath ProxyPathResolver ldapConfig ldap.Config + ldapStore ldap.LDAPStore } type ProxyPathResolver func(in string) string -func New(dbc *db.DB, sessDB *gormstore.Store, scanner *scanner.Scanner, podcasts *podcast.Podcasts, lastfmClient *lastfm.Client, resolveProxyPath ProxyPathResolver, ldapConfig ldap.Config) (*Controller, error) { +func New(dbc *db.DB, sessDB *gormstore.Store, scanner *scanner.Scanner, podcasts *podcast.Podcasts, lastfmClient *lastfm.Client, resolveProxyPath ProxyPathResolver, ldapConfig ldap.Config, ldapStore ldap.LDAPStore) (*Controller, error) { c := Controller{ ServeMux: http.NewServeMux(), @@ -65,6 +66,7 @@ func New(dbc *db.DB, sessDB *gormstore.Store, scanner *scanner.Scanner, podcasts lastfmClient: lastfmClient, resolveProxyPath: resolveProxyPath, ldapConfig: ldapConfig, + ldapStore: ldapStore, } resp := respHandler(adminui.TemplatesFS, resolveProxyPath) diff --git a/server/ctrladmin/handlers_raw.go b/server/ctrladmin/handlers_raw.go index 8d496f1..4099651 100644 --- a/server/ctrladmin/handlers_raw.go +++ b/server/ctrladmin/handlers_raw.go @@ -21,7 +21,7 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) { } if c.ldapConfig.IsSetup() { - ok, err := ldap.CheckLDAPcreds(username, password, c.dbc, c.ldapConfig) + ok, err := ldap.CheckLDAPcreds(username, password, c.dbc, c.ldapConfig, c.ldapStore) if err != nil { log.Println("Failed to check LDAP credentials:", err) sessAddFlashW(session, []string{"failed to check LDAP credentials"}) diff --git a/server/ctrlsubsonic/ctrl.go b/server/ctrlsubsonic/ctrl.go index 511290e..4243353 100644 --- a/server/ctrlsubsonic/ctrl.go +++ b/server/ctrlsubsonic/ctrl.go @@ -74,7 +74,7 @@ type Controller struct { resolveProxyPath ProxyPathResolver } -func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, 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) (*Controller, error) { +func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, 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 +100,7 @@ func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, podcastsPa chain := handlerutil.Chain( withParams, withRequiredParams, - withUser(dbc, ldapConfig), + withUser(dbc, ldapConfig, ldapStore), ) chainRaw := handlerutil.Chain( chain, @@ -231,7 +231,7 @@ func withRequiredParams(next http.Handler) http.Handler { }) } -func withUser(dbc *db.DB, ldapConfig ldap.Config) 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) @@ -272,7 +272,7 @@ func withUser(dbc *db.DB, ldapConfig ldap.Config) handlerutil.Middleware { // Complete auth using LDAP log.Println("Authenticating using LDAP ...") - ok, err := ldap.CheckLDAPcreds(username, password, dbc, ldapConfig) + 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"))