@gruu6y/page-clean-login 0.2.9 → 0.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/account-browser.mjs +459 -0
- package/lib/account-exit.mjs +604 -0
- package/lib/account-proxies.mjs +376 -36
- package/lib/agent-cli.mjs +22 -0
- package/lib/agent-surface.mjs +41 -2
- package/lib/browser-launcher.mjs +142 -36
- package/lib/renew-hosting.mjs +90 -38
- package/lib/server.mjs +247 -83
- package/lib/signin-flow.mjs +51 -25
- package/package.json +1 -1
- package/ui/account-view.js +178 -6
- package/ui/app.js +422 -2
- package/ui/index.html +5 -0
- package/ui/style.css +33 -0
package/lib/server.mjs
CHANGED
|
@@ -41,11 +41,12 @@ import { MarketInstallError, createMarketInstaller } from "./market-install.mjs"
|
|
|
41
41
|
import { RenewHostingError, createRenewHosting, offView } from "./renew-hosting.mjs";
|
|
42
42
|
import { buildIssueRequest, isAllowedIssueUrl } from "./issue-report.mjs";
|
|
43
43
|
import { openExternal, OpenExternalError } from "./open-external.mjs";
|
|
44
|
-
import { createEnvironmentManager, EnvironmentError } from "./browser-environment.mjs";
|
|
44
|
+
import { EXECUTABLE_ENV, createEnvironmentManager, EnvironmentError } from "./browser-environment.mjs";
|
|
45
45
|
import { SIGNIN_TERMINAL_STATES, createSignInFlow, SignInFlowError } from "./signin-flow.mjs";
|
|
46
46
|
import { sanitizeSubmittedInputs } from "./method-inputs.mjs";
|
|
47
47
|
import { createNativeSignIn, NativeSignInError } from "./native-signin.mjs";
|
|
48
48
|
import { SignInBrowserError } from "./browser-launcher.mjs";
|
|
49
|
+
import { AccountBrowserError, createAccountBrowserController } from "./account-browser.mjs";
|
|
49
50
|
import { createProxyScore, ProxyScoreError } from "./proxy-score.mjs";
|
|
50
51
|
import {
|
|
51
52
|
AccountRecordError,
|
|
@@ -75,7 +76,16 @@ import {
|
|
|
75
76
|
readAccountExit,
|
|
76
77
|
writeAccountExit,
|
|
77
78
|
} from "./account-proxies.mjs";
|
|
78
|
-
import {
|
|
79
|
+
import {
|
|
80
|
+
accountExitProjection,
|
|
81
|
+
buildProxyUrlMap,
|
|
82
|
+
exitDisplayUrl,
|
|
83
|
+
lookupProxyByUrl,
|
|
84
|
+
readProvidersSnapshot,
|
|
85
|
+
resolveAccountProxyName,
|
|
86
|
+
resolveEffectiveAccountExit,
|
|
87
|
+
} from "./account-exit.mjs";
|
|
88
|
+
import { readAuthEntries, readAuthExpiries, signInKey } from "./signin-writeback.mjs";
|
|
79
89
|
import { createProviderRegistration } from "./provider-registration.mjs";
|
|
80
90
|
import {
|
|
81
91
|
agentDigest,
|
|
@@ -232,7 +242,8 @@ function sendError(res, error) {
|
|
|
232
242
|
error instanceof IdentityCheckError ||
|
|
233
243
|
error instanceof OpenExternalError ||
|
|
234
244
|
error instanceof MarketInstallError ||
|
|
235
|
-
error instanceof RenewHostingError
|
|
245
|
+
error instanceof RenewHostingError ||
|
|
246
|
+
error instanceof AccountBrowserError
|
|
236
247
|
) {
|
|
237
248
|
json(res, error.status, { error: error.code, message: error.message });
|
|
238
249
|
return;
|
|
@@ -365,6 +376,19 @@ function accountExitFromPath(path) {
|
|
|
365
376
|
}
|
|
366
377
|
}
|
|
367
378
|
|
|
379
|
+
// `/api/accounts/<provider>/<key>/browser`: open, inspect, or close the
|
|
380
|
+
// account's standalone fingerprint browser. Resolved BEFORE `accountKeyFromPath`
|
|
381
|
+
// so the three-segment path is never swallowed as an account key.
|
|
382
|
+
function accountBrowserFromPath(path) {
|
|
383
|
+
const match = /^\/api\/accounts\/([^/]+)\/([^/]+)\/browser$/.exec(path);
|
|
384
|
+
if (!match) return null;
|
|
385
|
+
try {
|
|
386
|
+
return { provider: decodeURIComponent(match[1]), key: decodeURIComponent(match[2]) };
|
|
387
|
+
} catch {
|
|
388
|
+
throw new HttpError(400, "invalid_account", "The account path is invalid.");
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
368
392
|
// Human-readable companions to the exit writer's machine reasons. The HTTP
|
|
369
393
|
// error code stays the reason itself, so the page can tailor its hint.
|
|
370
394
|
const EXIT_WRITE_MESSAGES = {
|
|
@@ -378,23 +402,6 @@ const EXIT_WRITE_MESSAGES = {
|
|
|
378
402
|
verify_failed: "The exit could not be verified after writing providers.json.",
|
|
379
403
|
};
|
|
380
404
|
|
|
381
|
-
// A display-only view of an exit URL: scheme, host and port, never the
|
|
382
|
-
// credentials. `providers.json` stores a custom exit as a full URL and
|
|
383
|
-
// `provider.proxy` may carry one too, but the page only needs to recognise
|
|
384
|
-
// which exit is in force. Unparseable text yields null rather than an echo,
|
|
385
|
-
// so this can never hand a credential to the page.
|
|
386
|
-
function exitDisplayUrl(value) {
|
|
387
|
-
if (typeof value !== "string") return null;
|
|
388
|
-
const trimmed = value.trim();
|
|
389
|
-
if (!trimmed || trimmed === "direct") return null;
|
|
390
|
-
try {
|
|
391
|
-
const url = new URL(trimmed);
|
|
392
|
-
return url.host ? `${url.protocol}//${url.host}` : null;
|
|
393
|
-
} catch {
|
|
394
|
-
return null;
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
405
|
// The service's port is fixed: the page reads it from `window.magpie.options`
|
|
399
406
|
// and has no fallback, so it cannot simply move to a free one. magpie reloads a
|
|
400
407
|
// plugin in a fresh host process after an update, and the host it replaces keeps
|
|
@@ -527,8 +534,13 @@ export async function startServer({
|
|
|
527
534
|
// The hosted-renewal controller (tests inject a fake one) so no test ever
|
|
528
535
|
// opens a background browser or touches a real credential.
|
|
529
536
|
renewHosting: renewHostingOption,
|
|
537
|
+
// The standalone account-browser controller (tests inject a fake one) and
|
|
538
|
+
// the launcher seam the real controller opens its browser through.
|
|
539
|
+
accountBrowser: accountBrowserOption,
|
|
540
|
+
accountBrowserLauncher,
|
|
530
541
|
} = {}) {
|
|
531
542
|
const token = randomBytes(32).toString("hex");
|
|
543
|
+
const effectiveEnv = env ?? process.env;
|
|
532
544
|
const store = createProxyStore({ dataDir });
|
|
533
545
|
// The asset registry shares the plugin's private data directory but keeps
|
|
534
546
|
// its own file, so it never has to migrate the proxy store's shape.
|
|
@@ -629,29 +641,69 @@ export async function startServer({
|
|
|
629
641
|
onCheck: (check) => recordCheck(check),
|
|
630
642
|
});
|
|
631
643
|
|
|
644
|
+
async function resolveBrowserExecutable() {
|
|
645
|
+
if (environment?.status) {
|
|
646
|
+
try {
|
|
647
|
+
const status = await environment.status();
|
|
648
|
+
if (typeof status?.executablePath === "string" && status.executablePath) {
|
|
649
|
+
return status.executablePath;
|
|
650
|
+
}
|
|
651
|
+
} catch {
|
|
652
|
+
// Fall through to the environment override.
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
return effectiveEnv?.[EXECUTABLE_ENV] || process.env[EXECUTABLE_ENV] || null;
|
|
656
|
+
}
|
|
657
|
+
|
|
632
658
|
// Hosted renewal watches the credentials it has been explicitly enabled for
|
|
633
659
|
// and re-runs those accounts' own sign-in, headless, through the exit they
|
|
634
660
|
// logged in with. It shares the sign-in controller, so a hosted renewal and
|
|
635
|
-
// an interactive login can never run at the same time.
|
|
661
|
+
// an interactive login can never run at the same time. The account-browser
|
|
662
|
+
// guard uses a lazy wrapper so renewHosting and accountBrowser never deadlock
|
|
663
|
+
// on mutual initialization.
|
|
664
|
+
let accountBrowser = null;
|
|
636
665
|
const renewHosting =
|
|
637
666
|
renewHostingOption ||
|
|
638
|
-
createRenewHosting({
|
|
667
|
+
createRenewHosting({
|
|
668
|
+
env: effectiveEnv,
|
|
669
|
+
dataDir,
|
|
670
|
+
signIn,
|
|
671
|
+
store,
|
|
672
|
+
identityCheck,
|
|
673
|
+
accountBrowser: {
|
|
674
|
+
isOpen: (provider, key) => accountBrowser?.isOpen?.(provider, key) === true,
|
|
675
|
+
},
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
// Standalone account-browser controller: lets the user open an account's own
|
|
679
|
+
// pinned Camoufox profile through its live effective exit without mutating
|
|
680
|
+
// identity or exit state.
|
|
681
|
+
accountBrowser =
|
|
682
|
+
accountBrowserOption ||
|
|
683
|
+
createAccountBrowserController({
|
|
684
|
+
env: effectiveEnv,
|
|
685
|
+
store,
|
|
686
|
+
signIn,
|
|
687
|
+
renewHosting,
|
|
688
|
+
resolveExecutable: resolveBrowserExecutable,
|
|
689
|
+
...(accountBrowserLauncher ? { launchBrowser: accountBrowserLauncher } : {}),
|
|
690
|
+
});
|
|
639
691
|
|
|
640
692
|
// A single account object, used both by the list and by the PATCH response.
|
|
641
693
|
// Optional fields are whitelisted projections of the ledger; the fingerprint
|
|
642
694
|
// is a short digest, never the identity object or the directory path.
|
|
643
695
|
// `hostingById` lets the list read the policy file once for every row; a
|
|
644
696
|
// single-account response reads just its own entry.
|
|
645
|
-
async function accountView(entry, keys, hostingById = null,
|
|
697
|
+
async function accountView(entry, keys, hostingById = null, liveExit = undefined, authContext = null) {
|
|
646
698
|
const hosting = hostingById
|
|
647
699
|
? hostingById.get(`${entry.provider}\u0000${entry.key}`) ?? offView(entry.provider, entry.key)
|
|
648
700
|
: await renewHosting.viewOf(entry.provider, entry.key);
|
|
649
701
|
const extras = accountExtras(entry.meta);
|
|
650
|
-
//
|
|
651
|
-
//
|
|
652
|
-
//
|
|
653
|
-
//
|
|
654
|
-
if (
|
|
702
|
+
// magpie's providers.json is the single source of truth for the account's
|
|
703
|
+
// current exit. Whenever providers.json authoritatively resolves this
|
|
704
|
+
// account, its credential-free projection overrides any stale ledger
|
|
705
|
+
// snapshot (`extras.exit`).
|
|
706
|
+
if (liveExit !== undefined) extras.exit = liveExit;
|
|
655
707
|
const runtimeEnv = env ?? process.env;
|
|
656
708
|
const authEntries = authContext?.authEntries ?? readAuthEntries(runtimeEnv);
|
|
657
709
|
const authExpiries = authContext?.authExpiries ?? readAuthExpiries(runtimeEnv);
|
|
@@ -664,6 +716,14 @@ export async function startServer({
|
|
|
664
716
|
? entry.meta.methodLabel
|
|
665
717
|
: null;
|
|
666
718
|
const expiresAt = authExpiries?.[entry.key] ?? hosting?.expiresAt ?? null;
|
|
719
|
+
const browser =
|
|
720
|
+
typeof accountBrowser?.status === "function"
|
|
721
|
+
? await accountBrowser.status(
|
|
722
|
+
entry.provider,
|
|
723
|
+
entry.key,
|
|
724
|
+
authContext?.liveResolved ? { liveResolved: authContext.liveResolved } : {},
|
|
725
|
+
)
|
|
726
|
+
: { open: false, openedAt: null, exit: null, exitChanged: false };
|
|
667
727
|
return {
|
|
668
728
|
provider: entry.provider,
|
|
669
729
|
key: entry.key,
|
|
@@ -677,6 +737,7 @@ export async function startServer({
|
|
|
677
737
|
...extras,
|
|
678
738
|
fingerprint: await readFingerprintSummary(entry.dir),
|
|
679
739
|
hosting,
|
|
740
|
+
browser,
|
|
680
741
|
};
|
|
681
742
|
}
|
|
682
743
|
|
|
@@ -712,7 +773,6 @@ export async function startServer({
|
|
|
712
773
|
// The account orphan sweep: one run per process at startup, plus an on-demand
|
|
713
774
|
// run from POST /api/accounts/prune. The last report is summarized on
|
|
714
775
|
// /api/health. It is fire-and-forget and never throws.
|
|
715
|
-
const effectiveEnv = env ?? process.env;
|
|
716
776
|
let lastAccountPrune = null;
|
|
717
777
|
let lastAccountPruneAt = null;
|
|
718
778
|
let pruneRun = null;
|
|
@@ -769,6 +829,15 @@ export async function startServer({
|
|
|
769
829
|
if (state !== "idle" && !SIGNIN_TERMINAL_STATES.has(state) && flow?.provider && flow?.key) {
|
|
770
830
|
dirs.add(accountDir(effectiveEnv, flow.provider, flow.key));
|
|
771
831
|
}
|
|
832
|
+
try {
|
|
833
|
+
for (const open of accountBrowser?.listOpen?.() ?? []) {
|
|
834
|
+
if (open?.provider && open?.key) {
|
|
835
|
+
dirs.add(accountDir(effectiveEnv, open.provider, open.key));
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
} catch {
|
|
839
|
+
// Best effort.
|
|
840
|
+
}
|
|
772
841
|
return dirs;
|
|
773
842
|
}
|
|
774
843
|
|
|
@@ -891,6 +960,29 @@ export async function startServer({
|
|
|
891
960
|
return { relogin: "none" };
|
|
892
961
|
}
|
|
893
962
|
|
|
963
|
+
async function liveAccountExit(entry, { authEntries, nativeEntries, providersDoc, proxyByUrl } = {}) {
|
|
964
|
+
const resolved = await resolveEffectiveAccountExit({
|
|
965
|
+
env: effectiveEnv,
|
|
966
|
+
provider: entry.provider,
|
|
967
|
+
key: entry.key,
|
|
968
|
+
meta: entry.meta,
|
|
969
|
+
store,
|
|
970
|
+
authEntries,
|
|
971
|
+
nativeEntries,
|
|
972
|
+
providersDoc,
|
|
973
|
+
proxyByUrl,
|
|
974
|
+
// When the account's standalone browser is open, match open()'s
|
|
975
|
+
// fallbackToLedger: true resolution so exitChanged stays accurate even
|
|
976
|
+
// when providers.json is absent; accountExitProjection still only
|
|
977
|
+
// overrides `account.exit` for authoritative providers.json sources.
|
|
978
|
+
fallbackToLedger: accountBrowser?.isOpen?.(entry.provider, entry.key) === true,
|
|
979
|
+
});
|
|
980
|
+
return {
|
|
981
|
+
exit: accountExitProjection(resolved, accountExtras(entry.meta).exit),
|
|
982
|
+
resolved,
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
|
|
894
986
|
// Every known account with its ownership ledger fields and whether its key
|
|
895
987
|
// is signed in. No path and no credential is ever returned. Shared by
|
|
896
988
|
// GET /api/accounts and the compact /api/agent digest, so the two can never
|
|
@@ -910,43 +1002,30 @@ export async function startServer({
|
|
|
910
1002
|
// An unreadable policy file must not hide the accounts themselves; every
|
|
911
1003
|
// row simply shows hosting as off.
|
|
912
1004
|
}
|
|
913
|
-
// Read
|
|
914
|
-
//
|
|
915
|
-
//
|
|
1005
|
+
// Read identity stores and magpie's providers.json once for the whole
|
|
1006
|
+
// table. providers.json is the single source of truth for each account's
|
|
1007
|
+
// active exit, so every row projects live state rather than being masked
|
|
1008
|
+
// by a stale ledger `exit` snapshot.
|
|
916
1009
|
const authEntries = readAuthEntries(effectiveEnv);
|
|
917
1010
|
const authExpiries = readAuthExpiries(effectiveEnv);
|
|
918
1011
|
const nativeEntries = readNativeLoginsResult(effectiveEnv).entries ?? [];
|
|
919
|
-
|
|
1012
|
+
const providersDoc = await readProvidersSnapshot({ env: effectiveEnv });
|
|
1013
|
+
const proxyByUrl = providersDoc.ok ? await buildProxyUrlMap(store) : null;
|
|
920
1014
|
const accounts = [];
|
|
921
1015
|
for (const entry of dirs) {
|
|
922
1016
|
if (!entry.meta) continue;
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|| nativeEntries.find((e) => e?.key === key)?.email
|
|
930
|
-
|| "";
|
|
931
|
-
const read = await readAccountExit({ env: effectiveEnv, provider: entry.provider, accountId });
|
|
932
|
-
if (read.ok && read.mode === "custom") {
|
|
933
|
-
// providers.json only holds URL text; the stored records are needed
|
|
934
|
-
// to name the node. Built lazily and once, deduped by URL.
|
|
935
|
-
if (!proxyByUrl) {
|
|
936
|
-
proxyByUrl = new Map();
|
|
937
|
-
for (const item of await store.list()) {
|
|
938
|
-
const record = await store.get(item.id);
|
|
939
|
-
const url = proxyUrlFor(record);
|
|
940
|
-
if (url && !proxyByUrl.has(url)) proxyByUrl.set(url, record);
|
|
941
|
-
}
|
|
942
|
-
}
|
|
943
|
-
fallback = loginSnapshot(proxyByUrl.get(read.url) ?? null, null);
|
|
944
|
-
} else if (read.ok && read.mode === "direct") {
|
|
945
|
-
fallback = loginSnapshot(null, null);
|
|
946
|
-
}
|
|
947
|
-
}
|
|
1017
|
+
const { exit: liveExit, resolved: liveResolved } = await liveAccountExit(entry, {
|
|
1018
|
+
authEntries,
|
|
1019
|
+
nativeEntries,
|
|
1020
|
+
providersDoc,
|
|
1021
|
+
proxyByUrl,
|
|
1022
|
+
});
|
|
948
1023
|
accounts.push(
|
|
949
|
-
await accountView(entry, keys, hostingById,
|
|
1024
|
+
await accountView(entry, keys, hostingById, liveExit, {
|
|
1025
|
+
authEntries,
|
|
1026
|
+
authExpiries,
|
|
1027
|
+
liveResolved,
|
|
1028
|
+
}),
|
|
950
1029
|
);
|
|
951
1030
|
}
|
|
952
1031
|
// Deterministic order: provider, then account key.
|
|
@@ -1602,14 +1681,16 @@ export async function startServer({
|
|
|
1602
1681
|
}
|
|
1603
1682
|
if (req.method === "PATCH") {
|
|
1604
1683
|
const changes = validateUpdate(await readJson(req));
|
|
1684
|
+
const previousUrl = proxyUrlFor(existing);
|
|
1605
1685
|
const updated = await store.update(route.id, changes);
|
|
1606
1686
|
if (!updated) throw new HttpError(404, "not_found", "Proxy not found.");
|
|
1607
1687
|
const connectionChanged =
|
|
1608
1688
|
proxyConnectionKey(existing) !== proxyConnectionKey(updated) ||
|
|
1609
|
-
(existing.password || "") !== (updated.password || "")
|
|
1689
|
+
(existing.password || "") !== (updated.password || "") ||
|
|
1690
|
+
previousUrl !== proxyUrlFor(updated);
|
|
1610
1691
|
if (connectionChanged && typeof accountProxySync.syncProxyUpdate === "function") {
|
|
1611
1692
|
try {
|
|
1612
|
-
await accountProxySync.syncProxyUpdate(updated);
|
|
1693
|
+
await accountProxySync.syncProxyUpdate({ proxyId: updated.id, previousUrl });
|
|
1613
1694
|
} catch {
|
|
1614
1695
|
// Best effort sync of bound accounts in providers.json.
|
|
1615
1696
|
}
|
|
@@ -1855,15 +1936,18 @@ export async function startServer({
|
|
|
1855
1936
|
// directory key. The name comes from the ledger first, then the auth
|
|
1856
1937
|
// file, then the native login ledger; an empty name is passed through
|
|
1857
1938
|
// so the lib reports `account_name_unknown` instead of a 500 here.
|
|
1858
|
-
const
|
|
1859
|
-
const
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1939
|
+
const authEntries = readAuthEntries(effectiveEnv);
|
|
1940
|
+
const nativeEntries = readNativeLoginsResult(effectiveEnv).entries ?? [];
|
|
1941
|
+
const accountId = resolveAccountProxyName({
|
|
1942
|
+
meta,
|
|
1943
|
+
key,
|
|
1944
|
+
authEntries,
|
|
1945
|
+
nativeEntries,
|
|
1946
|
+
env: effectiveEnv,
|
|
1947
|
+
});
|
|
1864
1948
|
// The page's select needs the stored node id, but providers.json only
|
|
1865
|
-
// holds URL text. `
|
|
1866
|
-
//
|
|
1949
|
+
// holds URL text. `buildProxyUrlMap` indexes full records by exact and
|
|
1950
|
+
// canonical URL so matching stays consistent with the accounts list.
|
|
1867
1951
|
async function exitView() {
|
|
1868
1952
|
const read = await readAccountExit({ env: effectiveEnv, provider, accountId });
|
|
1869
1953
|
// Only the fields the page renders leave the service, and never the
|
|
@@ -1882,13 +1966,8 @@ export async function startServer({
|
|
|
1882
1966
|
: { ok: false, reason: read.reason, key: read.key };
|
|
1883
1967
|
let proxyId = null;
|
|
1884
1968
|
if (read.ok && read.mode === "custom") {
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
if (proxyUrlFor(record) === read.url) {
|
|
1888
|
-
proxyId = item.id;
|
|
1889
|
-
break;
|
|
1890
|
-
}
|
|
1891
|
-
}
|
|
1969
|
+
const matched = lookupProxyByUrl(await buildProxyUrlMap(store), read.url);
|
|
1970
|
+
proxyId = matched?.id ?? null;
|
|
1892
1971
|
}
|
|
1893
1972
|
return { ...view, proxyId };
|
|
1894
1973
|
}
|
|
@@ -1920,7 +1999,14 @@ export async function startServer({
|
|
|
1920
1999
|
}
|
|
1921
2000
|
exitProxy = stored;
|
|
1922
2001
|
}
|
|
1923
|
-
const result = await writeAccountExit({
|
|
2002
|
+
const result = await writeAccountExit({
|
|
2003
|
+
env: effectiveEnv,
|
|
2004
|
+
provider,
|
|
2005
|
+
accountId,
|
|
2006
|
+
mode,
|
|
2007
|
+
url,
|
|
2008
|
+
dataDir,
|
|
2009
|
+
});
|
|
1924
2010
|
if (!result.ok) {
|
|
1925
2011
|
// The error code is the lib's reason itself, so the page can show
|
|
1926
2012
|
// a specific hint for each failure.
|
|
@@ -1936,9 +2022,10 @@ export async function startServer({
|
|
|
1936
2022
|
// Mirror the change into the plugin's own ledger. `exit` records "the
|
|
1937
2023
|
// exit this account is set to use right now", separate from the
|
|
1938
2024
|
// lastLogin history: the accounts list and hosting renewal both read
|
|
1939
|
-
//
|
|
1940
|
-
//
|
|
1941
|
-
//
|
|
2025
|
+
// providers.json as SSOT and fall back to the ledger only when
|
|
2026
|
+
// providers.json is absent. Only the whitelisted display fields are
|
|
2027
|
+
// stored, exactly like recordCheck above. The updated account record
|
|
2028
|
+
// is projected through the same live resolver as GET /api/accounts.
|
|
1942
2029
|
let updatedAccount = null;
|
|
1943
2030
|
try {
|
|
1944
2031
|
const now = new Date().toISOString();
|
|
@@ -1953,7 +2040,17 @@ export async function startServer({
|
|
|
1953
2040
|
await writeAccountMeta(dir, merged);
|
|
1954
2041
|
return merged;
|
|
1955
2042
|
});
|
|
1956
|
-
|
|
2043
|
+
const { exit: liveExit, resolved: liveResolved } = await liveAccountExit(
|
|
2044
|
+
{ provider, key, dir, meta: updatedMeta },
|
|
2045
|
+
{ authEntries, nativeEntries },
|
|
2046
|
+
);
|
|
2047
|
+
updatedAccount = await accountView(
|
|
2048
|
+
{ provider, key, dir, meta: updatedMeta },
|
|
2049
|
+
keys,
|
|
2050
|
+
null,
|
|
2051
|
+
liveExit,
|
|
2052
|
+
{ authEntries, liveResolved },
|
|
2053
|
+
);
|
|
1957
2054
|
} catch {
|
|
1958
2055
|
// A ledger write failure must not change the reported exit change.
|
|
1959
2056
|
}
|
|
@@ -1967,6 +2064,40 @@ export async function startServer({
|
|
|
1967
2064
|
return methodNotAllowed(res, "GET, PATCH");
|
|
1968
2065
|
}
|
|
1969
2066
|
|
|
2067
|
+
// Standalone fingerprint browser per account. Resolved BEFORE
|
|
2068
|
+
// `accountKeyFromPath` so `/browser` is never swallowed as an account key.
|
|
2069
|
+
const browserTarget = accountBrowserFromPath(url.pathname);
|
|
2070
|
+
if (browserTarget) {
|
|
2071
|
+
const { provider, key } = browserTarget;
|
|
2072
|
+
if (req.method === "GET") {
|
|
2073
|
+
const meta = await readAccountMeta(accountDir(effectiveEnv, provider, key));
|
|
2074
|
+
if (!meta || meta.provider !== provider) {
|
|
2075
|
+
throw new HttpError(404, "not_found", "Account not found.");
|
|
2076
|
+
}
|
|
2077
|
+
json(res, 200, { ok: true, browser: await accountBrowser.status(provider, key) });
|
|
2078
|
+
return;
|
|
2079
|
+
}
|
|
2080
|
+
if (req.method === "POST") {
|
|
2081
|
+
const body = await readJson(req, { optional: true });
|
|
2082
|
+
if (body !== null && (typeof body !== "object" || Array.isArray(body))) {
|
|
2083
|
+
throw new HttpError(400, "invalid_body", "Request body must be a JSON object.");
|
|
2084
|
+
}
|
|
2085
|
+
const browser = await accountBrowser.open({ provider, key, url: body?.url });
|
|
2086
|
+
json(res, 200, { ok: true, browser });
|
|
2087
|
+
return;
|
|
2088
|
+
}
|
|
2089
|
+
if (req.method === "DELETE") {
|
|
2090
|
+
const meta = await readAccountMeta(accountDir(effectiveEnv, provider, key));
|
|
2091
|
+
if (!meta || meta.provider !== provider) {
|
|
2092
|
+
throw new HttpError(404, "not_found", "Account not found.");
|
|
2093
|
+
}
|
|
2094
|
+
const result = await accountBrowser.close(provider, key);
|
|
2095
|
+
json(res, 200, { ok: true, ...result });
|
|
2096
|
+
return;
|
|
2097
|
+
}
|
|
2098
|
+
return methodNotAllowed(res, "GET, POST, DELETE");
|
|
2099
|
+
}
|
|
2100
|
+
|
|
1970
2101
|
const accountTarget = accountKeyFromPath(url.pathname);
|
|
1971
2102
|
if (accountTarget) {
|
|
1972
2103
|
const dir = accountDir(effectiveEnv, accountTarget.provider, accountTarget.key);
|
|
@@ -1985,13 +2116,27 @@ export async function startServer({
|
|
|
1985
2116
|
return merged;
|
|
1986
2117
|
});
|
|
1987
2118
|
const keys = liveAccountKeys();
|
|
2119
|
+
const { exit: liveExit, resolved: liveResolved } = await liveAccountExit({
|
|
2120
|
+
...accountTarget,
|
|
2121
|
+
dir,
|
|
2122
|
+
meta: updated,
|
|
2123
|
+
});
|
|
1988
2124
|
json(res, 200, {
|
|
1989
2125
|
ok: true,
|
|
1990
|
-
account: await accountView({ ...accountTarget, dir, meta: updated }, keys
|
|
2126
|
+
account: await accountView({ ...accountTarget, dir, meta: updated }, keys, null, liveExit, {
|
|
2127
|
+
liveResolved,
|
|
2128
|
+
}),
|
|
1991
2129
|
});
|
|
1992
2130
|
return;
|
|
1993
2131
|
}
|
|
1994
2132
|
if (req.method !== "DELETE") return methodNotAllowed(res, "DELETE");
|
|
2133
|
+
if (accountBrowser.isOpen?.(accountTarget.provider, accountTarget.key)) {
|
|
2134
|
+
throw new HttpError(
|
|
2135
|
+
409,
|
|
2136
|
+
"browser_open",
|
|
2137
|
+
"Close this account's fingerprint browser before removing this account.",
|
|
2138
|
+
);
|
|
2139
|
+
}
|
|
1995
2140
|
const keyResult = liveAccountKeySet();
|
|
1996
2141
|
if (!keyResult.ok) {
|
|
1997
2142
|
// Without the key set we cannot tell a signed-in account from a
|
|
@@ -2012,9 +2157,11 @@ export async function startServer({
|
|
|
2012
2157
|
}
|
|
2013
2158
|
try {
|
|
2014
2159
|
if (typeof accountProxySync.clearAccountProxy === "function") {
|
|
2160
|
+
const clearAccountId =
|
|
2161
|
+
resolveAccountProxyName({ meta, key: accountTarget.key, env: effectiveEnv }) || null;
|
|
2015
2162
|
await accountProxySync.clearAccountProxy({
|
|
2016
2163
|
provider: accountTarget.provider,
|
|
2017
|
-
accountId:
|
|
2164
|
+
accountId: clearAccountId,
|
|
2018
2165
|
});
|
|
2019
2166
|
}
|
|
2020
2167
|
} catch {
|
|
@@ -2069,6 +2216,21 @@ export async function startServer({
|
|
|
2069
2216
|
...(body.account.rotate === true ? { rotate: true } : {}),
|
|
2070
2217
|
}
|
|
2071
2218
|
: null;
|
|
2219
|
+
// Refuse to start a sign-in or fingerprint rotation against an
|
|
2220
|
+
// account whose standalone fingerprint browser holds the same
|
|
2221
|
+
// `profile/` directory.
|
|
2222
|
+
const targetBrowserKey =
|
|
2223
|
+
account?.key ??
|
|
2224
|
+
(body?.single === true
|
|
2225
|
+
? signInKey(provider, "new", liveAccountKeys(), { single: true })
|
|
2226
|
+
: null);
|
|
2227
|
+
if (targetBrowserKey && accountBrowser.isOpen?.(provider, targetBrowserKey)) {
|
|
2228
|
+
throw new HttpError(
|
|
2229
|
+
409,
|
|
2230
|
+
"browser_open",
|
|
2231
|
+
"Close this account's fingerprint browser before signing in or rotating its fingerprint.",
|
|
2232
|
+
);
|
|
2233
|
+
}
|
|
2072
2234
|
// Whitelist the chosen exit for the ownership ledger. A stored proxy
|
|
2073
2235
|
// carries credentials that must never be written to account.json, so
|
|
2074
2236
|
// only these four display fields are handed to the flow. A direct
|
|
@@ -2353,6 +2515,7 @@ export async function startServer({
|
|
|
2353
2515
|
store,
|
|
2354
2516
|
assetStore,
|
|
2355
2517
|
renewHosting,
|
|
2518
|
+
accountBrowser,
|
|
2356
2519
|
// Exposed so the package's own tests can wait for the startup sweep
|
|
2357
2520
|
// instead of racing it.
|
|
2358
2521
|
prune: () => runPrune(),
|
|
@@ -2368,6 +2531,7 @@ export async function startServer({
|
|
|
2368
2531
|
// host) on shutdown instead of leaving an orphan browser.
|
|
2369
2532
|
await proxyScore.close?.();
|
|
2370
2533
|
await identityCheck.close?.();
|
|
2534
|
+
await accountBrowser.closeAll?.();
|
|
2371
2535
|
// Stop the watcher and close any renewal browser it had open, instead of
|
|
2372
2536
|
// leaving a headless profile running after the service is gone.
|
|
2373
2537
|
renewHosting.stop?.();
|
package/lib/signin-flow.mjs
CHANGED
|
@@ -695,34 +695,48 @@ export function createSignInFlow(options = {}) {
|
|
|
695
695
|
// ledger are saved: a magpie without providers.json, or a read-only
|
|
696
696
|
// config directory, must not turn a successful sign-in into an
|
|
697
697
|
// error. Only a credential-free summary is kept, never the URL.
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
current.proxySync = {
|
|
705
|
-
ok: sync?.ok !== false,
|
|
706
|
-
reason: sync?.reason ?? null,
|
|
707
|
-
changed: Array.isArray(sync?.changed) ? sync.changed.length : 0,
|
|
708
|
-
// A credential-free registration report tells the caller whether
|
|
709
|
-
// magpie now offers the provider whose first sign-in happened here.
|
|
710
|
-
registration: sync?.registration
|
|
711
|
-
? {
|
|
712
|
-
ok: sync.registration.ok === true,
|
|
713
|
-
state: sync.registration.state ?? null,
|
|
714
|
-
action: sync.registration.action ?? null,
|
|
715
|
-
reason: sync.registration.reason ?? null,
|
|
716
|
-
}
|
|
717
|
-
: null,
|
|
718
|
-
};
|
|
719
|
-
} catch (error) {
|
|
698
|
+
//
|
|
699
|
+
// Silent runs (hosted renewals) already follow the account's live
|
|
700
|
+
// exit from providers.json; writing back here would turn an
|
|
701
|
+
// `inherit` account using `provider.proxy` into a pinned `custom`
|
|
702
|
+
// override, so skip proxy sync on silent renewals.
|
|
703
|
+
if (current.silent) {
|
|
720
704
|
current.proxySync = {
|
|
721
|
-
ok:
|
|
722
|
-
reason: "
|
|
705
|
+
ok: true,
|
|
706
|
+
reason: "skipped_silent",
|
|
723
707
|
changed: 0,
|
|
724
|
-
|
|
708
|
+
registration: null,
|
|
725
709
|
};
|
|
710
|
+
} else {
|
|
711
|
+
try {
|
|
712
|
+
const sync = await syncAccountProxy({
|
|
713
|
+
provider: current.provider,
|
|
714
|
+
accountId: written.entry?.accountId ?? null,
|
|
715
|
+
proxy: current.proxy,
|
|
716
|
+
});
|
|
717
|
+
current.proxySync = {
|
|
718
|
+
ok: sync?.ok !== false,
|
|
719
|
+
reason: sync?.reason ?? null,
|
|
720
|
+
changed: Array.isArray(sync?.changed) ? sync.changed.length : 0,
|
|
721
|
+
// A credential-free registration report tells the caller whether
|
|
722
|
+
// magpie now offers the provider whose first sign-in happened here.
|
|
723
|
+
registration: sync?.registration
|
|
724
|
+
? {
|
|
725
|
+
ok: sync.registration.ok === true,
|
|
726
|
+
state: sync.registration.state ?? null,
|
|
727
|
+
action: sync.registration.action ?? null,
|
|
728
|
+
reason: sync.registration.reason ?? null,
|
|
729
|
+
}
|
|
730
|
+
: null,
|
|
731
|
+
};
|
|
732
|
+
} catch (error) {
|
|
733
|
+
current.proxySync = {
|
|
734
|
+
ok: false,
|
|
735
|
+
reason: "sync_failed",
|
|
736
|
+
changed: 0,
|
|
737
|
+
error: String(error?.message ?? error),
|
|
738
|
+
};
|
|
739
|
+
}
|
|
726
740
|
}
|
|
727
741
|
current.state = "done";
|
|
728
742
|
current.error = null;
|
|
@@ -1217,6 +1231,17 @@ export function createSignInFlow(options = {}) {
|
|
|
1217
1231
|
return publicFlow(flow);
|
|
1218
1232
|
}
|
|
1219
1233
|
|
|
1234
|
+
// Read-only view of the account key currently occupied by a non-terminal
|
|
1235
|
+
// sign-in or rotation flow (if any), so sibling features (such as opening
|
|
1236
|
+
// the account's fingerprint browser) never collide on the same profile dir.
|
|
1237
|
+
function activeAccount() {
|
|
1238
|
+
const current = flow;
|
|
1239
|
+
if (!current || current.state === "idle" || TERMINAL.has(current.state)) return null;
|
|
1240
|
+
const key = current.accountKey || current.claimKey || current.key || null;
|
|
1241
|
+
if (!current.provider || !key) return null;
|
|
1242
|
+
return { provider: current.provider, key };
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1220
1245
|
async function cancel() {
|
|
1221
1246
|
const current = flow;
|
|
1222
1247
|
if (!current || TERMINAL.has(current.state)) {
|
|
@@ -1246,6 +1271,7 @@ export function createSignInFlow(options = {}) {
|
|
|
1246
1271
|
startSignIn,
|
|
1247
1272
|
submitCode,
|
|
1248
1273
|
status,
|
|
1274
|
+
activeAccount,
|
|
1249
1275
|
cancel,
|
|
1250
1276
|
// Waits for the background callback to reach a terminal state. Used by
|
|
1251
1277
|
// tests; in production a GET reads `status()` instead.
|