dsh-auth-remote 0.1.7 → 0.1.9
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 +12 -1
- package/CHANGELOG.zh-CN.md +12 -1
- package/CONTRIBUTING.md +1 -1
- package/CONTRIBUTING.zh-CN.md +1 -1
- package/README.md +30 -12
- package/README.zh-CN.md +30 -12
- package/dist/cli.js +50 -0
- package/dist/cli.js.map +2 -2
- package/dist/client.js +457 -1
- package/dist/client.js.map +4 -4
- package/dist/index.js +366 -26
- package/dist/index.js.map +3 -3
- package/dist/login.js +50 -0
- package/dist/login.js.map +2 -2
- package/dist/types/adapters/dsh/account-callback.d.ts +12 -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/webserver.d.ts +3 -0
- package/dist/types/adapters/dsh/webserver.d.ts.map +1 -1
- package/dist/types/http/account-login.d.ts +43 -0
- package/dist/types/http/account-login.d.ts.map +1 -0
- package/dist/types/http/gate.d.ts.map +1 -1
- package/dist/types/http/origin-policy.d.ts.map +1 -1
- package/dist/types/http/public-assets.d.ts +3 -0
- package/dist/types/http/public-assets.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.map +1 -1
- package/dist/types/shared/account-login.d.ts +21 -0
- package/dist/types/shared/account-login.d.ts.map +1 -0
- package/dist/types/shared/i18n.d.ts +50 -0
- package/dist/types/shared/i18n.d.ts.map +1 -1
- package/dist/types/shared/locales/en.d.ts +25 -0
- package/dist/types/shared/locales/en.d.ts.map +1 -1
- package/dist/types/shared/locales/zh.d.ts +25 -0
- package/dist/types/shared/locales/zh.d.ts.map +1 -1
- package/docs/deployment.md +21 -15
- package/docs/deployment.zh-CN.md +21 -15
- package/docs/development.md +16 -6
- package/docs/development.zh-CN.md +16 -6
- package/docs/legacy-compatibility.md +170 -117
- package/docs/legacy-compatibility.zh-CN.md +170 -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/package.json +25 -11
package/dist/index.js
CHANGED
|
@@ -59,10 +59,23 @@ function assertConnectionCompatibility(connection, anchor) {
|
|
|
59
59
|
return own.manifest.version;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
// src/shared/account-login.ts
|
|
63
|
+
var ACCOUNT_LOGIN_BASE = "/auth-remote/account";
|
|
64
|
+
var ACCOUNT_CALLBACK_PATH = "/oauth/callback";
|
|
65
|
+
var ACCOUNT_LOGIN_CAPABILITY = "__DSH_AUTH_REMOTE_MANUAL_ACCOUNT_LOGIN__";
|
|
66
|
+
function supportsManualAccountLogin(version) {
|
|
67
|
+
return version === "0.2.1-alpha.3";
|
|
68
|
+
}
|
|
69
|
+
function isActiveAccountLogin(phase) {
|
|
70
|
+
return ["initializing", "waiting-browser", "exchanging", "committing"].includes(phase);
|
|
71
|
+
}
|
|
72
|
+
|
|
62
73
|
// src/adapters/dsh/browser-capability.ts
|
|
63
|
-
function installAuthenticatedHostCapability(ctx) {
|
|
74
|
+
function installAuthenticatedHostCapability(ctx, version) {
|
|
64
75
|
ctx.on("webserver/index-inject", (table) => {
|
|
65
76
|
table.push({ kind: "global", name: "__DSH_TRANSPORT__", value: { ownsHost: true } });
|
|
77
|
+
if (version && supportsManualAccountLogin(version))
|
|
78
|
+
table.push({ kind: "global", name: ACCOUNT_LOGIN_CAPABILITY, value: true });
|
|
66
79
|
});
|
|
67
80
|
}
|
|
68
81
|
|
|
@@ -227,6 +240,12 @@ import semver2 from "semver";
|
|
|
227
240
|
// src/adapters/dsh/public-url.ts
|
|
228
241
|
import { isIP } from "node:net";
|
|
229
242
|
|
|
243
|
+
// src/http/public-assets.ts
|
|
244
|
+
var publicPwaPaths = /* @__PURE__ */ new Set(["/manifest.webmanifest", "/favicon.svg", "/favicon-dark.svg"]);
|
|
245
|
+
function isPublicPwaAsset(method, pathname) {
|
|
246
|
+
return (method === "GET" || method === "HEAD") && publicPwaPaths.has(pathname);
|
|
247
|
+
}
|
|
248
|
+
|
|
230
249
|
// src/http/origin-policy.ts
|
|
231
250
|
function defaultPort(protocol) {
|
|
232
251
|
return protocol === "https:" ? 443 : 80;
|
|
@@ -375,7 +394,7 @@ function evaluateRequestOrigin(policy, request) {
|
|
|
375
394
|
const target = request.target ? parseTarget(request.target) : null;
|
|
376
395
|
const method = request.method;
|
|
377
396
|
if (!authority || !target || !method || !/^[A-Z!#$%&'*+.^_`|~-]+$/u.test(method)) return null;
|
|
378
|
-
const needsOrigin = request.upgrade === true || method !== "GET";
|
|
397
|
+
const needsOrigin = request.upgrade === true || method !== "GET" && !isPublicPwaAsset(method, target.pathname);
|
|
379
398
|
if (needsOrigin && origins.length !== 1) return null;
|
|
380
399
|
const site = fetchSites[0]?.toLowerCase();
|
|
381
400
|
if (site !== void 0 && (needsOrigin ? site !== "same-origin" : !["same-origin", "none"].includes(site)))
|
|
@@ -561,15 +580,30 @@ var GuardedWebServer = class extends WebServer2 {
|
|
|
561
580
|
}
|
|
562
581
|
gate;
|
|
563
582
|
originPolicy;
|
|
583
|
+
accountCallbackBridge;
|
|
564
584
|
/** Ready and the authenticated gate consume the same listener-finalized policy. */
|
|
565
585
|
setOriginPolicy(policy) {
|
|
566
586
|
this.originPolicy = policy;
|
|
567
587
|
}
|
|
588
|
+
installAccountCallbackBridge(bridge) {
|
|
589
|
+
this.accountCallbackBridge = bridge;
|
|
590
|
+
}
|
|
568
591
|
register(route) {
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
592
|
+
const callback = this.accountCallbackBridge?.capture(route);
|
|
593
|
+
const handler = callback?.handler ?? route.handler;
|
|
594
|
+
try {
|
|
595
|
+
const dispose = super.register({
|
|
596
|
+
...route,
|
|
597
|
+
handler: (req, res) => this.gate.http(req, res, () => Promise.resolve(handler(req, res)))
|
|
598
|
+
});
|
|
599
|
+
return () => {
|
|
600
|
+
dispose();
|
|
601
|
+
callback?.dispose();
|
|
602
|
+
};
|
|
603
|
+
} catch (error) {
|
|
604
|
+
callback?.dispose();
|
|
605
|
+
throw error;
|
|
606
|
+
}
|
|
573
607
|
}
|
|
574
608
|
registerFallback(handler) {
|
|
575
609
|
return super.registerFallback(
|
|
@@ -2011,6 +2045,10 @@ function requireRequestContext(req) {
|
|
|
2011
2045
|
// src/http/routes.ts
|
|
2012
2046
|
import { readFileSync as readFileSync2 } from "node:fs";
|
|
2013
2047
|
var policies = {
|
|
2048
|
+
[`GET ${ACCOUNT_LOGIN_BASE}/state`]: "session",
|
|
2049
|
+
[`POST ${ACCOUNT_LOGIN_BASE}/start`]: "session",
|
|
2050
|
+
[`POST ${ACCOUNT_LOGIN_BASE}/submit`]: "session",
|
|
2051
|
+
[`POST ${ACCOUNT_LOGIN_BASE}/cancel`]: "session",
|
|
2014
2052
|
[`GET ${AUTH_BASE}/state`]: "public",
|
|
2015
2053
|
[`GET ${AUTH_BASE}/login`]: "public",
|
|
2016
2054
|
[`GET ${AUTH_BASE}/login.js`]: "public",
|
|
@@ -2161,6 +2199,23 @@ function errorStatus(error) {
|
|
|
2161
2199
|
}
|
|
2162
2200
|
return { status: 503, body: { error: "service_unavailable" } };
|
|
2163
2201
|
}
|
|
2202
|
+
function sendHttpError(req, res, error) {
|
|
2203
|
+
if (res.headersSent) {
|
|
2204
|
+
res.destroy();
|
|
2205
|
+
return;
|
|
2206
|
+
}
|
|
2207
|
+
const failure2 = errorStatus(error);
|
|
2208
|
+
const terminateBody = failure2.status === 413 || failure2.status === 408 || failure2.status === 415;
|
|
2209
|
+
sendJson(res, failure2.status, failure2.body, {
|
|
2210
|
+
...failure2.retryAfter ? { "retry-after": failure2.retryAfter } : {},
|
|
2211
|
+
...terminateBody ? { connection: "close" } : {}
|
|
2212
|
+
});
|
|
2213
|
+
if (terminateBody)
|
|
2214
|
+
res.once("finish", () => {
|
|
2215
|
+
req.resume();
|
|
2216
|
+
req.socket.end();
|
|
2217
|
+
});
|
|
2218
|
+
}
|
|
2164
2219
|
var AuthRoutes = class {
|
|
2165
2220
|
constructor(auth, profileName) {
|
|
2166
2221
|
this.auth = auth;
|
|
@@ -2169,7 +2224,9 @@ var AuthRoutes = class {
|
|
|
2169
2224
|
auth;
|
|
2170
2225
|
profileName;
|
|
2171
2226
|
install(server) {
|
|
2172
|
-
const paths = new Set(
|
|
2227
|
+
const paths = new Set(
|
|
2228
|
+
Object.keys(policies).map((row) => row.slice(row.indexOf(" ") + 1)).filter((path) => !path.startsWith(`${ACCOUNT_LOGIN_BASE}/`))
|
|
2229
|
+
);
|
|
2173
2230
|
for (const path of paths)
|
|
2174
2231
|
server.register({
|
|
2175
2232
|
kind: "exact",
|
|
@@ -2197,12 +2254,12 @@ var AuthRoutes = class {
|
|
|
2197
2254
|
res.writeHead(200, {
|
|
2198
2255
|
"cache-control": "no-store",
|
|
2199
2256
|
"content-type": "text/html; charset=utf-8",
|
|
2200
|
-
"content-security-policy": "default-src 'none'; script-src 'self'; style-src 'self'; connect-src 'self'; img-src 'self' data:; base-uri 'none'; frame-ancestors 'none'",
|
|
2257
|
+
"content-security-policy": "default-src 'none'; script-src 'self'; style-src 'self'; connect-src 'self'; img-src 'self' data:; manifest-src 'self'; base-uri 'none'; frame-ancestors 'none'",
|
|
2201
2258
|
"x-content-type-options": "nosniff",
|
|
2202
2259
|
"referrer-policy": "no-referrer"
|
|
2203
2260
|
});
|
|
2204
2261
|
res.end(
|
|
2205
|
-
'<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Sign in to DSH</title><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>'
|
|
2262
|
+
'<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Sign in to DSH</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>'
|
|
2206
2263
|
);
|
|
2207
2264
|
return;
|
|
2208
2265
|
}
|
|
@@ -2364,21 +2421,7 @@ var AuthRoutes = class {
|
|
|
2364
2421
|
sendJson(res, 405, { error: "method_not_allowed" });
|
|
2365
2422
|
}
|
|
2366
2423
|
} catch (error) {
|
|
2367
|
-
|
|
2368
|
-
res.destroy();
|
|
2369
|
-
return;
|
|
2370
|
-
}
|
|
2371
|
-
const failure2 = errorStatus(error);
|
|
2372
|
-
const terminateBody = failure2.status === 413 || failure2.status === 408 || failure2.status === 415;
|
|
2373
|
-
sendJson(res, failure2.status, failure2.body, {
|
|
2374
|
-
...failure2.retryAfter ? { "retry-after": failure2.retryAfter } : {},
|
|
2375
|
-
...terminateBody ? { connection: "close" } : {}
|
|
2376
|
-
});
|
|
2377
|
-
if (terminateBody)
|
|
2378
|
-
res.once("finish", () => {
|
|
2379
|
-
req.resume();
|
|
2380
|
-
req.socket.end();
|
|
2381
|
-
});
|
|
2424
|
+
sendHttpError(req, res, error);
|
|
2382
2425
|
}
|
|
2383
2426
|
}
|
|
2384
2427
|
};
|
|
@@ -2432,6 +2475,7 @@ var AuthGate = class {
|
|
|
2432
2475
|
if (!raw) return rejectHttp(res, 403, "invalid_origin");
|
|
2433
2476
|
if (!this.ready()) return rejectHttp(res, 503, "service_unavailable");
|
|
2434
2477
|
setRequestContext(req, raw);
|
|
2478
|
+
if (isPublicPwaAsset(req.method, raw.pathname)) return next();
|
|
2435
2479
|
const permission = routePermission(req.method, raw.pathname);
|
|
2436
2480
|
if (permission === "public" || permission === "logout") return next();
|
|
2437
2481
|
const token = currentSessionToken(req);
|
|
@@ -2503,6 +2547,287 @@ var AuthGate = class {
|
|
|
2503
2547
|
}
|
|
2504
2548
|
};
|
|
2505
2549
|
|
|
2550
|
+
// src/http/account-login.ts
|
|
2551
|
+
import { randomInt } from "node:crypto";
|
|
2552
|
+
|
|
2553
|
+
// src/adapters/dsh/account-callback.ts
|
|
2554
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
2555
|
+
import { Duplex } from "node:stream";
|
|
2556
|
+
import { finished } from "node:stream/promises";
|
|
2557
|
+
var AccountCallbackBridge = class {
|
|
2558
|
+
handler;
|
|
2559
|
+
capture(route) {
|
|
2560
|
+
if (route.kind !== "exact" || route.path !== ACCOUNT_CALLBACK_PATH) return;
|
|
2561
|
+
const handler = route.handler;
|
|
2562
|
+
this.handler = handler;
|
|
2563
|
+
return {
|
|
2564
|
+
handler: (_req, res) => {
|
|
2565
|
+
res.writeHead(403, { "cache-control": "no-store" }).end();
|
|
2566
|
+
},
|
|
2567
|
+
dispose: () => {
|
|
2568
|
+
if (this.handler === handler) this.handler = void 0;
|
|
2569
|
+
}
|
|
2570
|
+
};
|
|
2571
|
+
}
|
|
2572
|
+
/** Use Node's actual response lifecycle: DSH awaits its completion during cleanup. */
|
|
2573
|
+
async submit(query) {
|
|
2574
|
+
const handler = this.handler;
|
|
2575
|
+
if (!handler) return 410;
|
|
2576
|
+
const socket = new Duplex({
|
|
2577
|
+
read() {
|
|
2578
|
+
},
|
|
2579
|
+
write(_chunk, _encoding, done) {
|
|
2580
|
+
done();
|
|
2581
|
+
}
|
|
2582
|
+
});
|
|
2583
|
+
const request = new IncomingMessage(socket);
|
|
2584
|
+
request.method = "GET";
|
|
2585
|
+
request.url = `${ACCOUNT_CALLBACK_PATH}?${query}`;
|
|
2586
|
+
const response = new ServerResponse(request);
|
|
2587
|
+
response.assignSocket(socket);
|
|
2588
|
+
response.once("finish", () => socket.destroy());
|
|
2589
|
+
const completed = finished(response, { cleanup: true });
|
|
2590
|
+
void completed.catch(() => void 0);
|
|
2591
|
+
try {
|
|
2592
|
+
await handler(request, response);
|
|
2593
|
+
await completed;
|
|
2594
|
+
return response.statusCode;
|
|
2595
|
+
} finally {
|
|
2596
|
+
response.destroy();
|
|
2597
|
+
socket.destroy();
|
|
2598
|
+
}
|
|
2599
|
+
}
|
|
2600
|
+
};
|
|
2601
|
+
|
|
2602
|
+
// src/http/account-login.ts
|
|
2603
|
+
var ManualAccountLogin = class {
|
|
2604
|
+
constructor(ctx, auth, store, version) {
|
|
2605
|
+
this.auth = auth;
|
|
2606
|
+
this.version = version;
|
|
2607
|
+
this.unsubscribe = store.subscribe((_before, after) => this.reconcile(after));
|
|
2608
|
+
if (!supportsManualAccountLogin(version)) return;
|
|
2609
|
+
ctx.inject(["deepseekAccount"], (accountCtx) => {
|
|
2610
|
+
const provider = accountCtx.get("deepseekAccount");
|
|
2611
|
+
accountCtx.effect(() => {
|
|
2612
|
+
this.currentProvider = provider;
|
|
2613
|
+
return async () => {
|
|
2614
|
+
if (this.currentProvider === provider) this.currentProvider = void 0;
|
|
2615
|
+
if (this.active?.provider === provider) await this.cancelAttempt(this.active);
|
|
2616
|
+
};
|
|
2617
|
+
}, "auth-remote: account provider lifetime");
|
|
2618
|
+
});
|
|
2619
|
+
}
|
|
2620
|
+
auth;
|
|
2621
|
+
version;
|
|
2622
|
+
bridge = new AccountCallbackBridge();
|
|
2623
|
+
unsubscribe;
|
|
2624
|
+
active;
|
|
2625
|
+
starting = false;
|
|
2626
|
+
disposed = false;
|
|
2627
|
+
currentProvider;
|
|
2628
|
+
disposal;
|
|
2629
|
+
install(server) {
|
|
2630
|
+
if (supportsManualAccountLogin(this.version)) server.installAccountCallbackBridge(this.bridge);
|
|
2631
|
+
for (const action of ["state", "start", "submit", "cancel"])
|
|
2632
|
+
server.register({
|
|
2633
|
+
kind: "exact",
|
|
2634
|
+
path: `${ACCOUNT_LOGIN_BASE}/${action}`,
|
|
2635
|
+
handler: (req, res) => this.handle(req, res, action)
|
|
2636
|
+
});
|
|
2637
|
+
}
|
|
2638
|
+
provider() {
|
|
2639
|
+
const provider = this.currentProvider;
|
|
2640
|
+
return provider && typeof provider.getState === "function" && typeof provider.startSignIn === "function" && typeof provider.cancelSignIn === "function" ? provider : void 0;
|
|
2641
|
+
}
|
|
2642
|
+
session(token) {
|
|
2643
|
+
const session = token && !this.disposed ? this.auth.session(token) : null;
|
|
2644
|
+
if (!token || !session) throw new AuthError("unauthorized");
|
|
2645
|
+
return { hash: sessionHash(token), session };
|
|
2646
|
+
}
|
|
2647
|
+
owned(token, id) {
|
|
2648
|
+
const { hash } = this.session(token);
|
|
2649
|
+
const active = this.active;
|
|
2650
|
+
if (!active || active.owner !== hash || active.id !== id)
|
|
2651
|
+
throw new HttpFailure(409, "account_attempt_mismatch");
|
|
2652
|
+
if (active.cancelled || active.deadline <= Date.now())
|
|
2653
|
+
throw new HttpFailure(410, "account_attempt_expired");
|
|
2654
|
+
if (this.provider() !== active.provider) throw new HttpFailure(409, "account_attempt_mismatch");
|
|
2655
|
+
return active;
|
|
2656
|
+
}
|
|
2657
|
+
schedule(attempt) {
|
|
2658
|
+
if (attempt.timer) clearTimeout(attempt.timer);
|
|
2659
|
+
attempt.timer = setTimeout(
|
|
2660
|
+
() => void this.cancelAttempt(attempt).catch(() => {
|
|
2661
|
+
}),
|
|
2662
|
+
Math.max(1, attempt.deadline - Date.now())
|
|
2663
|
+
);
|
|
2664
|
+
attempt.timer.unref();
|
|
2665
|
+
}
|
|
2666
|
+
async state(token) {
|
|
2667
|
+
const { hash } = this.session(token);
|
|
2668
|
+
const provider = this.provider();
|
|
2669
|
+
if (!provider) return { supported: false, status: "signed-out", attempt: null };
|
|
2670
|
+
const view = await provider.getState();
|
|
2671
|
+
this.session(token);
|
|
2672
|
+
const active = this.active;
|
|
2673
|
+
if (!active || active.owner !== hash || active.provider !== provider || active.id !== view.attempt?.id)
|
|
2674
|
+
return { supported: true, status: view.status, attempt: null };
|
|
2675
|
+
const attempt = view.attempt;
|
|
2676
|
+
if (attempt?.expiresAt !== void 0 && attempt.expiresAt < active.deadline) {
|
|
2677
|
+
active.deadline = attempt.expiresAt;
|
|
2678
|
+
this.schedule(active);
|
|
2679
|
+
}
|
|
2680
|
+
if (attempt && !isActiveAccountLogin(attempt.phase) && active.timer) clearTimeout(active.timer);
|
|
2681
|
+
return {
|
|
2682
|
+
supported: true,
|
|
2683
|
+
status: view.status,
|
|
2684
|
+
attempt: attempt && { ...attempt, expiresAt: active.deadline }
|
|
2685
|
+
};
|
|
2686
|
+
}
|
|
2687
|
+
async start(token, body) {
|
|
2688
|
+
const owner = this.session(token);
|
|
2689
|
+
const locale = stringField(body, "locale");
|
|
2690
|
+
const offset = body.timezoneOffsetSeconds;
|
|
2691
|
+
if (locale.length > 64 || !/^[a-z][a-z0-9_-]*$/iu.test(locale) || !Number.isInteger(offset) || Math.abs(offset) > 86400)
|
|
2692
|
+
throw new HttpFailure(400, "invalid_input");
|
|
2693
|
+
if (this.starting) throw new HttpFailure(409, "account_busy");
|
|
2694
|
+
const provider = this.provider();
|
|
2695
|
+
if (!provider) throw new HttpFailure(503, "account_unavailable");
|
|
2696
|
+
this.starting = true;
|
|
2697
|
+
let entry;
|
|
2698
|
+
try {
|
|
2699
|
+
const previous = await provider.getState();
|
|
2700
|
+
if (previous.attempt && isActiveAccountLogin(previous.attempt.phase))
|
|
2701
|
+
throw new HttpFailure(409, "account_busy");
|
|
2702
|
+
if (previous.status === "credential-stored")
|
|
2703
|
+
return { ...previous, supported: true, attempt: null };
|
|
2704
|
+
if (this.active?.timer) clearTimeout(this.active.timer);
|
|
2705
|
+
entry = {
|
|
2706
|
+
owner: owner.hash,
|
|
2707
|
+
provider,
|
|
2708
|
+
callbackOrigin: `http://127.0.0.1:${randomInt(49152, 65536)}`,
|
|
2709
|
+
deadline: Math.min(Date.now() + 6e5, owner.session.expiresAt),
|
|
2710
|
+
cancelled: false,
|
|
2711
|
+
submitting: false
|
|
2712
|
+
};
|
|
2713
|
+
this.active = entry;
|
|
2714
|
+
this.schedule(entry);
|
|
2715
|
+
const view = await provider.startSignIn(
|
|
2716
|
+
{ version: this.version, locale, timezoneOffsetSeconds: offset },
|
|
2717
|
+
entry.callbackOrigin,
|
|
2718
|
+
"web"
|
|
2719
|
+
);
|
|
2720
|
+
if (!view.attempt?.id) throw new HttpFailure(503, "account_unavailable");
|
|
2721
|
+
entry.id = view.attempt.id;
|
|
2722
|
+
if (entry.cancelled || !token || !this.auth.session(token) || this.disposed) {
|
|
2723
|
+
await this.cancelAttempt(entry);
|
|
2724
|
+
throw new AuthError("unauthorized");
|
|
2725
|
+
}
|
|
2726
|
+
return await this.state(token);
|
|
2727
|
+
} catch (error) {
|
|
2728
|
+
if (entry) await this.cancelAttempt(entry);
|
|
2729
|
+
throw error;
|
|
2730
|
+
} finally {
|
|
2731
|
+
this.starting = false;
|
|
2732
|
+
}
|
|
2733
|
+
}
|
|
2734
|
+
callback(value, active) {
|
|
2735
|
+
if (value.length > 8192 || /[\u0000-\u0020\u007f]/u.test(value.trim()))
|
|
2736
|
+
throw new HttpFailure(400, "account_invalid_callback");
|
|
2737
|
+
let url;
|
|
2738
|
+
try {
|
|
2739
|
+
url = new URL(value.trim());
|
|
2740
|
+
} catch {
|
|
2741
|
+
throw new HttpFailure(400, "account_invalid_callback");
|
|
2742
|
+
}
|
|
2743
|
+
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"))
|
|
2744
|
+
throw new HttpFailure(400, "account_invalid_callback");
|
|
2745
|
+
return url.searchParams;
|
|
2746
|
+
}
|
|
2747
|
+
async submit(token, body) {
|
|
2748
|
+
const active = this.owned(token, stringField(body, "id"));
|
|
2749
|
+
const query = this.callback(stringField(body, "callbackUrl"), active);
|
|
2750
|
+
if (active.submitting) throw new HttpFailure(409, "account_busy");
|
|
2751
|
+
active.submitting = true;
|
|
2752
|
+
try {
|
|
2753
|
+
const before = await active.provider.getState();
|
|
2754
|
+
if (!before.attempt || before.attempt.id !== active.id || before.attempt.phase !== "waiting-browser")
|
|
2755
|
+
throw new HttpFailure(409, "account_attempt_mismatch");
|
|
2756
|
+
this.owned(token, active.id);
|
|
2757
|
+
const status = await this.bridge.submit(query);
|
|
2758
|
+
if (status === 400) throw new HttpFailure(400, "account_invalid_callback");
|
|
2759
|
+
const state = await this.state(token);
|
|
2760
|
+
if (state.status !== "credential-stored" || state.attempt?.phase !== "succeeded")
|
|
2761
|
+
throw new HttpFailure(
|
|
2762
|
+
status === 410 ? 410 : 409,
|
|
2763
|
+
state.attempt?.phase === "expired" ? "account_attempt_expired" : "account_exchange_failed"
|
|
2764
|
+
);
|
|
2765
|
+
return state;
|
|
2766
|
+
} finally {
|
|
2767
|
+
active.submitting = false;
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
async cancelAttempt(attempt) {
|
|
2771
|
+
attempt.cancelled = true;
|
|
2772
|
+
if (attempt.timer) clearTimeout(attempt.timer);
|
|
2773
|
+
if (attempt.id) {
|
|
2774
|
+
attempt.cancellation ??= Promise.resolve().then(async () => {
|
|
2775
|
+
await attempt.provider.cancelSignIn(attempt.id);
|
|
2776
|
+
});
|
|
2777
|
+
await attempt.cancellation;
|
|
2778
|
+
}
|
|
2779
|
+
}
|
|
2780
|
+
async reconcile(state) {
|
|
2781
|
+
const active = this.active;
|
|
2782
|
+
if (!active || active.cancelled) return;
|
|
2783
|
+
const session = state.sessions[active.owner];
|
|
2784
|
+
const account = state.account;
|
|
2785
|
+
if (!session || !account || session.accountId !== account.id || session.securityVersion !== account.securityVersion || session.expiresAt <= Date.now() || this.auth.policySnapshot().requireTotp && !account.totp) {
|
|
2786
|
+
await this.cancelAttempt(active);
|
|
2787
|
+
} else {
|
|
2788
|
+
active.deadline = Math.min(active.deadline, session.expiresAt);
|
|
2789
|
+
this.schedule(active);
|
|
2790
|
+
}
|
|
2791
|
+
}
|
|
2792
|
+
async handle(req, res, action) {
|
|
2793
|
+
try {
|
|
2794
|
+
const token = currentSessionToken(req);
|
|
2795
|
+
this.session(token);
|
|
2796
|
+
const expected = action === "state" ? "GET" : "POST";
|
|
2797
|
+
if (req.method !== expected) throw new HttpFailure(405, "method_not_allowed");
|
|
2798
|
+
if (action === "state") {
|
|
2799
|
+
sendJson(res, 200, await this.state(token));
|
|
2800
|
+
return;
|
|
2801
|
+
}
|
|
2802
|
+
const body = await readJsonBody(req);
|
|
2803
|
+
switch (action) {
|
|
2804
|
+
case "start":
|
|
2805
|
+
sendJson(res, 200, await this.start(token, body));
|
|
2806
|
+
break;
|
|
2807
|
+
case "submit":
|
|
2808
|
+
sendJson(res, 200, await this.submit(token, body));
|
|
2809
|
+
break;
|
|
2810
|
+
case "cancel": {
|
|
2811
|
+
const active = this.owned(token, stringField(body, "id"));
|
|
2812
|
+
await this.cancelAttempt(active);
|
|
2813
|
+
sendJson(res, 200, await this.state(token));
|
|
2814
|
+
break;
|
|
2815
|
+
}
|
|
2816
|
+
}
|
|
2817
|
+
} catch (error) {
|
|
2818
|
+
sendHttpError(req, res, error);
|
|
2819
|
+
}
|
|
2820
|
+
}
|
|
2821
|
+
dispose() {
|
|
2822
|
+
return this.disposal ??= this.disposeAttempt();
|
|
2823
|
+
}
|
|
2824
|
+
async disposeAttempt() {
|
|
2825
|
+
this.disposed = true;
|
|
2826
|
+
this.unsubscribe();
|
|
2827
|
+
if (this.active) await this.cancelAttempt(this.active);
|
|
2828
|
+
}
|
|
2829
|
+
};
|
|
2830
|
+
|
|
2506
2831
|
// src/index.ts
|
|
2507
2832
|
var name = "auth-remote";
|
|
2508
2833
|
var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
@@ -2526,7 +2851,7 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2526
2851
|
super(ctx, config, gate);
|
|
2527
2852
|
this.pending = config;
|
|
2528
2853
|
this.switchGate = gate;
|
|
2529
|
-
installAuthenticatedHostCapability(ctx);
|
|
2854
|
+
installAuthenticatedHostCapability(ctx, config.capabilities.version);
|
|
2530
2855
|
if (config.legacyWarning)
|
|
2531
2856
|
console.warn("auth-remote: allowedOrigins is deprecated; use publicUrl with one root URL");
|
|
2532
2857
|
}
|
|
@@ -2537,10 +2862,13 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2537
2862
|
let readiness;
|
|
2538
2863
|
let connections;
|
|
2539
2864
|
let admin;
|
|
2865
|
+
let accountLogin;
|
|
2540
2866
|
const store = await AuthStateStore.open(profile.dir, {
|
|
2541
2867
|
onUnavailable: () => {
|
|
2542
2868
|
this.switchGate.set(new DenyGate());
|
|
2543
2869
|
readiness?.detach();
|
|
2870
|
+
void accountLogin?.dispose().catch(() => {
|
|
2871
|
+
});
|
|
2544
2872
|
if (connections) void connections.closeAll();
|
|
2545
2873
|
}
|
|
2546
2874
|
});
|
|
@@ -2553,9 +2881,11 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2553
2881
|
this.stateStore = store;
|
|
2554
2882
|
this.authService = auth;
|
|
2555
2883
|
await super[Service.init]();
|
|
2884
|
+
const port = this.port;
|
|
2885
|
+
if (port === void 0) throw new Error("auth-remote: listener has not bound a port");
|
|
2556
2886
|
this.resolvedConfig = finalizeAuthConfig(this.pending, {
|
|
2557
2887
|
host: this.host,
|
|
2558
|
-
port
|
|
2888
|
+
port,
|
|
2559
2889
|
protocol: listenerProtocol(this, this.pending.capabilities)
|
|
2560
2890
|
});
|
|
2561
2891
|
this.setOriginPolicy(this.resolved.originPolicy);
|
|
@@ -2565,6 +2895,13 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2565
2895
|
});
|
|
2566
2896
|
this.switchGate.set(new AuthGate(this.resolved, store, auth, readiness, connections));
|
|
2567
2897
|
new AuthRoutes(auth, profileName).install(this);
|
|
2898
|
+
accountLogin = new ManualAccountLogin(
|
|
2899
|
+
this.ctx,
|
|
2900
|
+
auth,
|
|
2901
|
+
store,
|
|
2902
|
+
this.pending.capabilities.version
|
|
2903
|
+
);
|
|
2904
|
+
accountLogin.install(this);
|
|
2568
2905
|
admin = new AdminSocket(
|
|
2569
2906
|
profile.dir,
|
|
2570
2907
|
auth,
|
|
@@ -2579,10 +2916,12 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2579
2916
|
const activeConnections = connections;
|
|
2580
2917
|
const activeReadiness = readiness;
|
|
2581
2918
|
const activeAdmin = admin;
|
|
2919
|
+
const activeAccountLogin = accountLogin;
|
|
2582
2920
|
this.ctx.effect(
|
|
2583
2921
|
() => async () => {
|
|
2584
2922
|
this.switchGate.set(new DenyGate());
|
|
2585
2923
|
activeReadiness.detach();
|
|
2924
|
+
await activeAccountLogin.dispose();
|
|
2586
2925
|
await activeAdmin.dispose();
|
|
2587
2926
|
await activeConnections.dispose();
|
|
2588
2927
|
auth.dispose();
|
|
@@ -2613,6 +2952,7 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2613
2952
|
} catch (error) {
|
|
2614
2953
|
this.switchGate.set(new DenyGate());
|
|
2615
2954
|
await admin?.dispose();
|
|
2955
|
+
await accountLogin?.dispose();
|
|
2616
2956
|
await connections?.dispose();
|
|
2617
2957
|
this.authService?.dispose();
|
|
2618
2958
|
await store.close();
|