mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-08 02:17:25 +02:00
API v1 no longer mints short-lived JWT access tokens. Clients send the grant secret from POST /auth/login or /auth/setup as `Authorization: Bearer` on every request. Every request already looked the grant up in the database, so the JWT gave no speed or revocation benefit and only added a refresh loop, which early client authors pushed back on. The grant already is an API key: one per client sign-in, scoped and revocable. Revocation is now immediate on every node; the contract promises "within one minute". Removed: POST /auth/token, the grantAuth scheme, the TokenRequest and AccessToken schemas, the token_expired problem code, the API v1 JWT signer and its signing key, the grant liveness cache, and PropertyRepository.PutIfAbsent. ResolveGrant is now Authenticate. Short-lived tokens return later only as narrow media tokens for ?access_token= on media URLs, together with the media endpoints. Signed-off-by: Deluan <deluan@navidrome.org>
135 lines
4.5 KiB
Go
135 lines
4.5 KiB
Go
package apiv1
|
|
|
|
import (
|
|
"cmp"
|
|
"errors"
|
|
"net/http"
|
|
"runtime/debug"
|
|
"slices"
|
|
"strings"
|
|
|
|
"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"
|
|
"github.com/navidrome/navidrome/model"
|
|
)
|
|
|
|
const maxBodyBytes = 1 << 20
|
|
|
|
type Router struct {
|
|
http.Handler
|
|
ds model.DataStore
|
|
auth *apiauth.Service
|
|
}
|
|
|
|
func New(ds model.DataStore) *Router {
|
|
rt := &Router{ds: ds, auth: apiauth.New(ds)}
|
|
rt.Handler = rt.routes()
|
|
return rt
|
|
}
|
|
|
|
var gateRulesV1 = gateRules{
|
|
limited: map[string]bool{"login": true, "setupFirstAdmin": true, "changePassword": true},
|
|
noScope: map[string]bool{"getCapabilities": true},
|
|
noStore: map[string]bool{"login": true, "setupFirstAdmin": true},
|
|
}
|
|
|
|
func (rt *Router) routes() http.Handler {
|
|
r := chi.NewRouter()
|
|
doc, err := openapi3.NewLoader().LoadFromData(api.SpecJSON())
|
|
if err != nil {
|
|
log.Fatal("API v1: cannot load the embedded OpenAPI spec", err)
|
|
}
|
|
g, err := newGate(doc, r, rt.auth, gateRulesV1)
|
|
if err != nil {
|
|
log.Fatal("API v1: the embedded OpenAPI spec breaks the security rules", err)
|
|
}
|
|
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")
|
|
})
|
|
r.MethodNotAllowed(func(w http.ResponseWriter, req *http.Request) {
|
|
w.Header().Set("Allow", strings.Join(allowedMethods(r, req), ", "))
|
|
writeProblemStatus(w, req, http.StatusMethodNotAllowed, ProblemCodeMethodNotAllowed, "")
|
|
})
|
|
|
|
r.Get("/openapi.json", specHandler(withBasePath(api.SpecJSON(), `"url": `, true), "application/json"))
|
|
r.Get("/openapi.yaml", specHandler(withBasePath(api.SpecYAML(), "url: ", false), "application/yaml"))
|
|
|
|
strict := NewStrictHandlerWithOptions(rt, nil, StrictHTTPServerOptions{
|
|
RequestErrorHandlerFunc: func(w http.ResponseWriter, req *http.Request, err error) {
|
|
if tooLarge(err) {
|
|
writeProblem(w, req, ClientError(err, tooLargeDetail))
|
|
return
|
|
}
|
|
writeProblemStatus(w, req, http.StatusBadRequest, ProblemCodeValidation, "request body is not valid JSON")
|
|
},
|
|
ResponseErrorHandlerFunc: writeProblem,
|
|
})
|
|
HandlerWithOptions(strict, ChiServerOptions{BaseRouter: r, ErrorHandlerFunc: bindingErrorHandler})
|
|
if err := g.checkRoutes(); err != nil {
|
|
log.Fatal("API v1: routes and the embedded OpenAPI spec disagree", err)
|
|
}
|
|
return r
|
|
}
|
|
|
|
func problemRecoverer(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
defer func() {
|
|
rec := recover()
|
|
if rec == nil {
|
|
return
|
|
}
|
|
if err, ok := rec.(error); ok && errors.Is(err, http.ErrAbortHandler) {
|
|
panic(rec)
|
|
}
|
|
log.Error(r.Context(), "API v1: panic in handler", "panic", rec, "stack", string(debug.Stack()))
|
|
writeProblemStatus(w, r, http.StatusInternalServerError, ProblemCodeInternal, "")
|
|
}()
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
var routableMethods = []string{http.MethodGet, http.MethodHead, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete}
|
|
|
|
// Looks routes up on mux itself: chi's RouteContext().Routes points at the parent router when mounted.
|
|
func allowedMethods(mux chi.Routes, req *http.Request) []string {
|
|
path := routePath(req)
|
|
var allowed []string
|
|
for _, m := range routableMethods {
|
|
if mux.Match(chi.NewRouteContext(), m, path) || (m == http.MethodHead && slices.Contains(allowed, http.MethodGet)) {
|
|
allowed = append(allowed, m)
|
|
}
|
|
}
|
|
return allowed
|
|
}
|
|
|
|
func headAsGet(mux chi.Routes) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
if req.Method == http.MethodHead && !mux.Match(chi.NewRouteContext(), http.MethodHead, routePath(req)) {
|
|
chi.RouteContext(req.Context()).RouteMethod = http.MethodGet
|
|
}
|
|
next.ServeHTTP(w, req)
|
|
})
|
|
}
|
|
}
|
|
|
|
// routeMethod is the method chi dispatches on, which headAsGet sets to GET for a HEAD only GET serves.
|
|
func routeMethod(req *http.Request) string {
|
|
if rctx := chi.RouteContext(req.Context()); rctx != nil && rctx.RouteMethod != "" {
|
|
return rctx.RouteMethod
|
|
}
|
|
return req.Method
|
|
}
|
|
|
|
// routePath must pick the same path chi's routeHTTP dispatches on, or the gate could vet a different route.
|
|
func routePath(req *http.Request) string {
|
|
if rctx := chi.RouteContext(req.Context()); rctx != nil && rctx.RoutePath != "" {
|
|
return rctx.RoutePath
|
|
}
|
|
return cmp.Or(req.URL.RawPath, req.URL.Path, "/")
|
|
}
|