dsh-auth-remote 0.1.8 → 0.1.10
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/CHANGELOG.md +19 -1
- package/CHANGELOG.zh-CN.md +19 -1
- package/CONTRIBUTING.md +1 -1
- package/CONTRIBUTING.zh-CN.md +1 -1
- package/README.md +35 -13
- package/README.zh-CN.md +35 -13
- package/cordis.patch.yml +2 -1
- package/dist/cli.js +125 -30
- package/dist/cli.js.map +2 -2
- package/dist/client.js +3196 -441
- package/dist/client.js.map +4 -4
- package/dist/index.js +472 -42
- package/dist/index.js.map +3 -3
- package/dist/login.css +2 -8
- package/dist/login.css.map +2 -2
- package/dist/login.js +114 -26
- package/dist/login.js.map +2 -2
- package/dist/types/adapters/dsh/account-callback.d.ts +15 -0
- package/dist/types/adapters/dsh/account-callback.d.ts.map +1 -0
- package/dist/types/adapters/dsh/browser-capability.d.ts +1 -1
- package/dist/types/adapters/dsh/browser-capability.d.ts.map +1 -1
- package/dist/types/adapters/dsh/hmr-context.d.ts +10 -0
- package/dist/types/adapters/dsh/hmr-context.d.ts.map +1 -0
- package/dist/types/adapters/dsh/webserver.d.ts +3 -0
- package/dist/types/adapters/dsh/webserver.d.ts.map +1 -1
- package/dist/types/auth/service.d.ts +2 -0
- package/dist/types/auth/service.d.ts.map +1 -1
- package/dist/types/auth/sessions.d.ts +2 -0
- package/dist/types/auth/sessions.d.ts.map +1 -1
- package/dist/types/config.d.ts +9 -3
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/http/account-login.d.ts +45 -0
- package/dist/types/http/account-login.d.ts.map +1 -0
- package/dist/types/http/routes.d.ts +16 -1
- package/dist/types/http/routes.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/shared/account-login.d.ts +22 -0
- package/dist/types/shared/account-login.d.ts.map +1 -0
- package/dist/types/shared/auth-contract.d.ts +2 -0
- package/dist/types/shared/auth-contract.d.ts.map +1 -1
- package/dist/types/shared/i18n.d.ts +112 -24
- package/dist/types/shared/i18n.d.ts.map +1 -1
- package/dist/types/shared/locales/en.d.ts +56 -12
- package/dist/types/shared/locales/en.d.ts.map +1 -1
- package/dist/types/shared/locales/zh.d.ts +56 -12
- package/dist/types/shared/locales/zh.d.ts.map +1 -1
- package/docs/deployment.md +24 -17
- package/docs/deployment.zh-CN.md +24 -17
- package/docs/development.md +29 -7
- package/docs/development.zh-CN.md +29 -7
- package/docs/legacy-compatibility.md +192 -117
- package/docs/legacy-compatibility.zh-CN.md +192 -0
- package/docs/releasing.md +6 -6
- package/docs/releasing.zh-CN.md +6 -6
- package/docs/reverse-proxy.md +2 -2
- package/docs/reverse-proxy.zh-CN.md +2 -2
- package/examples/web-profile-auth-remote.patch.yml +4 -2
- package/package.json +31 -13
package/dist/index.js
CHANGED
|
@@ -59,10 +59,24 @@ function assertConnectionCompatibility(connection, anchor) {
|
|
|
59
59
|
return own.manifest.version;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
// src/shared/account-login.ts
|
|
63
|
+
import { satisfies } from "semver";
|
|
64
|
+
var ACCOUNT_LOGIN_BASE = "/auth-remote/account";
|
|
65
|
+
var ACCOUNT_CALLBACK_PATH = "/oauth/callback";
|
|
66
|
+
var ACCOUNT_LOGIN_CAPABILITY = "__DSH_AUTH_REMOTE_MANUAL_ACCOUNT_LOGIN__";
|
|
67
|
+
function supportsManualAccountLogin(version) {
|
|
68
|
+
return satisfies(version, ">=0.2.1-alpha.3", { includePrerelease: true });
|
|
69
|
+
}
|
|
70
|
+
function isActiveAccountLogin(phase) {
|
|
71
|
+
return ["initializing", "waiting-browser", "exchanging", "committing"].includes(phase);
|
|
72
|
+
}
|
|
73
|
+
|
|
62
74
|
// src/adapters/dsh/browser-capability.ts
|
|
63
|
-
function installAuthenticatedHostCapability(ctx) {
|
|
75
|
+
function installAuthenticatedHostCapability(ctx, version) {
|
|
64
76
|
ctx.on("webserver/index-inject", (table) => {
|
|
65
77
|
table.push({ kind: "global", name: "__DSH_TRANSPORT__", value: { ownsHost: true } });
|
|
78
|
+
if (version && supportsManualAccountLogin(version))
|
|
79
|
+
table.push({ kind: "global", name: ACCOUNT_LOGIN_CAPABILITY, value: true });
|
|
66
80
|
});
|
|
67
81
|
}
|
|
68
82
|
|
|
@@ -491,6 +505,17 @@ function listenerTransport(actual, capabilities) {
|
|
|
491
505
|
};
|
|
492
506
|
}
|
|
493
507
|
|
|
508
|
+
// src/adapters/dsh/hmr-context.ts
|
|
509
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
510
|
+
function outsideHmrTransaction(ctx, initialize) {
|
|
511
|
+
const hmr = ctx.get("hmr");
|
|
512
|
+
const scope = hmr?.executing;
|
|
513
|
+
if (typeof hmr?.runExclusive === "function" && scope instanceof AsyncLocalStorage && scope.getStore() === true) {
|
|
514
|
+
return scope.exit(initialize);
|
|
515
|
+
}
|
|
516
|
+
return initialize();
|
|
517
|
+
}
|
|
518
|
+
|
|
494
519
|
// src/adapters/dsh/webserver.ts
|
|
495
520
|
import WebServer2 from "@deepseek-ai/dsh-host-webserver";
|
|
496
521
|
var READY_PATH = "/auth-remote/ready";
|
|
@@ -567,15 +592,30 @@ var GuardedWebServer = class extends WebServer2 {
|
|
|
567
592
|
}
|
|
568
593
|
gate;
|
|
569
594
|
originPolicy;
|
|
595
|
+
accountCallbackBridge;
|
|
570
596
|
/** Ready and the authenticated gate consume the same listener-finalized policy. */
|
|
571
597
|
setOriginPolicy(policy) {
|
|
572
598
|
this.originPolicy = policy;
|
|
573
599
|
}
|
|
600
|
+
installAccountCallbackBridge(bridge) {
|
|
601
|
+
this.accountCallbackBridge = bridge;
|
|
602
|
+
}
|
|
574
603
|
register(route) {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
604
|
+
const callback = this.accountCallbackBridge?.capture(route);
|
|
605
|
+
const handler = callback?.handler ?? route.handler;
|
|
606
|
+
try {
|
|
607
|
+
const dispose = super.register({
|
|
608
|
+
...route,
|
|
609
|
+
handler: (req, res) => this.gate.http(req, res, () => Promise.resolve(handler(req, res)))
|
|
610
|
+
});
|
|
611
|
+
return () => {
|
|
612
|
+
dispose();
|
|
613
|
+
callback?.dispose();
|
|
614
|
+
};
|
|
615
|
+
} catch (error) {
|
|
616
|
+
callback?.dispose();
|
|
617
|
+
throw error;
|
|
618
|
+
}
|
|
579
619
|
}
|
|
580
620
|
registerFallback(handler) {
|
|
581
621
|
return super.registerFallback(
|
|
@@ -1063,10 +1103,11 @@ function issueSession(draft, now, sessionHours) {
|
|
|
1063
1103
|
function sessionFor(state, token, now, requireTotp) {
|
|
1064
1104
|
if (typeof token !== "string" || !/^[A-Za-z0-9_-]{43}$/u.test(token)) return null;
|
|
1065
1105
|
const session = state.sessions[sessionHash(token)];
|
|
1106
|
+
return session && isActiveSession(state, session, now, requireTotp) ? session : null;
|
|
1107
|
+
}
|
|
1108
|
+
function isActiveSession(state, session, now, requireTotp) {
|
|
1066
1109
|
const account = state.account;
|
|
1067
|
-
|
|
1068
|
-
return null;
|
|
1069
|
-
return session;
|
|
1110
|
+
return !!(account && session.accountId === account.id && session.securityVersion === account.securityVersion && session.expiresAt > now && (!requireTotp || account.totp));
|
|
1070
1111
|
}
|
|
1071
1112
|
|
|
1072
1113
|
// src/auth/totp.ts
|
|
@@ -1192,11 +1233,12 @@ var AuthService = class {
|
|
|
1192
1233
|
}
|
|
1193
1234
|
status() {
|
|
1194
1235
|
const state = this.store.current();
|
|
1236
|
+
const now = this.now();
|
|
1195
1237
|
return {
|
|
1196
1238
|
initialized: state.account !== null,
|
|
1197
1239
|
totpEnabled: state.account?.totp !== null && state.account?.totp !== void 0,
|
|
1198
1240
|
activeSessions: Object.values(state.sessions).filter(
|
|
1199
|
-
(session) => state
|
|
1241
|
+
(session) => isActiveSession(state, session, now, this.policy.requireTotp)
|
|
1200
1242
|
).length
|
|
1201
1243
|
};
|
|
1202
1244
|
}
|
|
@@ -1208,12 +1250,17 @@ var AuthService = class {
|
|
|
1208
1250
|
}
|
|
1209
1251
|
me(token) {
|
|
1210
1252
|
const state = this.store.current();
|
|
1211
|
-
const
|
|
1253
|
+
const now = this.now();
|
|
1254
|
+
const session = sessionFor(state, token, now, this.policy.requireTotp);
|
|
1212
1255
|
if (!session || !state.account) throw new AuthError("unauthorized");
|
|
1213
1256
|
return {
|
|
1214
1257
|
username: state.account.username,
|
|
1215
1258
|
totpEnabled: state.account.totp !== null,
|
|
1216
|
-
|
|
1259
|
+
createdAt: session.createdAt,
|
|
1260
|
+
expiresAt: session.expiresAt,
|
|
1261
|
+
activeSessions: Object.values(state.sessions).filter(
|
|
1262
|
+
(entry) => isActiveSession(state, entry, now, this.policy.requireTotp)
|
|
1263
|
+
).length
|
|
1217
1264
|
};
|
|
1218
1265
|
}
|
|
1219
1266
|
bindingDetails(id) {
|
|
@@ -1579,17 +1626,18 @@ var AuthService = class {
|
|
|
1579
1626
|
import z from "@deepseek-ai/schemastery";
|
|
1580
1627
|
import { isIP as isIP3 } from "node:net";
|
|
1581
1628
|
import { resolve } from "node:path";
|
|
1582
|
-
var DEFAULT_REQUIRE_TOTP =
|
|
1629
|
+
var DEFAULT_REQUIRE_TOTP = false;
|
|
1583
1630
|
var DEFAULT_SESSION_HOURS = 168;
|
|
1584
1631
|
var DEFAULT_WEB_HOST = "127.0.0.1";
|
|
1585
1632
|
var DEFAULT_WEB_PORT = 3080;
|
|
1586
1633
|
var AUTH_CONFIG_KEYS = /* @__PURE__ */ new Set([
|
|
1587
1634
|
"allowedOrigins",
|
|
1588
1635
|
"requireTotp",
|
|
1636
|
+
"manualAccountLogin",
|
|
1589
1637
|
"sessionHours",
|
|
1590
1638
|
"preserveOriginPaths"
|
|
1591
1639
|
]);
|
|
1592
|
-
var
|
|
1640
|
+
var StaticConfig = z.object({
|
|
1593
1641
|
publicUrl: z.string(),
|
|
1594
1642
|
host: z.string(),
|
|
1595
1643
|
port: z.number().step(1).min(0).max(65535),
|
|
@@ -1600,6 +1648,11 @@ var Config = z.object({
|
|
|
1600
1648
|
sessionHours: z.number().step(1).min(1).default(DEFAULT_SESSION_HOURS),
|
|
1601
1649
|
preserveOriginPaths: z.array(z.string()).default([])
|
|
1602
1650
|
});
|
|
1651
|
+
var ManualAccountLoginConfig = z.boolean().default(true).volatile();
|
|
1652
|
+
var Config = z.object({
|
|
1653
|
+
...StaticConfig.dict,
|
|
1654
|
+
manualAccountLogin: ManualAccountLoginConfig
|
|
1655
|
+
});
|
|
1603
1656
|
function resolveStartupConfig(input, startup, context) {
|
|
1604
1657
|
if (typeof input !== "object" || input === null || Array.isArray(input))
|
|
1605
1658
|
throw new Error("auth-remote: config is required");
|
|
@@ -1610,7 +1663,16 @@ function resolveStartupConfig(input, startup, context) {
|
|
|
1610
1663
|
const explicitUrl = startup.publicUrl !== void 0 || Object.hasOwn(input, "publicUrl");
|
|
1611
1664
|
if (legacyWarning && explicitUrl)
|
|
1612
1665
|
throw new Error("auth-remote: publicUrl and allowedOrigins cannot be supplied together");
|
|
1613
|
-
const
|
|
1666
|
+
const { manualAccountLogin: manual, ...ordinary } = input;
|
|
1667
|
+
const config = StaticConfig(ordinary);
|
|
1668
|
+
let manualAccountLogin;
|
|
1669
|
+
if (typeof manual === "object" && manual !== null) {
|
|
1670
|
+
if (typeof manual.get !== "function") throw new Error("auth-remote: invalid manualAccountLogin");
|
|
1671
|
+
z.boolean().required()(manual.get());
|
|
1672
|
+
manualAccountLogin = manual;
|
|
1673
|
+
} else {
|
|
1674
|
+
manualAccountLogin = ManualAccountLoginConfig(manual);
|
|
1675
|
+
}
|
|
1614
1676
|
const capabilities = getRuntimeCapabilities(context.version);
|
|
1615
1677
|
let publicUrl;
|
|
1616
1678
|
let publicUrlSource = "derived";
|
|
@@ -1670,6 +1732,7 @@ function resolveStartupConfig(input, startup, context) {
|
|
|
1670
1732
|
tlsSource,
|
|
1671
1733
|
legacyWarning,
|
|
1672
1734
|
requireTotp: config.requireTotp ?? DEFAULT_REQUIRE_TOTP,
|
|
1735
|
+
manualAccountLogin,
|
|
1673
1736
|
sessionHours,
|
|
1674
1737
|
preserveOriginPaths: Object.freeze([...paths])
|
|
1675
1738
|
};
|
|
@@ -2017,6 +2080,10 @@ function requireRequestContext(req) {
|
|
|
2017
2080
|
// src/http/routes.ts
|
|
2018
2081
|
import { readFileSync as readFileSync2 } from "node:fs";
|
|
2019
2082
|
var policies = {
|
|
2083
|
+
[`GET ${ACCOUNT_LOGIN_BASE}/state`]: "session",
|
|
2084
|
+
[`POST ${ACCOUNT_LOGIN_BASE}/start`]: "session",
|
|
2085
|
+
[`POST ${ACCOUNT_LOGIN_BASE}/submit`]: "session",
|
|
2086
|
+
[`POST ${ACCOUNT_LOGIN_BASE}/cancel`]: "session",
|
|
2020
2087
|
[`GET ${AUTH_BASE}/state`]: "public",
|
|
2021
2088
|
[`GET ${AUTH_BASE}/login`]: "public",
|
|
2022
2089
|
[`GET ${AUTH_BASE}/login.js`]: "public",
|
|
@@ -2167,6 +2234,23 @@ function errorStatus(error) {
|
|
|
2167
2234
|
}
|
|
2168
2235
|
return { status: 503, body: { error: "service_unavailable" } };
|
|
2169
2236
|
}
|
|
2237
|
+
function sendHttpError(req, res, error) {
|
|
2238
|
+
if (res.headersSent) {
|
|
2239
|
+
res.destroy();
|
|
2240
|
+
return;
|
|
2241
|
+
}
|
|
2242
|
+
const failure2 = errorStatus(error);
|
|
2243
|
+
const terminateBody = failure2.status === 413 || failure2.status === 408 || failure2.status === 415;
|
|
2244
|
+
sendJson(res, failure2.status, failure2.body, {
|
|
2245
|
+
...failure2.retryAfter ? { "retry-after": failure2.retryAfter } : {},
|
|
2246
|
+
...terminateBody ? { connection: "close" } : {}
|
|
2247
|
+
});
|
|
2248
|
+
if (terminateBody)
|
|
2249
|
+
res.once("finish", () => {
|
|
2250
|
+
req.resume();
|
|
2251
|
+
req.socket.end();
|
|
2252
|
+
});
|
|
2253
|
+
}
|
|
2170
2254
|
var AuthRoutes = class {
|
|
2171
2255
|
constructor(auth, profileName) {
|
|
2172
2256
|
this.auth = auth;
|
|
@@ -2175,7 +2259,9 @@ var AuthRoutes = class {
|
|
|
2175
2259
|
auth;
|
|
2176
2260
|
profileName;
|
|
2177
2261
|
install(server) {
|
|
2178
|
-
const paths = new Set(
|
|
2262
|
+
const paths = new Set(
|
|
2263
|
+
Object.keys(policies).map((row) => row.slice(row.indexOf(" ") + 1)).filter((path) => !path.startsWith(`${ACCOUNT_LOGIN_BASE}/`))
|
|
2264
|
+
);
|
|
2179
2265
|
for (const path of paths)
|
|
2180
2266
|
server.register({
|
|
2181
2267
|
kind: "exact",
|
|
@@ -2208,7 +2294,7 @@ var AuthRoutes = class {
|
|
|
2208
2294
|
"referrer-policy": "no-referrer"
|
|
2209
2295
|
});
|
|
2210
2296
|
res.end(
|
|
2211
|
-
'<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Sign in
|
|
2297
|
+
'<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Sign in</title><link rel="manifest" href="/manifest.webmanifest"><link rel="stylesheet" href="/auth-remote/login.css"><script defer src="/auth-remote/login.js"></script></head><body><main id="login-root" aria-live="polite">Checking sign-in status\u2026</main></body></html>'
|
|
2212
2298
|
);
|
|
2213
2299
|
return;
|
|
2214
2300
|
}
|
|
@@ -2370,21 +2456,7 @@ var AuthRoutes = class {
|
|
|
2370
2456
|
sendJson(res, 405, { error: "method_not_allowed" });
|
|
2371
2457
|
}
|
|
2372
2458
|
} catch (error) {
|
|
2373
|
-
|
|
2374
|
-
res.destroy();
|
|
2375
|
-
return;
|
|
2376
|
-
}
|
|
2377
|
-
const failure2 = errorStatus(error);
|
|
2378
|
-
const terminateBody = failure2.status === 413 || failure2.status === 408 || failure2.status === 415;
|
|
2379
|
-
sendJson(res, failure2.status, failure2.body, {
|
|
2380
|
-
...failure2.retryAfter ? { "retry-after": failure2.retryAfter } : {},
|
|
2381
|
-
...terminateBody ? { connection: "close" } : {}
|
|
2382
|
-
});
|
|
2383
|
-
if (terminateBody)
|
|
2384
|
-
res.once("finish", () => {
|
|
2385
|
-
req.resume();
|
|
2386
|
-
req.socket.end();
|
|
2387
|
-
});
|
|
2459
|
+
sendHttpError(req, res, error);
|
|
2388
2460
|
}
|
|
2389
2461
|
}
|
|
2390
2462
|
};
|
|
@@ -2510,8 +2582,341 @@ var AuthGate = class {
|
|
|
2510
2582
|
}
|
|
2511
2583
|
};
|
|
2512
2584
|
|
|
2585
|
+
// src/http/account-login.ts
|
|
2586
|
+
import { randomInt } from "node:crypto";
|
|
2587
|
+
|
|
2588
|
+
// src/adapters/dsh/account-callback.ts
|
|
2589
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
2590
|
+
import { Duplex } from "node:stream";
|
|
2591
|
+
import { finished } from "node:stream/promises";
|
|
2592
|
+
var AccountCallbackBridge = class {
|
|
2593
|
+
constructor(ownsCallback = () => true) {
|
|
2594
|
+
this.ownsCallback = ownsCallback;
|
|
2595
|
+
}
|
|
2596
|
+
ownsCallback;
|
|
2597
|
+
handler;
|
|
2598
|
+
get available() {
|
|
2599
|
+
return this.handler !== void 0;
|
|
2600
|
+
}
|
|
2601
|
+
capture(route) {
|
|
2602
|
+
if (route.kind !== "exact" || route.path !== ACCOUNT_CALLBACK_PATH) return;
|
|
2603
|
+
const handler = route.handler;
|
|
2604
|
+
this.handler = handler;
|
|
2605
|
+
return {
|
|
2606
|
+
handler: async (req, res) => {
|
|
2607
|
+
if (await this.ownsCallback()) res.writeHead(403, { "cache-control": "no-store" }).end();
|
|
2608
|
+
else return handler(req, res);
|
|
2609
|
+
},
|
|
2610
|
+
dispose: () => {
|
|
2611
|
+
if (this.handler === handler) this.handler = void 0;
|
|
2612
|
+
}
|
|
2613
|
+
};
|
|
2614
|
+
}
|
|
2615
|
+
/** Use Node's actual response lifecycle: DSH awaits its completion during cleanup. */
|
|
2616
|
+
async submit(query) {
|
|
2617
|
+
const handler = this.handler;
|
|
2618
|
+
if (!handler) return 410;
|
|
2619
|
+
const socket = new Duplex({
|
|
2620
|
+
read() {
|
|
2621
|
+
},
|
|
2622
|
+
write(_chunk, _encoding, done) {
|
|
2623
|
+
done();
|
|
2624
|
+
}
|
|
2625
|
+
});
|
|
2626
|
+
const request = new IncomingMessage(socket);
|
|
2627
|
+
request.method = "GET";
|
|
2628
|
+
request.url = `${ACCOUNT_CALLBACK_PATH}?${query}`;
|
|
2629
|
+
const response = new ServerResponse(request);
|
|
2630
|
+
response.assignSocket(socket);
|
|
2631
|
+
response.once("finish", () => socket.destroy());
|
|
2632
|
+
const completed = finished(response, { cleanup: true });
|
|
2633
|
+
void completed.catch(() => void 0);
|
|
2634
|
+
try {
|
|
2635
|
+
await handler(request, response);
|
|
2636
|
+
await completed;
|
|
2637
|
+
return response.statusCode;
|
|
2638
|
+
} finally {
|
|
2639
|
+
response.destroy();
|
|
2640
|
+
socket.destroy();
|
|
2641
|
+
}
|
|
2642
|
+
}
|
|
2643
|
+
};
|
|
2644
|
+
|
|
2645
|
+
// src/http/account-login.ts
|
|
2646
|
+
var ManualAccountLogin = class {
|
|
2647
|
+
constructor(ctx, auth, store, version, enabled = () => true) {
|
|
2648
|
+
this.auth = auth;
|
|
2649
|
+
this.version = version;
|
|
2650
|
+
this.enabled = enabled;
|
|
2651
|
+
this.unsubscribe = store.subscribe((_before, after) => this.reconcile(after));
|
|
2652
|
+
if (!supportsManualAccountLogin(version)) return;
|
|
2653
|
+
ctx.inject(["deepseekAccount"], (accountCtx) => {
|
|
2654
|
+
const provider = accountCtx.get("deepseekAccount");
|
|
2655
|
+
accountCtx.effect(() => {
|
|
2656
|
+
this.currentProvider = provider;
|
|
2657
|
+
return async () => {
|
|
2658
|
+
if (this.currentProvider === provider) this.currentProvider = void 0;
|
|
2659
|
+
if (this.active?.provider === provider) await this.cancelAttempt(this.active);
|
|
2660
|
+
};
|
|
2661
|
+
}, "auth-remote: account provider lifetime");
|
|
2662
|
+
});
|
|
2663
|
+
}
|
|
2664
|
+
auth;
|
|
2665
|
+
version;
|
|
2666
|
+
enabled;
|
|
2667
|
+
bridge = new AccountCallbackBridge(() => this.ownsNativeCallback());
|
|
2668
|
+
unsubscribe;
|
|
2669
|
+
active;
|
|
2670
|
+
starting = false;
|
|
2671
|
+
disposed = false;
|
|
2672
|
+
currentProvider;
|
|
2673
|
+
disposal;
|
|
2674
|
+
install(server) {
|
|
2675
|
+
if (supportsManualAccountLogin(this.version)) server.installAccountCallbackBridge(this.bridge);
|
|
2676
|
+
for (const action of ["state", "start", "submit", "cancel"])
|
|
2677
|
+
server.register({
|
|
2678
|
+
kind: "exact",
|
|
2679
|
+
path: `${ACCOUNT_LOGIN_BASE}/${action}`,
|
|
2680
|
+
handler: (req, res) => this.handle(req, res, action)
|
|
2681
|
+
});
|
|
2682
|
+
}
|
|
2683
|
+
provider() {
|
|
2684
|
+
const provider = this.currentProvider;
|
|
2685
|
+
return provider && typeof provider.getState === "function" && typeof provider.startSignIn === "function" && typeof provider.cancelSignIn === "function" ? provider : void 0;
|
|
2686
|
+
}
|
|
2687
|
+
async ownsNativeCallback() {
|
|
2688
|
+
const active = this.active;
|
|
2689
|
+
if (!active?.ownsCallback) return false;
|
|
2690
|
+
if (!active.id) return true;
|
|
2691
|
+
try {
|
|
2692
|
+
const view = await active.provider.getState();
|
|
2693
|
+
if (this.active !== active) return this.active?.ownsCallback === true;
|
|
2694
|
+
if (!active.ownsCallback) return false;
|
|
2695
|
+
if (view.attempt?.id === active.id && isActiveAccountLogin(view.attempt.phase)) return true;
|
|
2696
|
+
active.ownsCallback = false;
|
|
2697
|
+
return false;
|
|
2698
|
+
} catch {
|
|
2699
|
+
return true;
|
|
2700
|
+
}
|
|
2701
|
+
}
|
|
2702
|
+
session(token) {
|
|
2703
|
+
const session = token && !this.disposed ? this.auth.session(token) : null;
|
|
2704
|
+
if (!token || !session) throw new AuthError("unauthorized");
|
|
2705
|
+
return { hash: sessionHash(token), session };
|
|
2706
|
+
}
|
|
2707
|
+
owned(token, id) {
|
|
2708
|
+
const { hash } = this.session(token);
|
|
2709
|
+
const active = this.active;
|
|
2710
|
+
if (!active || active.owner !== hash || active.id !== id)
|
|
2711
|
+
throw new HttpFailure(409, "account_attempt_mismatch");
|
|
2712
|
+
if (active.cancelled || active.deadline <= Date.now())
|
|
2713
|
+
throw new HttpFailure(410, "account_attempt_expired");
|
|
2714
|
+
if (this.provider() !== active.provider) throw new HttpFailure(409, "account_attempt_mismatch");
|
|
2715
|
+
return active;
|
|
2716
|
+
}
|
|
2717
|
+
schedule(attempt) {
|
|
2718
|
+
if (attempt.timer) clearTimeout(attempt.timer);
|
|
2719
|
+
attempt.timer = setTimeout(
|
|
2720
|
+
() => void this.cancelAttempt(attempt).catch(() => {
|
|
2721
|
+
}),
|
|
2722
|
+
Math.max(1, attempt.deadline - Date.now())
|
|
2723
|
+
);
|
|
2724
|
+
attempt.timer.unref();
|
|
2725
|
+
}
|
|
2726
|
+
async state(token) {
|
|
2727
|
+
const { hash } = this.session(token);
|
|
2728
|
+
const provider = this.provider();
|
|
2729
|
+
if (!provider)
|
|
2730
|
+
return { supported: false, enabled: this.enabled(), status: "signed-out", attempt: null };
|
|
2731
|
+
const view = await provider.getState();
|
|
2732
|
+
this.session(token);
|
|
2733
|
+
const active = this.active;
|
|
2734
|
+
if (!active || active.owner !== hash || active.provider !== provider || active.id !== view.attempt?.id)
|
|
2735
|
+
return { supported: true, enabled: this.enabled(), status: view.status, attempt: null };
|
|
2736
|
+
const attempt = view.attempt;
|
|
2737
|
+
if (attempt?.phase === "waiting-browser" && !this.bridge.available)
|
|
2738
|
+
throw new HttpFailure(503, "account_unavailable");
|
|
2739
|
+
if (attempt?.expiresAt !== void 0 && attempt.expiresAt < active.deadline) {
|
|
2740
|
+
active.deadline = attempt.expiresAt;
|
|
2741
|
+
this.schedule(active);
|
|
2742
|
+
}
|
|
2743
|
+
if (attempt && !isActiveAccountLogin(attempt.phase)) {
|
|
2744
|
+
if (active.timer) clearTimeout(active.timer);
|
|
2745
|
+
active.ownsCallback = false;
|
|
2746
|
+
}
|
|
2747
|
+
return {
|
|
2748
|
+
supported: true,
|
|
2749
|
+
enabled: this.enabled(),
|
|
2750
|
+
status: view.status,
|
|
2751
|
+
attempt: attempt && { ...attempt, expiresAt: active.deadline }
|
|
2752
|
+
};
|
|
2753
|
+
}
|
|
2754
|
+
async start(token, body) {
|
|
2755
|
+
const owner = this.session(token);
|
|
2756
|
+
const provider = this.provider();
|
|
2757
|
+
if (!provider) throw new HttpFailure(503, "account_unavailable");
|
|
2758
|
+
if (!this.enabled()) throw new HttpFailure(403, "account_disabled");
|
|
2759
|
+
const locale = stringField(body, "locale");
|
|
2760
|
+
const offset = body.timezoneOffsetSeconds;
|
|
2761
|
+
if (locale.length > 64 || !/^[a-z][a-z0-9_-]*$/iu.test(locale) || !Number.isInteger(offset) || Math.abs(offset) > 86400)
|
|
2762
|
+
throw new HttpFailure(400, "invalid_input");
|
|
2763
|
+
if (this.starting) throw new HttpFailure(409, "account_busy");
|
|
2764
|
+
this.starting = true;
|
|
2765
|
+
let entry;
|
|
2766
|
+
try {
|
|
2767
|
+
const previous = await provider.getState();
|
|
2768
|
+
if (previous.attempt && isActiveAccountLogin(previous.attempt.phase))
|
|
2769
|
+
throw new HttpFailure(409, "account_busy");
|
|
2770
|
+
if (previous.status === "credential-stored")
|
|
2771
|
+
return { ...previous, supported: true, enabled: this.enabled(), attempt: null };
|
|
2772
|
+
if (this.active?.timer) clearTimeout(this.active.timer);
|
|
2773
|
+
entry = {
|
|
2774
|
+
owner: owner.hash,
|
|
2775
|
+
provider,
|
|
2776
|
+
callbackOrigin: `http://127.0.0.1:${randomInt(49152, 65536)}`,
|
|
2777
|
+
deadline: Math.min(Date.now() + 6e5, owner.session.expiresAt),
|
|
2778
|
+
cancelled: false,
|
|
2779
|
+
submitting: false,
|
|
2780
|
+
ownsCallback: true
|
|
2781
|
+
};
|
|
2782
|
+
this.active = entry;
|
|
2783
|
+
this.schedule(entry);
|
|
2784
|
+
const view = await provider.startSignIn(
|
|
2785
|
+
{ version: this.version, locale, timezoneOffsetSeconds: offset },
|
|
2786
|
+
entry.callbackOrigin,
|
|
2787
|
+
"web"
|
|
2788
|
+
);
|
|
2789
|
+
if (!view.attempt?.id) throw new HttpFailure(503, "account_unavailable");
|
|
2790
|
+
entry.id = view.attempt.id;
|
|
2791
|
+
if (entry.cancelled || !token || !this.auth.session(token) || this.disposed) {
|
|
2792
|
+
await this.cancelAttempt(entry);
|
|
2793
|
+
throw new AuthError("unauthorized");
|
|
2794
|
+
}
|
|
2795
|
+
return await this.state(token);
|
|
2796
|
+
} catch (error) {
|
|
2797
|
+
if (entry) await this.cancelAttempt(entry);
|
|
2798
|
+
throw error;
|
|
2799
|
+
} finally {
|
|
2800
|
+
this.starting = false;
|
|
2801
|
+
if (entry?.cancelled && !entry.id) entry.ownsCallback = false;
|
|
2802
|
+
}
|
|
2803
|
+
}
|
|
2804
|
+
callback(value, active) {
|
|
2805
|
+
if (value.length > 8192 || /[\u0000-\u0020\u007f]/u.test(value.trim()))
|
|
2806
|
+
throw new HttpFailure(400, "account_invalid_callback");
|
|
2807
|
+
let url;
|
|
2808
|
+
try {
|
|
2809
|
+
url = new URL(value.trim());
|
|
2810
|
+
} catch {
|
|
2811
|
+
throw new HttpFailure(400, "account_invalid_callback");
|
|
2812
|
+
}
|
|
2813
|
+
if (url.origin !== active.callbackOrigin || url.pathname !== ACCOUNT_CALLBACK_PATH || url.username || url.password || url.hash || url.searchParams.getAll("code").length !== 1 || !url.searchParams.get("code") || url.searchParams.getAll("state").length !== 1 || !url.searchParams.get("state") || [...url.searchParams.keys()].some((key) => key !== "code" && key !== "state"))
|
|
2814
|
+
throw new HttpFailure(400, "account_invalid_callback");
|
|
2815
|
+
return url.searchParams;
|
|
2816
|
+
}
|
|
2817
|
+
async submit(token, body) {
|
|
2818
|
+
const active = this.owned(token, stringField(body, "id"));
|
|
2819
|
+
const query = this.callback(stringField(body, "callbackUrl"), active);
|
|
2820
|
+
if (active.submitting) throw new HttpFailure(409, "account_busy");
|
|
2821
|
+
active.submitting = true;
|
|
2822
|
+
try {
|
|
2823
|
+
const before = await active.provider.getState();
|
|
2824
|
+
if (!before.attempt || before.attempt.id !== active.id || before.attempt.phase !== "waiting-browser")
|
|
2825
|
+
throw new HttpFailure(409, "account_attempt_mismatch");
|
|
2826
|
+
this.owned(token, active.id);
|
|
2827
|
+
if (!this.bridge.available) throw new HttpFailure(503, "account_unavailable");
|
|
2828
|
+
const status = await this.bridge.submit(query);
|
|
2829
|
+
if (status === 400) throw new HttpFailure(400, "account_invalid_callback");
|
|
2830
|
+
const state = await this.state(token);
|
|
2831
|
+
if (state.status !== "credential-stored" || state.attempt?.phase !== "succeeded")
|
|
2832
|
+
throw new HttpFailure(
|
|
2833
|
+
status === 410 ? 410 : 409,
|
|
2834
|
+
state.attempt?.phase === "expired" ? "account_attempt_expired" : "account_exchange_failed"
|
|
2835
|
+
);
|
|
2836
|
+
return state;
|
|
2837
|
+
} finally {
|
|
2838
|
+
active.submitting = false;
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
async cancelAttempt(attempt) {
|
|
2842
|
+
attempt.cancelled = true;
|
|
2843
|
+
if (attempt.timer) clearTimeout(attempt.timer);
|
|
2844
|
+
if (attempt.id) {
|
|
2845
|
+
const cancellation = attempt.cancellation ??= Promise.resolve().then(async () => {
|
|
2846
|
+
await attempt.provider.cancelSignIn(attempt.id);
|
|
2847
|
+
});
|
|
2848
|
+
try {
|
|
2849
|
+
await cancellation;
|
|
2850
|
+
} catch (error) {
|
|
2851
|
+
if (attempt.cancellation === cancellation) delete attempt.cancellation;
|
|
2852
|
+
throw error;
|
|
2853
|
+
}
|
|
2854
|
+
}
|
|
2855
|
+
}
|
|
2856
|
+
async reconcile(state) {
|
|
2857
|
+
const active = this.active;
|
|
2858
|
+
if (!active || active.cancelled) return;
|
|
2859
|
+
const session = state.sessions[active.owner];
|
|
2860
|
+
const account = state.account;
|
|
2861
|
+
if (!session || !account || session.accountId !== account.id || session.securityVersion !== account.securityVersion || session.expiresAt <= Date.now() || this.auth.policySnapshot().requireTotp && !account.totp) {
|
|
2862
|
+
await this.cancelAttempt(active);
|
|
2863
|
+
} else {
|
|
2864
|
+
active.deadline = Math.min(active.deadline, session.expiresAt);
|
|
2865
|
+
this.schedule(active);
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
async handle(req, res, action) {
|
|
2869
|
+
try {
|
|
2870
|
+
const token = currentSessionToken(req);
|
|
2871
|
+
this.session(token);
|
|
2872
|
+
const expected = action === "state" ? "GET" : "POST";
|
|
2873
|
+
if (req.method !== expected) throw new HttpFailure(405, "method_not_allowed");
|
|
2874
|
+
if (action === "state") {
|
|
2875
|
+
sendJson(res, 200, await this.state(token));
|
|
2876
|
+
return;
|
|
2877
|
+
}
|
|
2878
|
+
const body = await readJsonBody(req);
|
|
2879
|
+
switch (action) {
|
|
2880
|
+
case "start":
|
|
2881
|
+
sendJson(res, 200, await this.start(token, body));
|
|
2882
|
+
break;
|
|
2883
|
+
case "submit":
|
|
2884
|
+
sendJson(res, 200, await this.submit(token, body));
|
|
2885
|
+
break;
|
|
2886
|
+
case "cancel": {
|
|
2887
|
+
const active = this.owned(token, stringField(body, "id"));
|
|
2888
|
+
await this.cancelAttempt(active);
|
|
2889
|
+
sendJson(res, 200, await this.state(token));
|
|
2890
|
+
break;
|
|
2891
|
+
}
|
|
2892
|
+
}
|
|
2893
|
+
} catch (error) {
|
|
2894
|
+
sendHttpError(req, res, error);
|
|
2895
|
+
}
|
|
2896
|
+
}
|
|
2897
|
+
dispose() {
|
|
2898
|
+
return this.disposal ??= this.disposeAttempt();
|
|
2899
|
+
}
|
|
2900
|
+
async disposeAttempt() {
|
|
2901
|
+
this.disposed = true;
|
|
2902
|
+
this.unsubscribe();
|
|
2903
|
+
if (this.active) await this.cancelAttempt(this.active);
|
|
2904
|
+
}
|
|
2905
|
+
};
|
|
2906
|
+
|
|
2513
2907
|
// src/index.ts
|
|
2514
2908
|
var name = "auth-remote";
|
|
2909
|
+
async function disposeResources(disposers) {
|
|
2910
|
+
const errors = [];
|
|
2911
|
+
for (const dispose of disposers) {
|
|
2912
|
+
try {
|
|
2913
|
+
await dispose();
|
|
2914
|
+
} catch (error) {
|
|
2915
|
+
errors.push(error);
|
|
2916
|
+
}
|
|
2917
|
+
}
|
|
2918
|
+
if (errors.length) throw new AggregateError(errors, "auth-remote: resource cleanup failed");
|
|
2919
|
+
}
|
|
2515
2920
|
var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
2516
2921
|
static Config = Config;
|
|
2517
2922
|
stateStore;
|
|
@@ -2533,7 +2938,7 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2533
2938
|
super(ctx, config, gate);
|
|
2534
2939
|
this.pending = config;
|
|
2535
2940
|
this.switchGate = gate;
|
|
2536
|
-
installAuthenticatedHostCapability(ctx);
|
|
2941
|
+
installAuthenticatedHostCapability(ctx, config.capabilities.version);
|
|
2537
2942
|
if (config.legacyWarning)
|
|
2538
2943
|
console.warn("auth-remote: allowedOrigins is deprecated; use publicUrl with one root URL");
|
|
2539
2944
|
}
|
|
@@ -2544,10 +2949,13 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2544
2949
|
let readiness;
|
|
2545
2950
|
let connections;
|
|
2546
2951
|
let admin;
|
|
2952
|
+
let accountLogin;
|
|
2547
2953
|
const store = await AuthStateStore.open(profile.dir, {
|
|
2548
2954
|
onUnavailable: () => {
|
|
2549
2955
|
this.switchGate.set(new DenyGate());
|
|
2550
2956
|
readiness?.detach();
|
|
2957
|
+
void accountLogin?.dispose().catch(() => {
|
|
2958
|
+
});
|
|
2551
2959
|
if (connections) void connections.closeAll();
|
|
2552
2960
|
}
|
|
2553
2961
|
});
|
|
@@ -2559,10 +2967,12 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2559
2967
|
await auth.enforceCurrentPolicy();
|
|
2560
2968
|
this.stateStore = store;
|
|
2561
2969
|
this.authService = auth;
|
|
2562
|
-
await super[Service.init]();
|
|
2970
|
+
await outsideHmrTransaction(this.ctx, () => super[Service.init]());
|
|
2971
|
+
const port = this.port;
|
|
2972
|
+
if (port === void 0) throw new Error("auth-remote: listener has not bound a port");
|
|
2563
2973
|
this.resolvedConfig = finalizeAuthConfig(this.pending, {
|
|
2564
2974
|
host: this.host,
|
|
2565
|
-
port
|
|
2975
|
+
port,
|
|
2566
2976
|
protocol: listenerProtocol(this, this.pending.capabilities)
|
|
2567
2977
|
});
|
|
2568
2978
|
this.setOriginPolicy(this.resolved.originPolicy);
|
|
@@ -2572,6 +2982,14 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2572
2982
|
});
|
|
2573
2983
|
this.switchGate.set(new AuthGate(this.resolved, store, auth, readiness, connections));
|
|
2574
2984
|
new AuthRoutes(auth, profileName).install(this);
|
|
2985
|
+
accountLogin = new ManualAccountLogin(
|
|
2986
|
+
this.ctx,
|
|
2987
|
+
auth,
|
|
2988
|
+
store,
|
|
2989
|
+
this.pending.capabilities.version,
|
|
2990
|
+
() => this.pending.manualAccountLogin.get()
|
|
2991
|
+
);
|
|
2992
|
+
accountLogin.install(this);
|
|
2575
2993
|
admin = new AdminSocket(
|
|
2576
2994
|
profile.dir,
|
|
2577
2995
|
auth,
|
|
@@ -2586,14 +3004,18 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2586
3004
|
const activeConnections = connections;
|
|
2587
3005
|
const activeReadiness = readiness;
|
|
2588
3006
|
const activeAdmin = admin;
|
|
3007
|
+
const activeAccountLogin = accountLogin;
|
|
2589
3008
|
this.ctx.effect(
|
|
2590
3009
|
() => async () => {
|
|
2591
3010
|
this.switchGate.set(new DenyGate());
|
|
2592
3011
|
activeReadiness.detach();
|
|
2593
|
-
await
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
3012
|
+
await disposeResources([
|
|
3013
|
+
() => activeAccountLogin.dispose(),
|
|
3014
|
+
() => activeAdmin.dispose(),
|
|
3015
|
+
() => activeConnections.dispose(),
|
|
3016
|
+
() => auth.dispose(),
|
|
3017
|
+
() => store.close()
|
|
3018
|
+
]);
|
|
2597
3019
|
},
|
|
2598
3020
|
"auth-remote.storage-and-gate"
|
|
2599
3021
|
);
|
|
@@ -2619,10 +3041,18 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2619
3041
|
);
|
|
2620
3042
|
} catch (error) {
|
|
2621
3043
|
this.switchGate.set(new DenyGate());
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
3044
|
+
readiness?.detach();
|
|
3045
|
+
try {
|
|
3046
|
+
await disposeResources([
|
|
3047
|
+
() => accountLogin?.dispose(),
|
|
3048
|
+
() => admin?.dispose(),
|
|
3049
|
+
() => connections?.dispose(),
|
|
3050
|
+
() => this.authService?.dispose(),
|
|
3051
|
+
() => store.close()
|
|
3052
|
+
]);
|
|
3053
|
+
} catch (cleanupError) {
|
|
3054
|
+
throw new AggregateError([error, cleanupError], "auth-remote: startup and cleanup failed");
|
|
3055
|
+
}
|
|
2626
3056
|
throw error;
|
|
2627
3057
|
}
|
|
2628
3058
|
}
|