Files
magpie/internal/access/accounts.go
T
yetone 1502433978 gui: a key's value can be changed in place, keeping its name, place and settings, and its rest is lifted (#1869)
A key rotated at its vendor had to be removed and added again, which
lost its name, its place in the order, its weight, protocol, models,
proxy and concurrency, and any gateway key restricted to it. As decided
on #1869, in multi-key mode the row now has a Change key action, with a
double-click on the row as a shortcut.

- provider.ReplaceKey(id, ref, key) sets the key's new value and moves
  what providers.json keys by the key's fingerprint (AccountModels,
  AccountProxies, AccountConcurrency) to the new one. An empty value, an
  unknown key and a value another key of the provider already holds are
  refused. The same value is a no-op.
- access.ReplaceAccount moves gateway keys' Accounts entries from
  provider/old to provider/new, under the caller-keys lock.
- gateway.Server.UnrestAll lifts a rest key and every per-model rest
  under it. The GUI's keys/replace lifts the key's rest (provider#key, and
  the provider's own rest key when it is the only key on), so the new
  value is asked from the next request.
- app.js: a pencil beside the key's copy button opens a password field in
  place of the masked value. Enter saves, Escape leaves it, and a
  double-click on the row away from its controls opens it too. The button
  has its own class (.key-edit) with the copy button's look.
- i18n: four strings in zh, zh-TW, ja and de.
- providers-accounts.md: ReplaceKey in the Keys table.

Past requests keep the old fingerprint in history, under the same name.
A rest is lifted only in the gateway this magpie serves.

Run:
- go test ./internal/gateway ./internal/provider ./internal/access
  ./internal/gui -count=1: ok. New TestReplaceKeyKeepsItsPlace,
  TestKeysReplaceKeepsTheKeyAndLiftsItsRest and
  TestUnrestAllLiftsAKeysModels each fail with their change taken out.
- gofmt -l clean; go vet darwin, linux -tags nogui, windows.
- key-replace.test.cjs (new): en/zh/zh-TW/ja/de, chromium and webkit,
  1100 and 420px. Checks the post, order, note, no sideways scroll, and
  no scroll on change or on double-click plus Escape.
- With key-copy, rest-try-now and key-import: 96/96.
- 72 key*/routing*/rest* GUI files at concurrency 6: 863/871. The 8 reds
  were key-copy, whose button.copy locator matched the pencil; that's why
  it has its own class now, and key-copy passes.
- Under load, about one run in 300 saw the editor body go back to the top
  after the copy button's double-click, with no script scrolling it. That
  is #1504 (findings added there). The test checks scroll before that
  step.
2026-10-13 07:30:06 +08:00

114 lines
3.3 KiB
Go

package access
import (
"errors"
"fmt"
"slices"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// CleanAccounts is a key's account list as kept (#905): each entry
// "<provider>/<account>", an account named by its stable id — or by the
// name it is shown by, resolved to that id, so the command line reads
// while the list keeps what a rename doesn't move — and a key by its
// fingerprint; trimmed, told apart in lower case as accounts are, each
// once, in the order given. Every account the entry names must be one
// its provider has now, so a typo can't quietly hold a key to nothing;
// one gone later — a key rotated, an account signed out — is kept as it
// is, matching nothing.
func CleanAccounts(in []string) ([]string, error) {
var out []string
for _, a := range in {
a = strings.TrimSpace(a)
if a == "" {
continue
}
pid, ref, ok := strings.Cut(a, "/")
if !ok || pid == "" || ref == "" {
return nil, errors.New("A key's account is <provider>/<account> or <provider>/<key id>, like codex/me@example.com: " + a)
}
if len(a) > 300 {
return nil, errors.New("A key's account is at most 300 characters")
}
p, err := provider.Find(pid)
if err != nil {
return nil, err
}
id := ""
for _, r := range p.AccountIDs() {
if strings.EqualFold(r.ID, ref) || strings.EqualFold(r.User, ref) {
id = r.ID
break
}
}
if id == "" {
if p.Account == nil {
return nil, fmt.Errorf("%s has no key %q", p.Name, ref)
}
return nil, fmt.Errorf("%s has no account %q", p.Name, ref)
}
kept := p.ID + "/" + id
if !slices.ContainsFunc(out, func(o string) bool { return strings.EqualFold(o, kept) }) {
out = append(out, kept)
}
}
if len(out) > 500 {
return nil, errors.New("A key can list at most 500 accounts")
}
return out, nil
}
// AllowsAccount says the provider pid's account or key ref is among the
// key's accounts — "<provider>/<ref>", told apart in lower case, as
// accounts are. The list holds a key to some accounts **of the providers
// it names**: a provider it names no account of, the key uses as it
// always did, its every account and key. Any, for a key that lists none.
func (who Identity) AllowsAccount(pid, ref string) bool {
if len(who.Accounts) == 0 {
return true
}
pid = strings.ToLower(strings.TrimSpace(pid))
key := pid + "/" + strings.ToLower(strings.TrimSpace(ref))
named := false
for _, a := range who.Accounts {
if a = strings.ToLower(strings.TrimSpace(a)); a == key {
return true
} else if p, _, ok := strings.Cut(a, "/"); ok && p == pid {
named = true
}
}
return !named
}
// ReplaceAccount moves every key's account list from one of provider
// pid's keys to the fingerprint it has now (#1869: the key's value
// replaced in place), so a key held to it is held to it still. It
// returns how many keys it moved.
func ReplaceAccount(pid, was, now string) (int, error) {
if was == now {
return 0, nil
}
mu.Lock()
defer mu.Unlock()
keys, err := load()
if err != nil {
return 0, err
}
old := strings.ToLower(pid + "/" + was)
moved := 0
for i := range keys {
for j, a := range keys[i].Accounts {
if strings.ToLower(strings.TrimSpace(a)) == old {
keys[i].Accounts[j] = pid + "/" + now
moved++
}
}
}
if moved == 0 {
return 0, nil
}
return moved, save(keys)
}