qianxunji (#feedback) and curtinp118 on Discord: a VPS restored from a backup, or a new computer joining sync, had to sign in to every subscription again. Sign-ins (subscription accounts in logins.json, Codex's auth.json, plugins' sign-ins in plugin-auth.json) can now go too, in each place only when picked; all are off by default, so an upgrade carries nothing new. - Sync (WebDAV, S3, GitHub): a "Sign-ins" tick in the form, with a line saying anyone with the passphrase and the server's file can use the accounts, and that an account on two computers can be signed out on one when the other renews it. CLI: `magpie webdav|s3|github set sign-ins=yes`. - Export: "With the sign-ins", off; ticked, the passphrase hint says the file holds them. CLI: `magpie backup --sign-ins`. - Import: "Bring in the sign-ins too, when the file has them", on. CLI: `magpie restore --no-sign-ins` leaves them out. - Back up by itself: "Sign-ins in backups", Off/On. On asks for a passphrase (twice) that seals the sign-ins alone (the rest of an automatic backup stays unsealed, as before); it is kept in autobackup.json (0600), never sent in the view. A backup whose sign-ins another passphrase sealed restores without them and says so. What goes (provider.CollectSignIns, plugin.CarriedAuth): magpie's own copies only, not an agent's own account, an agent-home row, a hidden or plugin: row, or a retired built-in. Each record is keyed by agent + email + org (or the account key); ids two rows share go on neither side. Restore/import (RestoreSignIns) adds what is missing and updates only when the incoming copy was renewed later; nothing here is removed. Before writing over a row it counts every row that could be the same account (same login, same refresh token, same id): none adds, several keeps all, one is kept when it isn't magpie's own copy, is held, or is the agent's live account. Codex's auth.json is written temp-and-rename, 0600, only where Codex is signed out. Sync merges per record against the last sync's hashes (MergeSignIns): the later renewal wins; a removal crosses only when the other side still had it as of the last sync; ApplySignIns re-checks each row's hash before writing so a renewal in between is kept. A computer with the tick off sends none, so others remove nothing for it; turning it off, a Restore, or a replaced server file clears the last-sync record. Export's and Import's forms moved out of app.js into syncfiles.js, imported when one opens (as autobackup.js is), so the core is 3.2KB under its budget with no raise (app.js 3.2KB smaller than before). docs/subsystems/sync-backup.md has a "Sign-ins, when picked" section; subscription_signin_test.go's header now says it pins the default. Run at the pushed head (678d28cba on origin/mainbc10a6198): - wsl-run go test -tags nogui -timeout 45m: backup, davsync, plugin, gui ok (TestCoreScriptBudget in it). provider: one red, TestCancelledPluginSignInSavesNothing, the late sign-in saved after Cancel. It is green 20 of 20 alone, at this head and at origin/main alike, and this change adds only new files there whose tests run after it; filed as #5047. cli there hung in proc.lookStamp's os.Stat on WSL at load 100, so it ran again on mote: one red, TestEveryRouteHasACommandOrIsNamed (GET /api/context/reply, #5026), the same at origin/main. gateway -run TestCompactionOverByteCapEveryTime ok (#4862 on mote's full run). TestMainWindowCorners on mote: red the same at origin/main (#3949). - GUI, on mote: the 16 files that open Sync, plus i18n-keys and old-webkit, Chromium and WebKit, every language: 255 of 294 pass. The 39 reds are segs-press-redraw's searchAllRow (#4566), s3-sync in WebKit (#2448) and usage-agents-hide (#3414). The same files at origin/mainbc10a6198give the same 39 pass and 39 fail. sync-signins, auto-backup, sync-sign-in, i18n-keys and old-webkit alone on WSL: 92/92. - The whole GUI suite on WSL, stopped at 737 of its tests (1162 files, load ~100 there): 9 reds, all WebKit, none in a Sync file: account-eye (#4244, #5052), add-button (#4792), account-plugin-user and agent-expand-steady. The last two pass at this head and at origin/main on mote. The rest of the suite was not run. - gofmt -l: clean. go vet: darwin (internal/gui, internal/cli), GOOS=linux and GOOS=windows -tags nogui for backup, davsync, provider, plugin, cli, gui: ok. - win-run and wsl-run: go test -tags nogui -run 'SignIns|Carries|AutoBackupSignIns' ./internal/provider ./internal/backup ./internal/davsync: ok on Windows 11 and Ubuntu 24.04. - sync-signins.test.cjs: 20/20, Chromium and WebKit, 1100 and 420px, en/zh/zh-TW/ja/de, no horizontal overflow, no page errors. Then rebased on origin/main5ee5274bd(12 commits; davsync.go, app.js and i18n.js merged with no conflict) and run again at the pushed head on mote: gofmt clean; go test -run 'TestCoreScriptBudget|SignIns|Carries| AutoBackup|Sync' for gui, davsync, backup and provider: ok; sync-signins, auto-backup, sync-sign-in, i18n-keys, old-webkit: 92/92. Broken by hand, each red, then restored: - RestoreSignIns writing an older token over a newer one: TestRestoreSignInsNeverWritesAnOlderTokenOver red. - The ambiguous-account guard has two layers (more than one match, and a single match with another record id); with only the first removed the test stayed green, with both removed TestRestoreSignInsLeavesAnAmbiguousAccount went red. - MergeSignIns removing a row renewed here: TestMergeSignInsRemovedOnlyWhenTheOtherSideRemovedIt red. - Codex auth.json written over a signed-in Codex: TestRestoreSignInsSignsCodexInOnlyWhereSignedOut red. - ApplySignIns not re-checking the hash: TestApplySignInsKeepsWhatChangedSinceTheMerge red. - davsync forgetting the last-sync record (st.SignIns = nil): TestSyncSignIns red. - GUI: Export's tick on by default, and auto backup's On posting with no passphrase: sync-signins.test.cjs red for each. Not tried: real accounts carried to a real VPS or second computer (all Go tests use testenv homes, fake DAV and fake tokens; the Keychain is never reached); a real vendor's refresh after a carried sign-in, and how each vendor treats one refresh token used from two machines; the CLI flags against a sandbox HOME (covered by the cli package's tests only); auto backup's sign-ins have no CLI switch.
89 lines
2.2 KiB
Go
89 lines
2.2 KiB
Go
package plugin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
|
|
"github.com/yetone/magpie/internal/lastgood"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// AuthEntries is plugin-auth.json's sign-ins by key, each as written, for
|
|
// a backup that carries them. A file that can't be read is an error,
|
|
// never no sign-ins: carried as none, it would read as every sign-in
|
|
// signed out.
|
|
func AuthEntries() (map[string]json.RawMessage, error) {
|
|
b, err := readAuthFile()
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return map[string]json.RawMessage{}, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var m map[string]json.RawMessage
|
|
if err := json.Unmarshal(b, &m); err != nil {
|
|
return nil, err
|
|
}
|
|
if m == nil {
|
|
m = map[string]json.RawMessage{}
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// AuthWho is who a sign-in entry is signed in as (its accountId, or the
|
|
// email it carries), "" when it doesn't say.
|
|
func AuthWho(raw json.RawMessage) string {
|
|
var a storedAuth
|
|
if json.Unmarshal(raw, &a) != nil {
|
|
return ""
|
|
}
|
|
return firstNonEmpty(a.AccountID, a.Metadata.Email, a.Email)
|
|
}
|
|
|
|
// AuthExpires is when an OAuth sign-in entry's access token runs out, in
|
|
// Unix milliseconds: a later one is a later refresh. 0 when it doesn't say.
|
|
func AuthExpires(raw json.RawMessage) int64 {
|
|
var a struct {
|
|
Expires int64 `json:"expires"`
|
|
}
|
|
if json.Unmarshal(raw, &a) != nil {
|
|
return 0
|
|
}
|
|
return a.Expires
|
|
}
|
|
|
|
// EditAuth changes plugin-auth.json under plugin-auth.json.lock, which
|
|
// host.js takes too, read afresh there: f gets the sign-ins by key and
|
|
// says whether it changed them. A file that can't be read is not written
|
|
// over; the one there is kept as its last good generation first, and the
|
|
// new one is renamed in whole.
|
|
func EditAuth(f func(m map[string]json.RawMessage) (bool, error)) error {
|
|
if err := os.MkdirAll(settings.Dir(), 0o700); err != nil {
|
|
return err
|
|
}
|
|
unlock := lockAuth()
|
|
defer unlock()
|
|
m, err := AuthEntries()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ch, err := f(m)
|
|
if err != nil || !ch {
|
|
return err
|
|
}
|
|
b, err := json.MarshalIndent(m, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := lastgood.Keep(AuthPath(), validAuth, 0o600); err != nil {
|
|
return err
|
|
}
|
|
if err := writeWhole(AuthPath(), append(b, '\n')); err != nil {
|
|
return err
|
|
}
|
|
changed()
|
|
return nil
|
|
}
|