Skip to content

Commit 3c00dbc

Browse files
edenfunfobviyus
andauthored
fix(line): retire the credential a rotation replaces (#132150)
A successful LINE credential rotation must remove the superseded source. The setup adapter now clears the paired inline or file field per credential, so file rotations take effect without changing the other credential. Co-authored-by: xuyuanhao <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent f191738 commit 3c00dbc

2 files changed

Lines changed: 95 additions & 23 deletions

File tree

‎extensions/line/src/setup-core.test.ts‎

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@ import type { OpenClawConfig } from "openclaw/plugin-sdk/setup";
44
import { describe, expect, it } from "vitest";
55
import { lineSetupAdapter, patchLineAccountConfig } from "./setup-core.js";
66

7-
type LineChannelConfig = { channelAccessToken?: string };
7+
type LineChannelConfig = {
8+
channelAccessToken?: string;
9+
channelSecret?: string;
10+
tokenFile?: string;
11+
secretFile?: string;
12+
};
813

9-
function appliedLineConfig(input: Record<string, unknown>): LineChannelConfig {
10-
const cfg = lineSetupAdapter.applyAccountConfig({
11-
cfg: {} as OpenClawConfig,
12-
accountId: "default",
13-
input,
14-
});
15-
return (cfg.channels?.line ?? {}) as LineChannelConfig;
14+
function applyLineSetup(
15+
input: Record<string, unknown>,
16+
cfg: OpenClawConfig = {} as OpenClawConfig,
17+
): OpenClawConfig {
18+
return lineSetupAdapter.applyAccountConfig({ cfg, accountId: "default", input });
19+
}
20+
21+
function appliedLineConfig(
22+
input: Record<string, unknown>,
23+
cfg?: OpenClawConfig,
24+
): LineChannelConfig {
25+
return (applyLineSetup(input, cfg).channels?.line ?? {}) as LineChannelConfig;
1626
}
1727

1828
describe("line setup token alias", () => {
@@ -85,3 +95,47 @@ describe("LINE scoped setup config", () => {
8595
});
8696
});
8797
});
98+
99+
describe("LINE credential rotation", () => {
100+
// The inline value wins over its file at resolution time, so a rotation that
101+
// leaves it behind silently keeps using the credential it was meant to replace.
102+
const inlineFirst = () =>
103+
applyLineSetup({ channelAccessToken: "inline-token", channelSecret: "inline-secret" });
104+
105+
it("retires an inline credential when its file replaces it", () => {
106+
const rotated = appliedLineConfig(
107+
{ tokenFile: "/run/secrets/line-token", secretFile: "/run/secrets/line-secret" },
108+
inlineFirst(),
109+
);
110+
111+
expect(rotated.tokenFile).toBe("/run/secrets/line-token");
112+
expect(rotated.secretFile).toBe("/run/secrets/line-secret");
113+
expect(rotated.channelAccessToken).toBeUndefined();
114+
expect(rotated.channelSecret).toBeUndefined();
115+
});
116+
117+
it("retires a credential file when an inline value replaces it", () => {
118+
const fromFiles = applyLineSetup({
119+
tokenFile: "/run/secrets/line-token",
120+
secretFile: "/run/secrets/line-secret",
121+
});
122+
123+
const rotated = appliedLineConfig(
124+
{ channelAccessToken: "inline-token", channelSecret: "inline-secret" },
125+
fromFiles,
126+
);
127+
128+
expect(rotated.channelAccessToken).toBe("inline-token");
129+
expect(rotated.channelSecret).toBe("inline-secret");
130+
expect(rotated.tokenFile).toBeUndefined();
131+
expect(rotated.secretFile).toBeUndefined();
132+
});
133+
134+
it("leaves the credential that was not replaced alone", () => {
135+
const rotated = appliedLineConfig({ tokenFile: "/run/secrets/line-token" }, inlineFirst());
136+
137+
expect(rotated.tokenFile).toBe("/run/secrets/line-token");
138+
expect(rotated.channelSecret).toBe("inline-secret");
139+
expect(rotated.channelAccessToken).toBeUndefined();
140+
});
141+
});

‎extensions/line/src/setup-core.ts‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,45 @@ export const lineSetupAdapter: ChannelSetupAdapter = {
7979
const accessToken = typedInput.channelAccessToken ?? typedInput.token;
8080
const normalizedAccountId = normalizeAccountId(accountId);
8181
const useEnv = normalizedAccountId === DEFAULT_ACCOUNT_ID && Boolean(typedInput.useEnv);
82+
// A credential resolves from the inline value first and only then from its
83+
// file, so writing one form has to retire the other. Leaving both behind
84+
// makes a rotation onto a file a silent no-op: the stale inline value keeps
85+
// winning and setup still reports success.
86+
const credentials = [
87+
{
88+
fileKey: "tokenFile",
89+
file: typedInput.tokenFile,
90+
inlineKey: "channelAccessToken",
91+
inline: accessToken,
92+
},
93+
{
94+
fileKey: "secretFile",
95+
file: typedInput.secretFile,
96+
inlineKey: "channelSecret",
97+
inline: typedInput.channelSecret,
98+
},
99+
] as const;
100+
const patch: Record<string, string> = {};
101+
const retired: string[] = [];
102+
for (const credential of credentials) {
103+
if (credential.file) {
104+
patch[credential.fileKey] = credential.file;
105+
retired.push(credential.inlineKey);
106+
} else if (credential.inline) {
107+
patch[credential.inlineKey] = credential.inline;
108+
retired.push(credential.fileKey);
109+
}
110+
}
82111
return patchLineAccountConfig({
83112
cfg,
84113
accountId: normalizedAccountId,
85114
enabled: true,
86115
clearFields: useEnv
87116
? ["channelAccessToken", "channelSecret", "tokenFile", "secretFile"]
88-
: undefined,
89-
patch: useEnv
90-
? {}
91-
: {
92-
...(typedInput.tokenFile
93-
? { tokenFile: typedInput.tokenFile }
94-
: accessToken
95-
? { channelAccessToken: accessToken }
96-
: {}),
97-
...(typedInput.secretFile
98-
? { secretFile: typedInput.secretFile }
99-
: typedInput.channelSecret
100-
? { channelSecret: typedInput.channelSecret }
101-
: {}),
102-
},
117+
: retired.length > 0
118+
? retired
119+
: undefined,
120+
patch: useEnv ? {} : patch,
103121
});
104122
},
105123
};

0 commit comments

Comments
 (0)