navidrome/server/apiv1/dto.go
Deluan 2aca5fe365 refactor(api): simplify the API v1 spec gate
Key operations by a struct, share the validation options, derive the
route method from chi, and set every rule-derived field in buildGateOp.
Build WWW-Authenticate challenges and 413 problems in one place, fetch
the principal through one helper, and refuse gate rules that name an
operation missing from the spec.
2026-09-28 20:06:28 -04:00

71 lines
1.8 KiB
Go

package apiv1
import (
"context"
"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 {
return slice.Map(in, func(s string) Scope { return Scope(s) })
}
// fromScopeRequests keeps nil (all scopes) apart from an empty list (no scopes).
func fromScopeRequests(in *[]ScopeRequest) []string {
if in == nil {
return nil
}
return append([]string{}, *in...)
}
func nullable(s string) *string {
if s == "" {
return nil
}
return &s
}
func toGrant(g model.Grant, currentID string) Grant {
return Grant{
Id: g.ID,
Name: g.Name,
Client: g.Client,
ClientVersion: nullable(g.ClientVersion),
Scopes: toScopes(g.Scopes),
Provider: g.Provider,
CreatedAt: g.CreatedAt,
LastUsedAt: g.LastUsedAt,
LastUsedIp: nullable(g.LastUsedIP),
Current: g.ID == currentID,
}
}
func toGrantCreated(i *apiauth.Issued) GrantCreated {
return GrantCreated{
Secret: i.Secret,
Grant: toGrant(i.Grant, i.Grant.ID),
User: AuthUser{
Id: i.User.ID,
UserName: i.User.UserName,
Name: i.User.Name,
IsAdmin: i.User.IsAdmin,
PasswordChangeable: apiauth.PasswordChangeable(i.User),
},
}
}
func clientMeta(c CredentialsRequest) apiauth.ClientMeta {
return apiauth.ClientMeta{Client: c.Client, Name: gg.V(c.Name), ClientVersion: gg.V(c.ClientVersion)}
}
// principalFrom fails closed if the gate did not attach a principal to the context.
func principalFrom(ctx context.Context) (*apiauth.Principal, error) {
p, ok := apiauth.PrincipalFrom(ctx)
if !ok || p == nil {
return nil, model.ErrInvalidAuth
}
return p, nil
}