Made withUser return a middleware and take dbc
This commit is contained in:
+39
-37
@@ -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,47 +224,49 @@ func withRequiredParams(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) withUser(next http.Handler) http.Handler {
|
func withUser(dbc *db.DB) handlerutil.Middleware {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return func(next http.Handler) http.Handler {
|
||||||
params := r.Context().Value(CtxParams).(params.Params)
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// ignoring errors here, a middleware has already ensured they exist
|
params := r.Context().Value(CtxParams).(params.Params)
|
||||||
username, _ := params.Get("u")
|
// ignoring errors here, a middleware has already ensured they exist
|
||||||
password, _ := params.Get("p")
|
username, _ := params.Get("u")
|
||||||
token, _ := params.Get("t")
|
password, _ := params.Get("p")
|
||||||
salt, _ := params.Get("s")
|
token, _ := params.Get("t")
|
||||||
|
salt, _ := params.Get("s")
|
||||||
|
|
||||||
passwordAuth := token == "" && salt == ""
|
passwordAuth := token == "" && salt == ""
|
||||||
tokenAuth := password == ""
|
tokenAuth := password == ""
|
||||||
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
|
|
||||||
}
|
|
||||||
user := c.dbc.GetUserByName(username)
|
|
||||||
|
|
||||||
var credsOk bool
|
|
||||||
if tokenAuth && user != nil {
|
|
||||||
credsOk = checkCredsToken(user.Password, token, salt)
|
|
||||||
} else if user != nil {
|
|
||||||
credsOk = checkCredsBasic(user.Password, password)
|
|
||||||
}
|
|
||||||
if !credsOk {
|
|
||||||
// Because internal authentication failed, we can now try to use LDAP, if
|
|
||||||
// it was enabled by the user.
|
|
||||||
ok, err := ldap.CheckLDAPcreds(username, password, c.dbc)
|
|
||||||
if err != nil {
|
|
||||||
_ = writeResp(w, r, spec.NewError(40, err.Error()))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
user := dbc.GetUserByName(username)
|
||||||
|
|
||||||
if !ok {
|
var credsOk bool
|
||||||
_ = writeResp(w, r, spec.NewError(40, "invalid password"))
|
if tokenAuth && user != nil {
|
||||||
return
|
credsOk = checkCredsToken(user.Password, token, salt)
|
||||||
|
} else if user != nil {
|
||||||
|
credsOk = checkCredsBasic(user.Password, password)
|
||||||
}
|
}
|
||||||
}
|
if !credsOk {
|
||||||
withUser := context.WithValue(r.Context(), CtxUser, user)
|
// Because internal authentication failed, we can now try to use LDAP,
|
||||||
next.ServeHTTP(w, r.WithContext(withUser))
|
// if it was enabled by the user.
|
||||||
})
|
ok, err := ldap.CheckLDAPcreds(username, password, dbc)
|
||||||
|
if err != nil {
|
||||||
|
_ = writeResp(w, r, spec.NewError(40, err.Error()))
|
||||||
|
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))
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func slow(next http.Handler) http.Handler {
|
func slow(next http.Handler) http.Handler {
|
||||||
|
|||||||
Reference in New Issue
Block a user