Files
magpie/internal/plugin/plugin_api_prompts_test.go
T
yetone e115b96411 plugin: an api sign-in whose prompts ask the key asks no second key
An OpenCode plugin's "api" auth method with its own prompts and an
authorize (inkstone's API key ways, Devin's session token in
magpie-community/plugins#928) asks the key in its prompts and returns it
from authorize(inputs). The GUI asked those prompts and then always showed
a key field of its own, so the user typed the same token twice and the
second entry went nowhere. The host's save() also copied every answer into
the account's metadata, so the token sat there a second time in plain
text.

- host.js marks such a method keyAsked (type api, prompts, authorize).
  The GUI's startPluginSignIn signs in with no key field for it, as for
  keyless, and a rejected answer asks the prompts again on Try again.
  PluginAPIKey and `magpie plugin` login skip the empty-key check for it
  (Method.NoKey).
- save() keeps only select answers (choices, never secrets) beside the
  authorize result's own metadata. A method with no authorize still keeps
  every answer, as OpenCode does.
- An authorize that gives no key, with no key typed, is now an error
  instead of an "ok" that saved nothing.

OpenCode itself differs: its v1 CLI (providers.ts) asks the prompts, then
always asks "Enter your API key", saves result.key ?? the typed key, and
merges the answers into metadata; its TUI never calls authorize for api
methods. magpie-community plugins are written for magpie's flow and read
the key from inputs, so this follows them.

Plugins that read prompt answers back from metadata (grep of
magpie-community/plugins packages): zcode (site, a select), joycode
(tenant) and agnes (keyProfile, consoleToken). None has an authorize, so
none changes. Every api+authorize plugin (inkstone, cloudflare-workers-ai,
sensenova, agnes-code, astudio, sap-aicore, Devin #928) puts what it needs
in its result's metadata itself; sensenova's apiKey: undefined
workaround is now moot.

Run on mote (macOS 27, Go 1.26.8, Playwright 1.62.0) at this change on
origin/main 9c39edb92: gofmt -l clean; go vet of internal/plugin/...
internal/gui/... internal/provider internal/cli, also GOOS=windows and
GOOS=linux -tags nogui: ok. go test ./internal/plugin/...
./internal/gui/... ./internal/provider: ok. ./internal/cli: ok but
TestEveryRouteHasACommandOrIsNamed, red the same alone on 9c39edb92
(POST /api/gateway/pause from e8fbee60e has no command), filed as #5946.
node --test, chromium and webkit, of plugin-api-prompt-key (new),
plugin-keyless-signin, plugin-password-prompt, plugin-signin,
plugin-ask-ways, plugin-key-hint, plugin-other-ways, signin-paste-key,
plugin-signin-opened, plugin-signin-scrolls, plugin-pick-sub,
plugin-two-subs, zcode-site-layout and i18n-keys: 176 of 176 ok. The new
GUI test passed 3 of 3 alone. plugin-signin-no-url is red 22 of 22 at
the base too (987eac0aa); that is #5938, commented with the macOS run.

Broken by hand: GUI back to keyless-only and host without keyAsked ->
TestAPIPromptsKeyAskedAndMetadata and plugin-api-prompt-key 22 of 22 red;
save() copying every answer again -> the Go test red (token kept twice,
the note answer kept).

Not tried: a real plugin sign-in (inkstone, Devin), the CLI path (no
test), a run on the Windows or Linux boxes, the full ./... suite.
2026-11-10 19:53:54 +08:00

90 lines
2.7 KiB
Go

package plugin
import (
"context"
"encoding/json"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
// An "api" method whose own questions ask its key (inkstone's API key way,
// Devin's session token in plugins#928) was asked a key of magpie's beside
// them, which went nowhere, and every answer was copied into the
// account's metadata, so the token sat there a second time in plain text.
// Such a method is marked keyAsked, signs in with no key, and keeps only
// its choices' answers beside what its authorize gave; a method with no
// authorize still keeps its answers as OpenCode does.
func TestAPIPromptsKeyAskedAndMetadata(t *testing.T) {
sandbox(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/apiprompts")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
ps, err := Providers(ctx)
if err != nil {
t.Fatal(err)
}
var ms []Method
for _, p := range ps {
if p.ID == "apiprompts" {
ms = p.Methods
}
}
if len(ms) != 3 || !ms[0].KeyAsked || !ms[0].NoKey() || ms[1].KeyAsked || ms[1].NoKey() || ms[2].KeyAsked || ms[2].NoKey() {
t.Fatalf("methods = %+v, want only the first keyAsked", ms)
}
if _, err := APIKey(ctx, "apiprompts", 0, map[string]string{"site": "eu", "token": "tok-secret", "note": "cookie-secret"}, "", NewAccount); err != nil {
t.Fatal(err)
}
if _, err := APIKey(ctx, "apiprompts", 1, map[string]string{"tenant": "acme"}, "k-site", NewAccount); err != nil {
t.Fatal(err)
}
if _, err := APIKey(ctx, "apiprompts", 2, nil, "k-own", NewAccount); err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(AuthPath())
if err != nil {
t.Fatal(err)
}
if n := strings.Count(string(b), "tok-secret"); n != 1 {
t.Errorf("the token is kept %d times, want once (as the key): %s", n, b)
}
if strings.Contains(string(b), "cookie-secret") {
t.Errorf("a typed answer is kept in metadata: %s", b)
}
var auth map[string]struct {
Type string `json:"type"`
Key string `json:"key"`
Metadata map[string]string `json:"metadata"`
}
if err := json.Unmarshal(b, &auth); err != nil {
t.Fatal(err)
}
want := map[string]map[string]string{
"tok-secret": {"site": "eu", "email": "who@apiprompts.invalid"},
"k-site": {"tenant": "acme"},
"k-own": {"checked": "yes"},
}
got := map[string]map[string]string{}
for _, a := range auth {
if a.Type == "api" {
got[a.Key] = a.Metadata
}
}
if !reflect.DeepEqual(got, want) {
t.Errorf("accounts by key = %v, want %v", got, want)
}
// no key typed and none given by authorize: nothing is saved
if _, err := APIKey(ctx, "apiprompts", 2, nil, "", NewAccount); err == nil {
t.Error("a sign-in with no key typed and none given was taken")
}
}