feat: add reusable loopback dev auth
This commit is contained in:
1 parent
6f34ad3e87
commit
4bc7f56330
21 files changed
+801
-69
No files matched your search
@@ -13,7 +13,9 @@ import { ChildProcessSpawner } from "effect/unstable/process";
|
||||
|
||||
import {
|
||||
checkPortAvailabilityOnHosts,
|
||||
createDevAccessUrls,
|
||||
createDevRunnerEnv,
|
||||
deriveDevAuthKey,
|
||||
findFirstAvailableOffset,
|
||||
getDevRunnerModeArgs,
|
||||
resolveModePortOffsets,
|
||||
@@ -125,6 +127,42 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
|
||||
);
|
||||
});
|
||||
|
||||
describe("development authentication", () => {
|
||||
it.effect("derives a stable opaque key from the canonical worktree root", () =>
|
||||
Effect.sync(() => {
|
||||
const first = deriveDevAuthKey("/tmp/t3-worktree-a");
|
||||
const repeated = deriveDevAuthKey("/tmp/t3-worktree-a");
|
||||
const other = deriveDevAuthKey("/tmp/t3-worktree-b");
|
||||
|
||||
assert.match(first, /^[a-f0-9]{64}$/);
|
||||
assert.equal(first, repeated);
|
||||
assert.notEqual(first, other);
|
||||
assert.notInclude(first, "t3-worktree-a");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("prints reusable web and platform-specific mobile pairing urls", () =>
|
||||
Effect.sync(() => {
|
||||
const urls = createDevAccessUrls({
|
||||
mode: "dev",
|
||||
serverPort: 13_773,
|
||||
webUrl: "http://localhost:5733",
|
||||
credential: "development-key",
|
||||
});
|
||||
|
||||
assert.equal(urls.web, "http://localhost:5733/pair#token=development-key");
|
||||
assert.include(
|
||||
decodeURIComponent(urls.ios),
|
||||
"pairingUrl=http://127.0.0.1:13773/pair#token=development-key",
|
||||
);
|
||||
assert.include(
|
||||
decodeURIComponent(urls.android),
|
||||
"pairingUrl=http://10.0.2.2:13773/pair#token=development-key",
|
||||
);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("createDevRunnerEnv", () => {
|
||||
it.effect("leaves the shared home implicit and disables browser auto-open", () =>
|
||||
Effect.gen(function* () {
|
||||
@@ -144,6 +182,8 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
|
||||
|
||||
assert.equal(env.T3CODE_HOME, undefined);
|
||||
assert.equal(env.T3CODE_NO_BROWSER, "1");
|
||||
assert.equal(env.T3CODE_DEV_AUTH, "1");
|
||||
assert.match(env.T3CODE_DEV_AUTH_KEY ?? "", /^[a-f0-9]{64}$/);
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -213,6 +253,8 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
|
||||
assert.equal(env.T3CODE_LOG_WS_EVENTS, "1");
|
||||
assert.equal(env.T3CODE_HOST, "0.0.0.0");
|
||||
assert.equal(env.VITE_DEV_SERVER_URL, "http://localhost:7331/");
|
||||
assert.equal(env.T3CODE_DEV_AUTH, undefined);
|
||||
assert.equal(env.T3CODE_DEV_AUTH_KEY, undefined);
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -316,6 +358,8 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
|
||||
assert.equal(env.T3CODE_NO_BROWSER, undefined);
|
||||
assert.equal(env.T3CODE_HOST, undefined);
|
||||
assert.equal(env.VITE_WS_URL, "ws://127.0.0.1:4222");
|
||||
assert.equal(env.T3CODE_DEV_AUTH, undefined);
|
||||
assert.equal(env.T3CODE_DEV_AUTH_KEY, undefined);
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import * as NodeCrypto from "node:crypto";
|
||||
import * as NodeOS from "node:os";
|
||||
import * as NodeURL from "node:url";
|
||||
|
||||
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import * as NetService from "@t3tools/shared/Net";
|
||||
import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
|
||||
import { isLoopbackHost } from "@t3tools/shared/networkHost";
|
||||
import { resolveSpawnCommand } from "@t3tools/shared/shell";
|
||||
import * as Config from "effect/Config";
|
||||
import * as Effect from "effect/Effect";
|
||||
@@ -56,6 +59,48 @@ export function getDevRunnerModeArgs(mode: DevMode): ReadonlyArray<string> {
|
||||
return MODE_ARGS[mode];
|
||||
}
|
||||
|
||||
export function deriveDevAuthKey(worktreeRoot: string): string {
|
||||
// This is a stable public identity, not a secret. Dev auth deliberately assumes a
|
||||
// single-user development machine and is rejected unless the server binds to loopback.
|
||||
return NodeCrypto.createHash("sha256").update(`t3-dev:${worktreeRoot}`).digest("hex");
|
||||
}
|
||||
|
||||
export function createDevAccessUrls(input: {
|
||||
readonly mode: DevMode;
|
||||
readonly serverPort: number;
|
||||
readonly webUrl: string;
|
||||
readonly credential: string;
|
||||
}): {
|
||||
readonly web: string | undefined;
|
||||
readonly ios: string;
|
||||
readonly android: string;
|
||||
} {
|
||||
const pairingUrl = (host: string) => {
|
||||
const url = new URL(`http://${host}:${input.serverPort}/pair`);
|
||||
url.hash = new URLSearchParams([["token", input.credential]]).toString();
|
||||
return url.toString();
|
||||
};
|
||||
const mobileUrl = (pairing: string) => {
|
||||
const url = new URL("t3code-dev://connections/new");
|
||||
url.searchParams.set("pairingUrl", pairing);
|
||||
return url.toString();
|
||||
};
|
||||
const web =
|
||||
input.mode === "dev"
|
||||
? (() => {
|
||||
const url = new URL("/pair", input.webUrl);
|
||||
url.hash = new URLSearchParams([["token", input.credential]]).toString();
|
||||
return url.toString();
|
||||
})()
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
web,
|
||||
ios: mobileUrl(pairingUrl("127.0.0.1")),
|
||||
android: mobileUrl(pairingUrl("10.0.2.2")),
|
||||
};
|
||||
}
|
||||
|
||||
export class DevRunnerConfigurationError extends Schema.TaggedErrorClass<DevRunnerConfigurationError>()(
|
||||
"DevRunnerConfigurationError",
|
||||
{
|
||||
@@ -243,10 +288,12 @@ export function createDevRunnerEnv({
|
||||
devUrl,
|
||||
}: CreateDevRunnerEnvInput): Effect.Effect<NodeJS.ProcessEnv, never, Path.Path> {
|
||||
return Effect.gen(function* () {
|
||||
const path = yield* Path.Path;
|
||||
const serverPort = port ?? BASE_SERVER_PORT + serverOffset;
|
||||
const webPort = BASE_WEB_PORT + webOffset;
|
||||
const configuredBaseDir = t3Home?.trim() || baseEnv.T3CODE_HOME?.trim() || undefined;
|
||||
const resolvedBaseDir = yield* resolveBaseDir(configuredBaseDir);
|
||||
const worktreeRoot = path.resolve(NodeURL.fileURLToPath(new URL("..", import.meta.url)));
|
||||
const isDesktopMode = mode === "dev:desktop";
|
||||
|
||||
const output: NodeJS.ProcessEnv = {
|
||||
@@ -276,6 +323,15 @@ export function createDevRunnerEnv({
|
||||
delete output.T3CODE_HOST;
|
||||
}
|
||||
|
||||
const devAuthEnabled = (mode === "dev" || mode === "dev:server") && isLoopbackHost(host);
|
||||
if (devAuthEnabled) {
|
||||
output.T3CODE_DEV_AUTH = "1";
|
||||
output.T3CODE_DEV_AUTH_KEY = deriveDevAuthKey(worktreeRoot);
|
||||
} else {
|
||||
delete output.T3CODE_DEV_AUTH;
|
||||
delete output.T3CODE_DEV_AUTH_KEY;
|
||||
}
|
||||
|
||||
if (!isDesktopMode && host !== undefined) {
|
||||
output.T3CODE_HOST = host;
|
||||
}
|
||||
@@ -529,6 +585,27 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) {
|
||||
`[dev-runner] mode=${input.mode} source=${source}${selectionSuffix} serverPort=${String(env.T3CODE_PORT)} webPort=${String(env.PORT)} baseDir=${baseDir}`,
|
||||
);
|
||||
|
||||
if (env.T3CODE_DEV_AUTH === "1" && env.T3CODE_DEV_AUTH_KEY) {
|
||||
const urls = createDevAccessUrls({
|
||||
mode: input.mode,
|
||||
serverPort: Number(env.T3CODE_PORT),
|
||||
webUrl: env.VITE_DEV_SERVER_URL!,
|
||||
credential: env.T3CODE_DEV_AUTH_KEY,
|
||||
});
|
||||
if (urls.web) {
|
||||
yield* Effect.logInfo(`[dev-runner] reusable web pairing URL: ${urls.web}`);
|
||||
}
|
||||
yield* Effect.logInfo(`[dev-runner] reusable iOS pairing URL: ${urls.ios}`);
|
||||
yield* Effect.logInfo(`[dev-runner] reusable Android pairing URL: ${urls.android}`);
|
||||
} else if (
|
||||
(input.mode === "dev" || input.mode === "dev:server") &&
|
||||
!isLoopbackHost(input.host)
|
||||
) {
|
||||
yield* Effect.logInfo(
|
||||
`[dev-runner] reusable dev authentication disabled for non-loopback host ${input.host}; use a regular pairing credential`,
|
||||
);
|
||||
}
|
||||
|
||||
if (input.dryRun) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user