ljayic asked for a complete, stable management API so nothing needs a hand-edited file. /v1/magpie/admin/ now serves the app's own /api handlers (providers and their keys and accounts, OAuth sign-in start/poll/cancel, gateway keys, account groups) to a gateway key set with `magpie gateway-key admin <id> local|any`. Routes and actions not in gui/admin.go's adminRoutes are 404, so a provider's saved key, a gateway key's secret, sign-in export and actions on this computer's apps are not served; "admin-key" is not served either, so the API can't raise its own caller. Sign-in returns the vendor's page and code, opens nothing, takes no password. Guard (gateway/admin.go), in order: any Origin or Sec-Fetch-Site/-Mode is 403 with or without a key (Origin null, foreign, listed, *.localhost, an app scheme); no key 401; a key without Key.Admin 403; from loopback the Host must be ownHost (a rebound page's GET has no Origin); from the network the key must be "any"; a proxied request counts as remote. Key.Admin is dropped (adminOf) for a read-only, Browser, issued, expiring or default key, and setting read-only clears it. The route is in readOnlyRefused and keySlotFree. Docs: docs/reference.md "Management API", gateway-routing.md, and the remote page in en, zh and ja. Routing Groups, sign-in export and a GUI switch for the level are left to #6307. Run (mica, the worktree before the rebase onto 9 newer commits): go vet of access, cli, gateway, gui (darwin), GOOS=windows vet of the same four, GOOS=linux -tags nogui vet of access, cli, gateway; gofmt -l empty; go test -count=1 -timeout 45m ./internal/access ./internal/cli ./internal/gateway ./internal/gui: all ok but TestKeySlotEveryRoute, which named the new route and is fixed here, then it and the read-only, admin and guard tests pass again. After the rebase the same tests ran again. Broken by hand, each red in TestAdminAPIGuards: browser check off (a browser case served), Admin check off (a key without it served), Host check off (a rebound name served), the "any" requirement off (a local-only key from the network served, and a proxied request). Tests: TestAdminAPIGuards (no key, unknown key, non-admin, read-only, local/any, x-api-key, 8 browser headers x 3 callers, rebound Host, proxy, no handler), TestAdminKey (access: levels, read-only, Browser, issued), TestGatewayKeyCLIAdmin, TestAdminAPIGatewayKeys / Providers (a canary key is never told) / SignIn, TestAdminRoutesEveryAPIRoute. Not tried: a real sign-in against a vendor through the API, a request from a second computer on the LAN, the GUI suite (no page or app.js changed), a Linux/Windows run of the new tests.
134 lines
5.0 KiB
Go
134 lines
5.0 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// The management API (gateway.AdminPath, ljayic on X) serves the app's own
|
|
// handlers for providers, accounts and gateway keys, so that what a script
|
|
// does and what the pages do can't drift. adminRoutes lists, by the path
|
|
// under /v1/magpie/admin/, what is served and which of the app's /api route
|
|
// answers it; a route not listed is a 404, whatever the app has, and
|
|
// TestAdminRoutesEveryAPIRoute fails when the list holds a route that is
|
|
// not the app's.
|
|
//
|
|
// Left out on purpose, because they tell a secret or act on this
|
|
// computer's apps and not on magpie's own objects: a provider's saved key
|
|
// ("key"), a gateway key's secret ("copy-key"), exporting sign-ins, and
|
|
// the actions that move, restart or rewrite an agent. Setting a key's
|
|
// Admin ("admin-key") is left out so that a key can't give another more
|
|
// than it was given: only the command line and the app do.
|
|
type adminRoute struct {
|
|
pattern string // "GET /providers": the method, and the path under AdminPath
|
|
to string // the app's route under /api it is served by, "" for the same path
|
|
// actions, when set, are the {action}s the route takes; any other is 404
|
|
actions []string
|
|
}
|
|
|
|
var adminRoutes = []adminRoute{
|
|
{pattern: "GET /providers"},
|
|
{pattern: "POST /provider/{action}", actions: adminProviderActions},
|
|
{pattern: "POST /keys/{action}"},
|
|
{pattern: "POST /login/{action}"},
|
|
{pattern: "GET /login/usage"},
|
|
{pattern: "GET /signin/{id}"},
|
|
{pattern: "POST /signin/{id}/cancel"},
|
|
{pattern: "POST /signin/{id}/callback"},
|
|
{pattern: "GET /gateway-keys", to: "/caller-keys"},
|
|
{pattern: "GET /gateway-keys/models", to: "/caller-keys/models"},
|
|
{pattern: "GET /gateway-keys/mcp", to: "/caller-keys/mcp"},
|
|
{pattern: "POST /gateway-keys/{action}", to: "/caller-keys/{action}", actions: adminKeyActions},
|
|
{pattern: "GET /account-groups"},
|
|
{pattern: "POST /account-groups/{action}"},
|
|
}
|
|
|
|
// adminProviderActions are the provider actions the API takes: saving and
|
|
// removing a provider, switching it, and the settings of its keys and
|
|
// accounts, and trying it. Not "key" (it tells the saved key), nor the
|
|
// actions on this computer's apps (show, move, adopt, restartapp, …).
|
|
var adminProviderActions = []string{
|
|
"save", "delete", "on", "off", "name", "efforts", "images", "arrange",
|
|
"accountmodels", "keylisted", "accountcap", "accountname", "windowcap",
|
|
"accountconcurrency", "keyspendcap", "keyownbalance", "keybalancerefill",
|
|
"keyexpiring", "accountweight", "route", "affinity", "sink", "usefirst",
|
|
"test", "teststop", "detect", "balance", "models", "list",
|
|
}
|
|
|
|
// adminKeyActions are access.Update's actions but "admin-key" and "copy-key".
|
|
var adminKeyActions = []string{
|
|
"add-key", "rotate-key", "rename-key", "limit-key", "reset-limit-key",
|
|
"models-key", "aliases-key", "accounts-key", "mcp-key", "quotas-key",
|
|
"concurrency-key", "tier-key", "redact-key", "issuer-key", "access-key",
|
|
"on-key", "off-key", "remove-key", "move-key",
|
|
}
|
|
|
|
var (
|
|
adminOnce sync.Once
|
|
adminMux http.Handler
|
|
)
|
|
|
|
func init() { gateway.SetAdminHandler(http.HandlerFunc(serveAdmin)) }
|
|
|
|
func serveAdmin(w http.ResponseWriter, r *http.Request) {
|
|
adminOnce.Do(func() { adminMux = adminAPI() })
|
|
adminMux.ServeHTTP(w, r)
|
|
}
|
|
|
|
// adminAPI is the management API's routes. The app's handlers are built
|
|
// with no window (nil Windows), so none opens a browser or a folder here.
|
|
func adminAPI() http.Handler {
|
|
app := http.NewServeMux()
|
|
providerRoutes(app, nil)
|
|
callerKeyRoutes(app)
|
|
mux := http.NewServeMux()
|
|
for _, ar := range adminRoutes {
|
|
mux.HandleFunc(ar.pattern, func(w http.ResponseWriter, r *http.Request) {
|
|
if ar.actions != nil && !slices.Contains(ar.actions, r.PathValue("action")) {
|
|
adminError(w, http.StatusNotFound, "this action is not in the management API")
|
|
return
|
|
}
|
|
to := r.URL.Path
|
|
if ar.to != "" {
|
|
to = strings.ReplaceAll(ar.to, "{action}", r.PathValue("action"))
|
|
}
|
|
r2 := r.Clone(r.Context())
|
|
r2.URL.Path, r2.URL.RawPath = "/api"+to, ""
|
|
app.ServeHTTP(w, r2)
|
|
})
|
|
}
|
|
// Signing in: the vendor's page and the code to type there are the
|
|
// answer, for the caller to open on a computer with a browser. Never
|
|
// opened here, and never given a password: the vendor's page takes it.
|
|
mux.HandleFunc("POST /signin", func(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
Agent, Site string
|
|
Proxy *string
|
|
User string
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
adminError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
st, err := provider.StartSignInWith(in.Agent, provider.SignInOptions{Site: in.Site, Proxy: in.Proxy, User: in.User})
|
|
if err != nil {
|
|
adminError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, st)
|
|
})
|
|
return mux
|
|
}
|
|
|
|
func adminError(w http.ResponseWriter, status int, msg string) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(map[string]string{"error": msg})
|
|
}
|