mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-18 08:08:45 +01:00
refactor(api): reuse existing helpers in API v1 auth
Use gg.V, slice.Map, chi's RequestSize, core/auth's encryption key and ClientIPRateLimiter instead of local copies; share the grant idle expiry constant and a Grant.LastActivity helper; pass last use as a time value.
This commit is contained in:
parent
88a652346b
commit
a41e656706
14 changed files with 70 additions and 71 deletions
|
|
@ -10,6 +10,7 @@ import (
|
|||
|
||||
"github.com/getkin/kin-openapi/openapi3"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/navidrome/navidrome/api"
|
||||
"github.com/navidrome/navidrome/core/apiauth"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
|
|
@ -37,15 +38,6 @@ var gateRulesV1 = gateRules{
|
|||
noStore: map[string]bool{"login": true, "setupFirstAdmin": true, "createAccessToken": true},
|
||||
}
|
||||
|
||||
func limitBody(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Body != nil {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxBodyBytes)
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (rt *Router) routes() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
doc, err := openapi3.NewLoader().LoadFromData(api.SpecJSON())
|
||||
|
|
@ -56,7 +48,7 @@ func (rt *Router) routes() http.Handler {
|
|||
if err != nil {
|
||||
log.Fatal("API v1: the embedded OpenAPI spec breaks the security rules", err)
|
||||
}
|
||||
r.Use(referenceIDMiddleware, problemRecoverer, headAsGet(r), limitBody, g.handler)
|
||||
r.Use(referenceIDMiddleware, problemRecoverer, headAsGet(r), middleware.RequestSize(maxBodyBytes), g.handler)
|
||||
r.NotFound(func(w http.ResponseWriter, req *http.Request) {
|
||||
writeProblemStatus(w, req, http.StatusNotFound, ProblemCodeNotFound, "no such endpoint")
|
||||
})
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"context"
|
||||
|
||||
"github.com/navidrome/navidrome/core/apiauth"
|
||||
"github.com/navidrome/navidrome/utils/gg"
|
||||
)
|
||||
|
||||
const defaultPageSize = 100
|
||||
|
|
@ -35,8 +36,8 @@ func (rt *Router) ListGrants(ctx context.Context, req ListGrantsRequestObject) (
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
offset := deref(req.Params.OffsetParam)
|
||||
limit := cmp.Or(deref(req.Params.LimitParam), defaultPageSize)
|
||||
offset := gg.V(req.Params.OffsetParam)
|
||||
limit := cmp.Or(gg.V(req.Params.LimitParam), defaultPageSize)
|
||||
grants, total, err := rt.auth.ListGrants(ctx, p, offset, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -3,14 +3,12 @@ package apiv1
|
|||
import (
|
||||
"github.com/navidrome/navidrome/core/apiauth"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/utils/gg"
|
||||
"github.com/navidrome/navidrome/utils/slice"
|
||||
)
|
||||
|
||||
func toScopes(in []string) []Scope {
|
||||
out := make([]Scope, len(in))
|
||||
for i, s := range in {
|
||||
out[i] = Scope(s)
|
||||
}
|
||||
return out
|
||||
return slice.Map(in, func(s string) Scope { return Scope(s) })
|
||||
}
|
||||
|
||||
// fromScopeRequests keeps nil (all scopes) apart from an empty list (no scopes).
|
||||
|
|
@ -58,7 +56,7 @@ func toGrantCreated(i *apiauth.Issued) GrantCreated {
|
|||
}
|
||||
|
||||
func clientMeta(c CredentialsRequest) apiauth.ClientMeta {
|
||||
return apiauth.ClientMeta{Client: c.Client, Name: deref(c.Name), ClientVersion: deref(c.ClientVersion)}
|
||||
return apiauth.ClientMeta{Client: c.Client, Name: gg.V(c.Name), ClientVersion: gg.V(c.ClientVersion)}
|
||||
}
|
||||
|
||||
// principal fails closed if the gate did not attach a principal to the context.
|
||||
|
|
@ -68,11 +66,3 @@ func principal(p *apiauth.Principal, ok bool) (*apiauth.Principal, error) {
|
|||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func deref[T any](p *T) T {
|
||||
var zero T
|
||||
if p == nil {
|
||||
return zero
|
||||
}
|
||||
return *p
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,8 +81,7 @@ func newGate(doc *openapi3.T, mux chi.Routes, auth authenticator, rules gateRule
|
|||
}
|
||||
}
|
||||
if conf.Server.AuthRequestLimit > 0 {
|
||||
g.limiter = httprate.LimitBy(conf.Server.AuthRequestLimit, conf.Server.AuthWindowLength,
|
||||
func(r *http.Request) (string, error) { return server.ClientIP(r), nil },
|
||||
g.limiter = server.ClientIPRateLimiter(conf.Server.AuthRequestLimit, conf.Server.AuthWindowLength,
|
||||
httprate.WithLimitHandler(func(w http.ResponseWriter, r *http.Request) {
|
||||
writeProblemStatus(w, r, http.StatusTooManyRequests, ProblemCodeRateLimited, "too many requests")
|
||||
}))
|
||||
|
|
|
|||
|
|
@ -234,10 +234,10 @@ func trustedProxyPrefixes(list string) []string {
|
|||
|
||||
// ClientIPRateLimiter returns a rate limiter keyed by ClientIP, so spoofed forwarding headers
|
||||
// cannot be rotated for a fresh bucket.
|
||||
func ClientIPRateLimiter(requestLimit int, windowLength time.Duration) func(http.Handler) http.Handler {
|
||||
func ClientIPRateLimiter(requestLimit int, windowLength time.Duration, opts ...httprate.Option) func(http.Handler) http.Handler {
|
||||
return httprate.LimitBy(requestLimit, windowLength, func(r *http.Request) (string, error) {
|
||||
return ClientIP(r), nil
|
||||
})
|
||||
}, opts...)
|
||||
}
|
||||
|
||||
// ClientIP returns the canonical client IP resolved by realIPMiddleware, for keying rate limits. The
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue