Made withUser return a middleware and take dbc

This commit is contained in:
Daniel Hall
2024-04-07 09:40:25 -07:00
parent 6d7c605578
commit 6eddc2a1a9
+8 -6
View File
@@ -94,7 +94,7 @@ func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, podcastsPa
chain := handlerutil.Chain( chain := handlerutil.Chain(
withParams, withParams,
withRequiredParams, withRequiredParams,
c.withUser, withUser(dbc),
) )
chainRaw := handlerutil.Chain( chainRaw := handlerutil.Chain(
chain, chain,
@@ -224,7 +224,8 @@ func withRequiredParams(next http.Handler) http.Handler {
}) })
} }
func (c *Controller) withUser(next http.Handler) http.Handler { func withUser(dbc *db.DB) handlerutil.Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
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
@@ -240,7 +241,7 @@ func (c *Controller) withUser(next http.Handler) http.Handler {
"please provide `t` and `s`, or just `p`")) "please provide `t` and `s`, or just `p`"))
return return
} }
user := c.dbc.GetUserByName(username) user := dbc.GetUserByName(username)
var credsOk bool var credsOk bool
if tokenAuth && user != nil { if tokenAuth && user != nil {
@@ -249,9 +250,9 @@ func (c *Controller) withUser(next http.Handler) http.Handler {
credsOk = checkCredsBasic(user.Password, password) credsOk = checkCredsBasic(user.Password, password)
} }
if !credsOk { if !credsOk {
// Because internal authentication failed, we can now try to use LDAP, if // Because internal authentication failed, we can now try to use LDAP,
// it was enabled by the user. // if it was enabled by the user.
ok, err := ldap.CheckLDAPcreds(username, password, c.dbc) ok, err := ldap.CheckLDAPcreds(username, password, dbc)
if err != nil { if err != nil {
_ = writeResp(w, r, spec.NewError(40, err.Error())) _ = writeResp(w, r, spec.NewError(40, err.Error()))
return return
@@ -265,6 +266,7 @@ func (c *Controller) withUser(next http.Handler) http.Handler {
withUser := context.WithValue(r.Context(), CtxUser, user) withUser := context.WithValue(r.Context(), CtxUser, user)
next.ServeHTTP(w, r.WithContext(withUser)) next.ServeHTTP(w, r.WithContext(withUser))
}) })
}
} }
func slow(next http.Handler) http.Handler { func slow(next http.Handler) http.Handler {