dsh-auth-remote 0.1.8 → 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 +8 -1
- package/CHANGELOG.zh-CN.md +8 -1
- package/CONTRIBUTING.md +1 -1
- package/CONTRIBUTING.zh-CN.md +1 -1
- package/README.md +28 -12
- package/README.zh-CN.md +28 -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 +356 -23
- 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/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 +19 -15
- package/docs/deployment.zh-CN.md +19 -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
|
|
|
@@ -567,15 +580,30 @@ var GuardedWebServer = class extends WebServer2 {
|
|
|
567
580
|
}
|
|
568
581
|
gate;
|
|
569
582
|
originPolicy;
|
|
583
|
+
accountCallbackBridge;
|
|
570
584
|
/** Ready and the authenticated gate consume the same listener-finalized policy. */
|
|
571
585
|
setOriginPolicy(policy) {
|
|
572
586
|
this.originPolicy = policy;
|
|
573
587
|
}
|
|
588
|
+
installAccountCallbackBridge(bridge) {
|
|
589
|
+
this.accountCallbackBridge = bridge;
|
|
590
|
+
}
|
|
574
591
|
register(route) {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
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
|
+
}
|
|
579
607
|
}
|
|
580
608
|
registerFallback(handler) {
|
|
581
609
|
return super.registerFallback(
|
|
@@ -2017,6 +2045,10 @@ function requireRequestContext(req) {
|
|
|
2017
2045
|
// src/http/routes.ts
|
|
2018
2046
|
import { readFileSync as readFileSync2 } from "node:fs";
|
|
2019
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",
|
|
2020
2052
|
[`GET ${AUTH_BASE}/state`]: "public",
|
|
2021
2053
|
[`GET ${AUTH_BASE}/login`]: "public",
|
|
2022
2054
|
[`GET ${AUTH_BASE}/login.js`]: "public",
|
|
@@ -2167,6 +2199,23 @@ function errorStatus(error) {
|
|
|
2167
2199
|
}
|
|
2168
2200
|
return { status: 503, body: { error: "service_unavailable" } };
|
|
2169
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
|
+
}
|
|
2170
2219
|
var AuthRoutes = class {
|
|
2171
2220
|
constructor(auth, profileName) {
|
|
2172
2221
|
this.auth = auth;
|
|
@@ -2175,7 +2224,9 @@ var AuthRoutes = class {
|
|
|
2175
2224
|
auth;
|
|
2176
2225
|
profileName;
|
|
2177
2226
|
install(server) {
|
|
2178
|
-
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
|
+
);
|
|
2179
2230
|
for (const path of paths)
|
|
2180
2231
|
server.register({
|
|
2181
2232
|
kind: "exact",
|
|
@@ -2370,21 +2421,7 @@ var AuthRoutes = class {
|
|
|
2370
2421
|
sendJson(res, 405, { error: "method_not_allowed" });
|
|
2371
2422
|
}
|
|
2372
2423
|
} 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
|
-
});
|
|
2424
|
+
sendHttpError(req, res, error);
|
|
2388
2425
|
}
|
|
2389
2426
|
}
|
|
2390
2427
|
};
|
|
@@ -2510,6 +2547,287 @@ var AuthGate = class {
|
|
|
2510
2547
|
}
|
|
2511
2548
|
};
|
|
2512
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
|
+
|
|
2513
2831
|
// src/index.ts
|
|
2514
2832
|
var name = "auth-remote";
|
|
2515
2833
|
var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
@@ -2533,7 +2851,7 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2533
2851
|
super(ctx, config, gate);
|
|
2534
2852
|
this.pending = config;
|
|
2535
2853
|
this.switchGate = gate;
|
|
2536
|
-
installAuthenticatedHostCapability(ctx);
|
|
2854
|
+
installAuthenticatedHostCapability(ctx, config.capabilities.version);
|
|
2537
2855
|
if (config.legacyWarning)
|
|
2538
2856
|
console.warn("auth-remote: allowedOrigins is deprecated; use publicUrl with one root URL");
|
|
2539
2857
|
}
|
|
@@ -2544,10 +2862,13 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2544
2862
|
let readiness;
|
|
2545
2863
|
let connections;
|
|
2546
2864
|
let admin;
|
|
2865
|
+
let accountLogin;
|
|
2547
2866
|
const store = await AuthStateStore.open(profile.dir, {
|
|
2548
2867
|
onUnavailable: () => {
|
|
2549
2868
|
this.switchGate.set(new DenyGate());
|
|
2550
2869
|
readiness?.detach();
|
|
2870
|
+
void accountLogin?.dispose().catch(() => {
|
|
2871
|
+
});
|
|
2551
2872
|
if (connections) void connections.closeAll();
|
|
2552
2873
|
}
|
|
2553
2874
|
});
|
|
@@ -2560,9 +2881,11 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2560
2881
|
this.stateStore = store;
|
|
2561
2882
|
this.authService = auth;
|
|
2562
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");
|
|
2563
2886
|
this.resolvedConfig = finalizeAuthConfig(this.pending, {
|
|
2564
2887
|
host: this.host,
|
|
2565
|
-
port
|
|
2888
|
+
port,
|
|
2566
2889
|
protocol: listenerProtocol(this, this.pending.capabilities)
|
|
2567
2890
|
});
|
|
2568
2891
|
this.setOriginPolicy(this.resolved.originPolicy);
|
|
@@ -2572,6 +2895,13 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2572
2895
|
});
|
|
2573
2896
|
this.switchGate.set(new AuthGate(this.resolved, store, auth, readiness, connections));
|
|
2574
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);
|
|
2575
2905
|
admin = new AdminSocket(
|
|
2576
2906
|
profile.dir,
|
|
2577
2907
|
auth,
|
|
@@ -2586,10 +2916,12 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2586
2916
|
const activeConnections = connections;
|
|
2587
2917
|
const activeReadiness = readiness;
|
|
2588
2918
|
const activeAdmin = admin;
|
|
2919
|
+
const activeAccountLogin = accountLogin;
|
|
2589
2920
|
this.ctx.effect(
|
|
2590
2921
|
() => async () => {
|
|
2591
2922
|
this.switchGate.set(new DenyGate());
|
|
2592
2923
|
activeReadiness.detach();
|
|
2924
|
+
await activeAccountLogin.dispose();
|
|
2593
2925
|
await activeAdmin.dispose();
|
|
2594
2926
|
await activeConnections.dispose();
|
|
2595
2927
|
auth.dispose();
|
|
@@ -2620,6 +2952,7 @@ var AuthRemoteWebServer = class extends GuardedWebServer {
|
|
|
2620
2952
|
} catch (error) {
|
|
2621
2953
|
this.switchGate.set(new DenyGate());
|
|
2622
2954
|
await admin?.dispose();
|
|
2955
|
+
await accountLogin?.dispose();
|
|
2623
2956
|
await connections?.dispose();
|
|
2624
2957
|
this.authService?.dispose();
|
|
2625
2958
|
await store.close();
|