fix: clear gateway auth on off selection
This commit is contained in:
55
src/commands/configure.gateway-auth.test.ts
Normal file
55
src/commands/configure.gateway-auth.test.ts
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { buildGatewayAuthConfig } from "./configure.js";
|
||||||
|
|
||||||
|
describe("buildGatewayAuthConfig", () => {
|
||||||
|
it("clears token/password when auth is off", () => {
|
||||||
|
const result = buildGatewayAuthConfig({
|
||||||
|
existing: { mode: "token", token: "abc", password: "secret" },
|
||||||
|
mode: "off",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves allowTailscale when auth is off", () => {
|
||||||
|
const result = buildGatewayAuthConfig({
|
||||||
|
existing: {
|
||||||
|
mode: "token",
|
||||||
|
token: "abc",
|
||||||
|
allowTailscale: true,
|
||||||
|
},
|
||||||
|
mode: "off",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toEqual({ allowTailscale: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("drops password when switching to token", () => {
|
||||||
|
const result = buildGatewayAuthConfig({
|
||||||
|
existing: {
|
||||||
|
mode: "password",
|
||||||
|
password: "secret",
|
||||||
|
allowTailscale: false,
|
||||||
|
},
|
||||||
|
mode: "token",
|
||||||
|
token: "abc",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
mode: "token",
|
||||||
|
token: "abc",
|
||||||
|
allowTailscale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("drops token when switching to password", () => {
|
||||||
|
const result = buildGatewayAuthConfig({
|
||||||
|
existing: { mode: "token", token: "abc" },
|
||||||
|
mode: "password",
|
||||||
|
password: "secret",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toEqual({ mode: "password", password: "secret" });
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -8,7 +8,7 @@ import {
|
|||||||
text as clackText,
|
text as clackText,
|
||||||
} from "@clack/prompts";
|
} from "@clack/prompts";
|
||||||
import { ensureAuthProfileStore } from "../agents/auth-profiles.js";
|
import { ensureAuthProfileStore } from "../agents/auth-profiles.js";
|
||||||
import type { ClawdbotConfig } from "../config/config.js";
|
import type { ClawdbotConfig, GatewayAuthConfig } from "../config/config.js";
|
||||||
import {
|
import {
|
||||||
CONFIG_PATH_CLAWDBOT,
|
CONFIG_PATH_CLAWDBOT,
|
||||||
readConfigFileSnapshot,
|
readConfigFileSnapshot,
|
||||||
@@ -157,6 +157,27 @@ const CONFIGURE_SECTION_OPTIONS: {
|
|||||||
|
|
||||||
type ConfigureSectionChoice = WizardSection | "__continue";
|
type ConfigureSectionChoice = WizardSection | "__continue";
|
||||||
|
|
||||||
|
type GatewayAuthChoice = "off" | "token" | "password";
|
||||||
|
|
||||||
|
export function buildGatewayAuthConfig(params: {
|
||||||
|
existing?: GatewayAuthConfig;
|
||||||
|
mode: GatewayAuthChoice;
|
||||||
|
token?: string;
|
||||||
|
password?: string;
|
||||||
|
}): GatewayAuthConfig | undefined {
|
||||||
|
const allowTailscale = params.existing?.allowTailscale;
|
||||||
|
const base: GatewayAuthConfig = {};
|
||||||
|
if (typeof allowTailscale === "boolean") base.allowTailscale = allowTailscale;
|
||||||
|
|
||||||
|
if (params.mode === "off") {
|
||||||
|
return Object.keys(base).length > 0 ? base : undefined;
|
||||||
|
}
|
||||||
|
if (params.mode === "token") {
|
||||||
|
return { ...base, mode: "token", token: params.token };
|
||||||
|
}
|
||||||
|
return { ...base, mode: "password", password: params.password };
|
||||||
|
}
|
||||||
|
|
||||||
async function promptConfigureSection(
|
async function promptConfigureSection(
|
||||||
runtime: RuntimeEnv,
|
runtime: RuntimeEnv,
|
||||||
hasSelection: boolean,
|
hasSelection: boolean,
|
||||||
@@ -225,7 +246,7 @@ async function promptGatewayConfig(
|
|||||||
initialValue: "token",
|
initialValue: "token",
|
||||||
}),
|
}),
|
||||||
runtime,
|
runtime,
|
||||||
) as "off" | "token" | "password";
|
) as GatewayAuthChoice;
|
||||||
|
|
||||||
const tailscaleMode = guardCancel(
|
const tailscaleMode = guardCancel(
|
||||||
await select({
|
await select({
|
||||||
@@ -287,6 +308,7 @@ async function promptGatewayConfig(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let gatewayToken: string | undefined;
|
let gatewayToken: string | undefined;
|
||||||
|
let gatewayPassword: string | undefined;
|
||||||
let next = cfg;
|
let next = cfg;
|
||||||
|
|
||||||
if (authMode === "token") {
|
if (authMode === "token") {
|
||||||
@@ -298,13 +320,6 @@ async function promptGatewayConfig(
|
|||||||
runtime,
|
runtime,
|
||||||
);
|
);
|
||||||
gatewayToken = String(tokenInput).trim() || randomToken();
|
gatewayToken = String(tokenInput).trim() || randomToken();
|
||||||
next = {
|
|
||||||
...next,
|
|
||||||
gateway: {
|
|
||||||
...next.gateway,
|
|
||||||
auth: { ...next.gateway?.auth, mode: "token", token: gatewayToken },
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (authMode === "password") {
|
if (authMode === "password") {
|
||||||
@@ -315,19 +330,16 @@ async function promptGatewayConfig(
|
|||||||
}),
|
}),
|
||||||
runtime,
|
runtime,
|
||||||
);
|
);
|
||||||
next = {
|
gatewayPassword = String(password).trim();
|
||||||
...next,
|
|
||||||
gateway: {
|
|
||||||
...next.gateway,
|
|
||||||
auth: {
|
|
||||||
...next.gateway?.auth,
|
|
||||||
mode: "password",
|
|
||||||
password: String(password).trim(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const authConfig = buildGatewayAuthConfig({
|
||||||
|
existing: next.gateway?.auth,
|
||||||
|
mode: authMode,
|
||||||
|
token: gatewayToken,
|
||||||
|
password: gatewayPassword,
|
||||||
|
});
|
||||||
|
|
||||||
next = {
|
next = {
|
||||||
...next,
|
...next,
|
||||||
gateway: {
|
gateway: {
|
||||||
@@ -335,6 +347,7 @@ async function promptGatewayConfig(
|
|||||||
mode: "local",
|
mode: "local",
|
||||||
port,
|
port,
|
||||||
bind,
|
bind,
|
||||||
|
auth: authConfig,
|
||||||
tailscale: {
|
tailscale: {
|
||||||
...next.gateway?.tailscale,
|
...next.gateway?.tailscale,
|
||||||
mode: tailscaleMode,
|
mode: tailscaleMode,
|
||||||
|
|||||||
Reference in New Issue
Block a user