refactor: clarify pairing setup auth labels

This commit is contained in:
Peter Steinberger
2026-03-12 22:42:04 +00:00
parent 01e4845f6d
commit 212afb6950
2 changed files with 38 additions and 32 deletions

View File

@@ -41,10 +41,8 @@ type ResolveUrlResult = {
error?: string;
};
type ResolveAuthResult = {
token?: string;
password?: string;
label?: string;
type ResolveAuthLabelResult = {
label?: "token" | "password";
error?: string;
};
@@ -187,7 +185,7 @@ async function resolveTailnetHost(): Promise<string | null> {
);
}
function resolveAuth(cfg: OpenClawPluginApi["config"]): ResolveAuthResult {
function resolveAuthLabel(cfg: OpenClawPluginApi["config"]): ResolveAuthLabelResult {
const mode = cfg.gateway?.auth?.mode;
const token =
pickFirstDefined([
@@ -203,13 +201,13 @@ function resolveAuth(cfg: OpenClawPluginApi["config"]): ResolveAuthResult {
]) ?? undefined;
if (mode === "token" || mode === "password") {
return resolveRequiredAuth(mode, { token, password });
return resolveRequiredAuthLabel(mode, { token, password });
}
if (token) {
return { token, label: "token" };
return { label: "token" };
}
if (password) {
return { password, label: "password" };
return { label: "password" };
}
return { error: "Gateway auth is not configured (no token or password)." };
}
@@ -227,17 +225,17 @@ function pickFirstDefined(candidates: Array<unknown>): string | null {
return null;
}
function resolveRequiredAuth(
function resolveRequiredAuthLabel(
mode: "token" | "password",
values: { token?: string; password?: string },
): ResolveAuthResult {
): ResolveAuthLabelResult {
if (mode === "token") {
return values.token
? { token: values.token, label: "token" }
? { label: "token" }
: { error: "Gateway auth is set to token, but no token is configured." };
}
return values.password
? { password: values.password, label: "password" }
? { label: "password" }
: { error: "Gateway auth is set to password, but no password is configured." };
}
@@ -393,9 +391,9 @@ export default function register(api: OpenClawPluginApi) {
return { text: `✅ Paired ${label}${platformLabel}.` };
}
const auth = resolveAuth(api.config);
if (auth.error) {
return { text: `Error: ${auth.error}` };
const authLabelResult = resolveAuthLabel(api.config);
if (authLabelResult.error) {
return { text: `Error: ${authLabelResult.error}` };
}
const urlResult = await resolveGatewayUrl(api);
@@ -411,7 +409,7 @@ export default function register(api: OpenClawPluginApi) {
if (action === "qr") {
const setupCode = encodeSetupCode(payload);
const qrAscii = await renderQrAscii(setupCode);
const authLabel = auth.label ?? "auth";
const authLabel = authLabelResult.label ?? "auth";
const channel = ctx.channel;
const target = ctx.senderId?.trim() || ctx.from?.trim() || ctx.to?.trim() || "";
@@ -502,7 +500,7 @@ export default function register(api: OpenClawPluginApi) {
const channel = ctx.channel;
const target = ctx.senderId?.trim() || ctx.from?.trim() || ctx.to?.trim() || "";
const authLabel = auth.label ?? "auth";
const authLabel = authLabelResult.label ?? "auth";
if (channel === "telegram" && target) {
try {