@kodelyth/line 2026.5.39 → 2026.5.42
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/api.ts +11 -0
- package/channel-plugin-api.ts +1 -0
- package/contract-api.ts +5 -0
- package/dist/accounts-CD4A1FE7.js +105 -0
- package/dist/api.js +11 -0
- package/dist/basic-cards-BISytiSa.js +307 -0
- package/dist/card-command-dQBX3fVN.js +240 -0
- package/dist/channel-DV5h44-j.js +649 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-Cc-v3szZ.js +4 -0
- package/dist/contract-api.js +2 -0
- package/dist/index.js +45 -0
- package/dist/markdown-to-line-CC3BU6CC.js +810 -0
- package/dist/monitor-Ci8Hg8ay.js +1485 -0
- package/dist/monitor.runtime-t6-QvlDB.js +2 -0
- package/dist/outbound.runtime-D1CxEvcL.js +2 -0
- package/dist/probe-BPSs_A_8.js +30 -0
- package/dist/probe.runtime-7u2o9QN5.js +2 -0
- package/dist/reply-payload-transform-CDuBzoT4.js +855 -0
- package/dist/runtime-api.js +291 -0
- package/dist/schedule-cards-D-yZMHDE.js +359 -0
- package/dist/secret-contract-api.js +5 -0
- package/dist/setup-api.js +2 -0
- package/dist/setup-entry.js +11 -0
- package/dist/setup-surface-CHfQ6Z4i.js +282 -0
- package/index.ts +53 -0
- package/klaw.plugin.json +2 -329
- package/package.json +4 -4
- package/runtime-api.ts +179 -0
- package/secret-contract-api.ts +4 -0
- package/setup-api.ts +2 -0
- package/setup-entry.ts +9 -0
- package/src/account-helpers.ts +16 -0
- package/src/accounts.test.ts +288 -0
- package/src/accounts.ts +187 -0
- package/src/actions.ts +61 -0
- package/src/auto-reply-delivery.test.ts +253 -0
- package/src/auto-reply-delivery.ts +200 -0
- package/src/bindings.ts +65 -0
- package/src/bot-access.ts +30 -0
- package/src/bot-handlers.test.ts +1094 -0
- package/src/bot-handlers.ts +620 -0
- package/src/bot-message-context.test.ts +420 -0
- package/src/bot-message-context.ts +586 -0
- package/src/bot.ts +66 -0
- package/src/card-command.ts +347 -0
- package/src/channel-access-token.ts +14 -0
- package/src/channel-api.ts +17 -0
- package/src/channel-setup-status.contract.test.ts +70 -0
- package/src/channel-shared.ts +48 -0
- package/src/channel.logout.test.ts +145 -0
- package/src/channel.runtime.ts +3 -0
- package/src/channel.sendPayload.test.ts +659 -0
- package/src/channel.setup.ts +11 -0
- package/src/channel.status.test.ts +63 -0
- package/src/channel.ts +155 -0
- package/src/config-adapter.ts +29 -0
- package/src/config-schema.test.ts +53 -0
- package/src/config-schema.ts +81 -0
- package/src/download.test.ts +164 -0
- package/src/download.ts +34 -0
- package/src/flex-templates/basic-cards.ts +395 -0
- package/src/flex-templates/common.ts +20 -0
- package/src/flex-templates/media-control-cards.ts +555 -0
- package/src/flex-templates/message.ts +13 -0
- package/src/flex-templates/schedule-cards.ts +467 -0
- package/src/flex-templates/types.ts +22 -0
- package/src/flex-templates.ts +32 -0
- package/src/gateway.ts +129 -0
- package/src/group-keys.test.ts +123 -0
- package/src/group-keys.ts +65 -0
- package/src/group-policy.ts +22 -0
- package/src/markdown-to-line.test.ts +348 -0
- package/src/markdown-to-line.ts +416 -0
- package/src/message-cards.test.ts +204 -0
- package/src/monitor-durable.test.ts +57 -0
- package/src/monitor-durable.ts +37 -0
- package/src/monitor.lifecycle.test.ts +499 -0
- package/src/monitor.runtime.ts +1 -0
- package/src/monitor.ts +507 -0
- package/src/outbound-media.test.ts +194 -0
- package/src/outbound-media.ts +120 -0
- package/src/outbound.runtime.ts +12 -0
- package/src/outbound.ts +427 -0
- package/src/probe.contract.test.ts +9 -0
- package/src/probe.runtime.ts +1 -0
- package/src/probe.ts +34 -0
- package/src/quick-reply-fallback.ts +10 -0
- package/src/reply-chunks.test.ts +180 -0
- package/src/reply-chunks.ts +110 -0
- package/src/reply-payload-transform.test.ts +392 -0
- package/src/reply-payload-transform.ts +317 -0
- package/src/rich-menu.test.ts +315 -0
- package/src/rich-menu.ts +326 -0
- package/src/runtime.ts +32 -0
- package/src/send-receipt.ts +32 -0
- package/src/send.test.ts +453 -0
- package/src/send.ts +531 -0
- package/src/setup-core.ts +149 -0
- package/src/setup-runtime-api.ts +9 -0
- package/src/setup-surface.test.ts +481 -0
- package/src/setup-surface.ts +229 -0
- package/src/signature.test.ts +34 -0
- package/src/signature.ts +24 -0
- package/src/status.ts +37 -0
- package/src/template-messages.ts +333 -0
- package/src/types.ts +130 -0
- package/src/webhook-node.test.ts +598 -0
- package/src/webhook-node.ts +155 -0
- package/src/webhook-utils.ts +10 -0
- package/src/webhook.ts +135 -0
- package/tsconfig.json +16 -0
- package/api.js +0 -7
- package/channel-plugin-api.js +0 -7
- package/contract-api.js +0 -7
- package/index.js +0 -7
- package/runtime-api.js +0 -7
- package/secret-contract-api.js +0 -7
- package/setup-api.js +0 -7
- package/setup-entry.js +0 -7
package/runtime-api.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// Private runtime barrel for the bundled LINE extension.
|
|
2
|
+
// Keep this barrel thin and aligned with the local extension surface.
|
|
3
|
+
|
|
4
|
+
export type {
|
|
5
|
+
ChannelAccountSnapshot,
|
|
6
|
+
ChannelPlugin,
|
|
7
|
+
KlawConfig,
|
|
8
|
+
KlawPluginApi,
|
|
9
|
+
PluginRuntime,
|
|
10
|
+
} from "klaw/plugin-sdk/core";
|
|
11
|
+
export type { ChannelGatewayContext, ChannelStatusIssue } from "klaw/plugin-sdk/channel-contract";
|
|
12
|
+
export { clearAccountEntryFields } from "klaw/plugin-sdk/core";
|
|
13
|
+
export { buildChannelConfigSchema } from "klaw/plugin-sdk/channel-config-schema";
|
|
14
|
+
export type { ReplyPayload } from "klaw/plugin-sdk/reply-runtime";
|
|
15
|
+
export type { ChannelSetupDmPolicy, ChannelSetupWizard } from "klaw/plugin-sdk/setup";
|
|
16
|
+
export {
|
|
17
|
+
buildComputedAccountStatusSnapshot,
|
|
18
|
+
buildTokenChannelStatusSummary,
|
|
19
|
+
} from "klaw/plugin-sdk/status-helpers";
|
|
20
|
+
export {
|
|
21
|
+
DEFAULT_ACCOUNT_ID,
|
|
22
|
+
formatDocsLink,
|
|
23
|
+
setSetupChannelEnabled,
|
|
24
|
+
splitSetupEntries,
|
|
25
|
+
} from "klaw/plugin-sdk/setup";
|
|
26
|
+
export { setLineRuntime } from "./src/runtime.js";
|
|
27
|
+
export { firstDefined, normalizeAllowFrom } from "./src/bot-access.js";
|
|
28
|
+
export { downloadLineMedia } from "./src/download.js";
|
|
29
|
+
export { probeLineBot } from "./src/probe.js";
|
|
30
|
+
export { buildTemplateMessageFromPayload } from "./src/template-messages.js";
|
|
31
|
+
export {
|
|
32
|
+
createQuickReplyItems,
|
|
33
|
+
pushFlexMessage,
|
|
34
|
+
pushLocationMessage,
|
|
35
|
+
pushMessageLine,
|
|
36
|
+
pushMessagesLine,
|
|
37
|
+
pushTemplateMessage,
|
|
38
|
+
pushTextMessageWithQuickReplies,
|
|
39
|
+
sendMessageLine,
|
|
40
|
+
} from "./src/send.js";
|
|
41
|
+
export { monitorLineProvider } from "./src/monitor.js";
|
|
42
|
+
export { hasLineDirectives, parseLineDirectives } from "./src/reply-payload-transform.js";
|
|
43
|
+
export {
|
|
44
|
+
listLineAccountIds,
|
|
45
|
+
normalizeAccountId,
|
|
46
|
+
resolveDefaultLineAccountId,
|
|
47
|
+
resolveLineAccount,
|
|
48
|
+
} from "./src/accounts.js";
|
|
49
|
+
export { type NormalizedAllowFrom } from "./src/bot-access.js";
|
|
50
|
+
export { resolveLineChannelAccessToken } from "./src/channel-access-token.js";
|
|
51
|
+
export {
|
|
52
|
+
LineChannelConfigSchema,
|
|
53
|
+
LineConfigSchema,
|
|
54
|
+
type LineConfigSchemaType,
|
|
55
|
+
} from "./src/config-schema.js";
|
|
56
|
+
export {
|
|
57
|
+
resolveExactLineGroupConfigKey,
|
|
58
|
+
resolveLineGroupConfigEntry,
|
|
59
|
+
resolveLineGroupLookupIds,
|
|
60
|
+
resolveLineGroupsConfig,
|
|
61
|
+
} from "./src/group-keys.js";
|
|
62
|
+
export {
|
|
63
|
+
type CodeBlock,
|
|
64
|
+
convertCodeBlockToFlexBubble,
|
|
65
|
+
convertLinksToFlexBubble,
|
|
66
|
+
convertTableToFlexBubble,
|
|
67
|
+
extractCodeBlocks,
|
|
68
|
+
extractLinks,
|
|
69
|
+
extractMarkdownTables,
|
|
70
|
+
hasMarkdownToConvert,
|
|
71
|
+
type MarkdownLink,
|
|
72
|
+
type MarkdownTable,
|
|
73
|
+
type ProcessedLineMessage,
|
|
74
|
+
processLineMessage,
|
|
75
|
+
stripMarkdown,
|
|
76
|
+
} from "./src/markdown-to-line.js";
|
|
77
|
+
export {
|
|
78
|
+
createAudioMessage,
|
|
79
|
+
createFlexMessage,
|
|
80
|
+
createImageMessage,
|
|
81
|
+
createLocationMessage,
|
|
82
|
+
createTextMessageWithQuickReplies,
|
|
83
|
+
createVideoMessage,
|
|
84
|
+
getUserDisplayName,
|
|
85
|
+
getUserProfile,
|
|
86
|
+
pushImageMessage,
|
|
87
|
+
replyMessageLine,
|
|
88
|
+
showLoadingAnimation,
|
|
89
|
+
} from "./src/send.js";
|
|
90
|
+
export { validateLineSignature } from "./src/signature.js";
|
|
91
|
+
export {
|
|
92
|
+
type ButtonsTemplate,
|
|
93
|
+
type CarouselColumn,
|
|
94
|
+
type CarouselTemplate,
|
|
95
|
+
type ConfirmTemplate,
|
|
96
|
+
createButtonMenu,
|
|
97
|
+
createButtonTemplate,
|
|
98
|
+
createCarouselColumn,
|
|
99
|
+
createConfirmTemplate,
|
|
100
|
+
createImageCarousel,
|
|
101
|
+
createImageCarouselColumn,
|
|
102
|
+
createLinkMenu,
|
|
103
|
+
createProductCarousel,
|
|
104
|
+
createTemplateCarousel,
|
|
105
|
+
createYesNoConfirm,
|
|
106
|
+
type ImageCarouselColumn,
|
|
107
|
+
type ImageCarouselTemplate,
|
|
108
|
+
type TemplateMessage,
|
|
109
|
+
} from "./src/template-messages.js";
|
|
110
|
+
export type {
|
|
111
|
+
LineChannelData,
|
|
112
|
+
LineConfig,
|
|
113
|
+
LineProbeResult,
|
|
114
|
+
ResolvedLineAccount,
|
|
115
|
+
} from "./src/types.js";
|
|
116
|
+
export { createLineNodeWebhookHandler, readLineWebhookRequestBody } from "./src/webhook-node.js";
|
|
117
|
+
export {
|
|
118
|
+
createLineWebhookMiddleware,
|
|
119
|
+
type LineWebhookOptions,
|
|
120
|
+
startLineWebhook,
|
|
121
|
+
type StartLineWebhookOptions,
|
|
122
|
+
} from "./src/webhook.js";
|
|
123
|
+
export { parseLineWebhookBody } from "./src/webhook-utils.js";
|
|
124
|
+
export { datetimePickerAction, messageAction, postbackAction, uriAction } from "./src/actions.js";
|
|
125
|
+
export type { Action } from "./src/actions.js";
|
|
126
|
+
export {
|
|
127
|
+
createActionCard,
|
|
128
|
+
createAgendaCard,
|
|
129
|
+
createAppleTvRemoteCard,
|
|
130
|
+
createCarousel,
|
|
131
|
+
createDeviceControlCard,
|
|
132
|
+
createEventCard,
|
|
133
|
+
createImageCard,
|
|
134
|
+
createInfoCard,
|
|
135
|
+
createListCard,
|
|
136
|
+
createMediaPlayerCard,
|
|
137
|
+
createNotificationBubble,
|
|
138
|
+
createReceiptCard,
|
|
139
|
+
toFlexMessage,
|
|
140
|
+
} from "./src/flex-templates.js";
|
|
141
|
+
export type {
|
|
142
|
+
CardAction,
|
|
143
|
+
FlexBox,
|
|
144
|
+
FlexBubble,
|
|
145
|
+
FlexButton,
|
|
146
|
+
FlexCarousel,
|
|
147
|
+
FlexComponent,
|
|
148
|
+
FlexContainer,
|
|
149
|
+
FlexImage,
|
|
150
|
+
FlexText,
|
|
151
|
+
ListItem,
|
|
152
|
+
} from "./src/flex-templates.js";
|
|
153
|
+
export {
|
|
154
|
+
cancelDefaultRichMenu,
|
|
155
|
+
createDefaultMenuConfig,
|
|
156
|
+
createGridLayout,
|
|
157
|
+
createRichMenu,
|
|
158
|
+
createRichMenuAlias,
|
|
159
|
+
deleteRichMenu,
|
|
160
|
+
deleteRichMenuAlias,
|
|
161
|
+
getDefaultRichMenuId,
|
|
162
|
+
getRichMenu,
|
|
163
|
+
getRichMenuIdOfUser,
|
|
164
|
+
getRichMenuList,
|
|
165
|
+
linkRichMenuToUser,
|
|
166
|
+
linkRichMenuToUsers,
|
|
167
|
+
setDefaultRichMenu,
|
|
168
|
+
unlinkRichMenuFromUser,
|
|
169
|
+
unlinkRichMenuFromUsers,
|
|
170
|
+
uploadRichMenuImage,
|
|
171
|
+
} from "./src/rich-menu.js";
|
|
172
|
+
export type {
|
|
173
|
+
CreateRichMenuParams,
|
|
174
|
+
RichMenuArea,
|
|
175
|
+
RichMenuAreaRequest,
|
|
176
|
+
RichMenuRequest,
|
|
177
|
+
RichMenuResponse,
|
|
178
|
+
RichMenuSize,
|
|
179
|
+
} from "./src/rich-menu.js";
|
package/setup-api.ts
ADDED
package/setup-entry.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type LineCredentialAccount = {
|
|
2
|
+
channelAccessToken?: string;
|
|
3
|
+
channelSecret?: string;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export function hasLineCredentials(account: LineCredentialAccount): boolean {
|
|
7
|
+
return Boolean(account.channelAccessToken?.trim() && account.channelSecret?.trim());
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function parseLineAllowFromId(raw: string): string | null {
|
|
11
|
+
const trimmed = raw.trim().replace(/^line:(?:user:)?/i, "");
|
|
12
|
+
if (!/^U[a-f0-9]{32}$/i.test(trimmed)) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
return trimmed;
|
|
16
|
+
}
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
6
|
+
import {
|
|
7
|
+
resolveLineAccount,
|
|
8
|
+
resolveDefaultLineAccountId,
|
|
9
|
+
normalizeAccountId,
|
|
10
|
+
DEFAULT_ACCOUNT_ID,
|
|
11
|
+
} from "./accounts.js";
|
|
12
|
+
|
|
13
|
+
describe("LINE accounts", () => {
|
|
14
|
+
const tempDirs: string[] = [];
|
|
15
|
+
|
|
16
|
+
const createSecretFile = (fileName: string, contents: string) => {
|
|
17
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "klaw-line-account-"));
|
|
18
|
+
tempDirs.push(dir);
|
|
19
|
+
const filePath = path.join(dir, fileName);
|
|
20
|
+
fs.writeFileSync(filePath, contents, "utf8");
|
|
21
|
+
return filePath;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
vi.stubEnv("LINE_CHANNEL_ACCESS_TOKEN", "");
|
|
26
|
+
vi.stubEnv("LINE_CHANNEL_SECRET", "");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
vi.unstubAllEnvs();
|
|
31
|
+
for (const dir of tempDirs.splice(0)) {
|
|
32
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe("resolveLineAccount", () => {
|
|
37
|
+
it("resolves account from config", () => {
|
|
38
|
+
const cfg: KlawConfig = {
|
|
39
|
+
channels: {
|
|
40
|
+
line: {
|
|
41
|
+
enabled: true,
|
|
42
|
+
channelAccessToken: "test-token",
|
|
43
|
+
channelSecret: "test-secret",
|
|
44
|
+
name: "Test Bot",
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const account = resolveLineAccount({ cfg });
|
|
50
|
+
|
|
51
|
+
expect(account.accountId).toBe(DEFAULT_ACCOUNT_ID);
|
|
52
|
+
expect(account.enabled).toBe(true);
|
|
53
|
+
expect(account.channelAccessToken).toBe("test-token");
|
|
54
|
+
expect(account.channelSecret).toBe("test-secret");
|
|
55
|
+
expect(account.name).toBe("Test Bot");
|
|
56
|
+
expect(account.tokenSource).toBe("config");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("resolves account from environment variables", () => {
|
|
60
|
+
vi.stubEnv("LINE_CHANNEL_ACCESS_TOKEN", "env-token");
|
|
61
|
+
vi.stubEnv("LINE_CHANNEL_SECRET", "env-secret");
|
|
62
|
+
|
|
63
|
+
const cfg: KlawConfig = {
|
|
64
|
+
channels: {
|
|
65
|
+
line: {
|
|
66
|
+
enabled: true,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const account = resolveLineAccount({ cfg });
|
|
72
|
+
|
|
73
|
+
expect(account.channelAccessToken).toBe("env-token");
|
|
74
|
+
expect(account.channelSecret).toBe("env-secret");
|
|
75
|
+
expect(account.tokenSource).toBe("env");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("resolves named account", () => {
|
|
79
|
+
const cfg: KlawConfig = {
|
|
80
|
+
channels: {
|
|
81
|
+
line: {
|
|
82
|
+
enabled: true,
|
|
83
|
+
accounts: {
|
|
84
|
+
business: {
|
|
85
|
+
enabled: true,
|
|
86
|
+
channelAccessToken: "business-token",
|
|
87
|
+
channelSecret: "business-secret",
|
|
88
|
+
name: "Business Bot",
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const account = resolveLineAccount({ cfg, accountId: "business" });
|
|
96
|
+
|
|
97
|
+
expect(account.accountId).toBe("business");
|
|
98
|
+
expect(account.enabled).toBe(true);
|
|
99
|
+
expect(account.channelAccessToken).toBe("business-token");
|
|
100
|
+
expect(account.channelSecret).toBe("business-secret");
|
|
101
|
+
expect(account.name).toBe("Business Bot");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("uses configured defaultAccount when accountId is omitted", () => {
|
|
105
|
+
const cfg: KlawConfig = {
|
|
106
|
+
channels: {
|
|
107
|
+
line: {
|
|
108
|
+
defaultAccount: "business",
|
|
109
|
+
accounts: {
|
|
110
|
+
business: {
|
|
111
|
+
enabled: true,
|
|
112
|
+
channelAccessToken: "business-token",
|
|
113
|
+
channelSecret: "business-secret",
|
|
114
|
+
name: "Business Bot",
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const account = resolveLineAccount({ cfg });
|
|
122
|
+
|
|
123
|
+
expect(account.accountId).toBe("business");
|
|
124
|
+
expect(account.enabled).toBe(true);
|
|
125
|
+
expect(account.channelAccessToken).toBe("business-token");
|
|
126
|
+
expect(account.channelSecret).toBe("business-secret");
|
|
127
|
+
expect(account.name).toBe("Business Bot");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("returns empty token when not configured", () => {
|
|
131
|
+
const cfg: KlawConfig = {};
|
|
132
|
+
|
|
133
|
+
const account = resolveLineAccount({ cfg });
|
|
134
|
+
|
|
135
|
+
expect(account.channelAccessToken).toBe("");
|
|
136
|
+
expect(account.channelSecret).toBe("");
|
|
137
|
+
expect(account.tokenSource).toBe("none");
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("resolves default account credentials from files", () => {
|
|
141
|
+
const cfg: KlawConfig = {
|
|
142
|
+
channels: {
|
|
143
|
+
line: {
|
|
144
|
+
tokenFile: createSecretFile("token.txt", "file-token\n"),
|
|
145
|
+
secretFile: createSecretFile("secret.txt", "file-secret\n"),
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const account = resolveLineAccount({ cfg });
|
|
151
|
+
|
|
152
|
+
expect(account.channelAccessToken).toBe("file-token");
|
|
153
|
+
expect(account.channelSecret).toBe("file-secret");
|
|
154
|
+
expect(account.tokenSource).toBe("file");
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("resolves named account credentials from account-level files", () => {
|
|
158
|
+
const cfg: KlawConfig = {
|
|
159
|
+
channels: {
|
|
160
|
+
line: {
|
|
161
|
+
accounts: {
|
|
162
|
+
business: {
|
|
163
|
+
tokenFile: createSecretFile("business-token.txt", "business-file-token\n"),
|
|
164
|
+
secretFile: createSecretFile("business-secret.txt", "business-file-secret\n"),
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const account = resolveLineAccount({ cfg, accountId: "business" });
|
|
172
|
+
|
|
173
|
+
expect(account.channelAccessToken).toBe("business-file-token");
|
|
174
|
+
expect(account.channelSecret).toBe("business-file-secret");
|
|
175
|
+
expect(account.tokenSource).toBe("file");
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it.runIf(process.platform !== "win32")("rejects symlinked token and secret files", () => {
|
|
179
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "klaw-line-account-"));
|
|
180
|
+
tempDirs.push(dir);
|
|
181
|
+
const tokenFile = path.join(dir, "token.txt");
|
|
182
|
+
const tokenLink = path.join(dir, "token-link.txt");
|
|
183
|
+
const secretFile = path.join(dir, "secret.txt");
|
|
184
|
+
const secretLink = path.join(dir, "secret-link.txt");
|
|
185
|
+
fs.writeFileSync(tokenFile, "file-token\n", "utf8");
|
|
186
|
+
fs.writeFileSync(secretFile, "file-secret\n", "utf8");
|
|
187
|
+
fs.symlinkSync(tokenFile, tokenLink);
|
|
188
|
+
fs.symlinkSync(secretFile, secretLink);
|
|
189
|
+
|
|
190
|
+
const cfg: KlawConfig = {
|
|
191
|
+
channels: {
|
|
192
|
+
line: {
|
|
193
|
+
tokenFile: tokenLink,
|
|
194
|
+
secretFile: secretLink,
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const account = resolveLineAccount({ cfg });
|
|
200
|
+
expect(account.channelAccessToken).toBe("");
|
|
201
|
+
expect(account.channelSecret).toBe("");
|
|
202
|
+
expect(account.tokenSource).toBe("none");
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe("resolveDefaultLineAccountId", () => {
|
|
207
|
+
it.each([
|
|
208
|
+
{
|
|
209
|
+
name: "prefers channels.line.defaultAccount when configured",
|
|
210
|
+
cfg: {
|
|
211
|
+
channels: {
|
|
212
|
+
line: {
|
|
213
|
+
defaultAccount: "business",
|
|
214
|
+
accounts: {
|
|
215
|
+
business: { enabled: true },
|
|
216
|
+
support: { enabled: true },
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
} satisfies KlawConfig,
|
|
221
|
+
expected: "business",
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
name: "normalizes channels.line.defaultAccount before lookup",
|
|
225
|
+
cfg: {
|
|
226
|
+
channels: {
|
|
227
|
+
line: {
|
|
228
|
+
defaultAccount: "Business Ops",
|
|
229
|
+
accounts: {
|
|
230
|
+
"business-ops": { enabled: true },
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
} satisfies KlawConfig,
|
|
235
|
+
expected: "business-ops",
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
name: "returns first named account when default not configured",
|
|
239
|
+
cfg: {
|
|
240
|
+
channels: {
|
|
241
|
+
line: {
|
|
242
|
+
accounts: {
|
|
243
|
+
business: { enabled: true },
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
} satisfies KlawConfig,
|
|
248
|
+
expected: "business",
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
name: "falls back when channels.line.defaultAccount is missing",
|
|
252
|
+
cfg: {
|
|
253
|
+
channels: {
|
|
254
|
+
line: {
|
|
255
|
+
defaultAccount: "missing",
|
|
256
|
+
accounts: {
|
|
257
|
+
business: { enabled: true },
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
} satisfies KlawConfig,
|
|
262
|
+
expected: "business",
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
name: "prefers the default account when base credentials are configured",
|
|
266
|
+
cfg: {
|
|
267
|
+
channels: {
|
|
268
|
+
line: {
|
|
269
|
+
channelAccessToken: "base-token",
|
|
270
|
+
accounts: {
|
|
271
|
+
business: { enabled: true },
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
} satisfies KlawConfig,
|
|
276
|
+
expected: DEFAULT_ACCOUNT_ID,
|
|
277
|
+
},
|
|
278
|
+
])("$name", ({ cfg, expected }) => {
|
|
279
|
+
expect(resolveDefaultLineAccountId(cfg)).toBe(expected);
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
describe("normalizeAccountId", () => {
|
|
284
|
+
it("trims and lowercases account ids", () => {
|
|
285
|
+
expect(normalizeAccountId(" Business ")).toBe("business");
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
});
|
package/src/accounts.ts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_ACCOUNT_ID,
|
|
3
|
+
normalizeAccountId as normalizeSharedAccountId,
|
|
4
|
+
normalizeOptionalAccountId,
|
|
5
|
+
} from "klaw/plugin-sdk/account-id";
|
|
6
|
+
import type { KlawConfig } from "klaw/plugin-sdk/account-resolution";
|
|
7
|
+
import { resolveAccountEntry } from "klaw/plugin-sdk/account-resolution";
|
|
8
|
+
import { tryReadSecretFileSync } from "klaw/plugin-sdk/core";
|
|
9
|
+
import type {
|
|
10
|
+
LineAccountConfig,
|
|
11
|
+
LineConfig,
|
|
12
|
+
LineTokenSource,
|
|
13
|
+
ResolvedLineAccount,
|
|
14
|
+
} from "./types.js";
|
|
15
|
+
|
|
16
|
+
export { DEFAULT_ACCOUNT_ID } from "klaw/plugin-sdk/account-id";
|
|
17
|
+
|
|
18
|
+
function readFileIfExists(filePath: string | undefined): string | undefined {
|
|
19
|
+
return tryReadSecretFileSync(filePath, "LINE credential file", { rejectSymlink: true });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function resolveToken(params: {
|
|
23
|
+
accountId: string;
|
|
24
|
+
baseConfig?: LineConfig;
|
|
25
|
+
accountConfig?: LineAccountConfig;
|
|
26
|
+
}): { token: string; tokenSource: LineTokenSource } {
|
|
27
|
+
const { accountId, baseConfig, accountConfig } = params;
|
|
28
|
+
|
|
29
|
+
if (accountConfig?.channelAccessToken?.trim()) {
|
|
30
|
+
return { token: accountConfig.channelAccessToken.trim(), tokenSource: "config" };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const accountFileToken = readFileIfExists(accountConfig?.tokenFile);
|
|
34
|
+
if (accountFileToken) {
|
|
35
|
+
return { token: accountFileToken, tokenSource: "file" };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (accountId === DEFAULT_ACCOUNT_ID) {
|
|
39
|
+
if (baseConfig?.channelAccessToken?.trim()) {
|
|
40
|
+
return { token: baseConfig.channelAccessToken.trim(), tokenSource: "config" };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const baseFileToken = readFileIfExists(baseConfig?.tokenFile);
|
|
44
|
+
if (baseFileToken) {
|
|
45
|
+
return { token: baseFileToken, tokenSource: "file" };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const envToken = process.env.LINE_CHANNEL_ACCESS_TOKEN?.trim();
|
|
49
|
+
if (envToken) {
|
|
50
|
+
return { token: envToken, tokenSource: "env" };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { token: "", tokenSource: "none" };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function resolveSecret(params: {
|
|
58
|
+
accountId: string;
|
|
59
|
+
baseConfig?: LineConfig;
|
|
60
|
+
accountConfig?: LineAccountConfig;
|
|
61
|
+
}): string {
|
|
62
|
+
const { accountId, baseConfig, accountConfig } = params;
|
|
63
|
+
|
|
64
|
+
if (accountConfig?.channelSecret?.trim()) {
|
|
65
|
+
return accountConfig.channelSecret.trim();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const accountFileSecret = readFileIfExists(accountConfig?.secretFile);
|
|
69
|
+
if (accountFileSecret) {
|
|
70
|
+
return accountFileSecret;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (accountId === DEFAULT_ACCOUNT_ID) {
|
|
74
|
+
if (baseConfig?.channelSecret?.trim()) {
|
|
75
|
+
return baseConfig.channelSecret.trim();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const baseFileSecret = readFileIfExists(baseConfig?.secretFile);
|
|
79
|
+
if (baseFileSecret) {
|
|
80
|
+
return baseFileSecret;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const envSecret = process.env.LINE_CHANNEL_SECRET?.trim();
|
|
84
|
+
if (envSecret) {
|
|
85
|
+
return envSecret;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return "";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function resolveLineAccount(params: {
|
|
93
|
+
cfg: KlawConfig;
|
|
94
|
+
accountId?: string;
|
|
95
|
+
}): ResolvedLineAccount {
|
|
96
|
+
const cfg = params.cfg;
|
|
97
|
+
const accountId = normalizeSharedAccountId(params.accountId ?? resolveDefaultLineAccountId(cfg));
|
|
98
|
+
const lineConfig = cfg.channels?.line as LineConfig | undefined;
|
|
99
|
+
const accounts = lineConfig?.accounts;
|
|
100
|
+
const accountConfig =
|
|
101
|
+
accountId !== DEFAULT_ACCOUNT_ID ? resolveAccountEntry(accounts, accountId) : undefined;
|
|
102
|
+
|
|
103
|
+
const { token, tokenSource } = resolveToken({
|
|
104
|
+
accountId,
|
|
105
|
+
baseConfig: lineConfig,
|
|
106
|
+
accountConfig,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const secret = resolveSecret({
|
|
110
|
+
accountId,
|
|
111
|
+
baseConfig: lineConfig,
|
|
112
|
+
accountConfig,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const {
|
|
116
|
+
accounts: _ignoredAccounts,
|
|
117
|
+
defaultAccount: _ignoredDefaultAccount,
|
|
118
|
+
...lineBase
|
|
119
|
+
} = (lineConfig ?? {}) as LineConfig & {
|
|
120
|
+
accounts?: unknown;
|
|
121
|
+
defaultAccount?: unknown;
|
|
122
|
+
};
|
|
123
|
+
const mergedConfig: LineConfig & LineAccountConfig = {
|
|
124
|
+
...lineBase,
|
|
125
|
+
...accountConfig,
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const enabled =
|
|
129
|
+
accountConfig?.enabled ??
|
|
130
|
+
(accountId === DEFAULT_ACCOUNT_ID ? (lineConfig?.enabled ?? true) : false);
|
|
131
|
+
|
|
132
|
+
const name =
|
|
133
|
+
accountConfig?.name ?? (accountId === DEFAULT_ACCOUNT_ID ? lineConfig?.name : undefined);
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
accountId,
|
|
137
|
+
name,
|
|
138
|
+
enabled,
|
|
139
|
+
channelAccessToken: token,
|
|
140
|
+
channelSecret: secret,
|
|
141
|
+
tokenSource,
|
|
142
|
+
config: mergedConfig,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function listLineAccountIds(cfg: KlawConfig): string[] {
|
|
147
|
+
const lineConfig = cfg.channels?.line as LineConfig | undefined;
|
|
148
|
+
const accounts = lineConfig?.accounts;
|
|
149
|
+
const ids = new Set<string>();
|
|
150
|
+
|
|
151
|
+
if (
|
|
152
|
+
lineConfig?.channelAccessToken?.trim() ||
|
|
153
|
+
lineConfig?.tokenFile ||
|
|
154
|
+
process.env.LINE_CHANNEL_ACCESS_TOKEN?.trim()
|
|
155
|
+
) {
|
|
156
|
+
ids.add(DEFAULT_ACCOUNT_ID);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (accounts) {
|
|
160
|
+
for (const id of Object.keys(accounts)) {
|
|
161
|
+
ids.add(id);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return Array.from(ids);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function resolveDefaultLineAccountId(cfg: KlawConfig): string {
|
|
169
|
+
const preferred = normalizeOptionalAccountId(
|
|
170
|
+
(cfg.channels?.line as LineConfig | undefined)?.defaultAccount,
|
|
171
|
+
);
|
|
172
|
+
if (
|
|
173
|
+
preferred &&
|
|
174
|
+
listLineAccountIds(cfg).some((accountId) => normalizeSharedAccountId(accountId) === preferred)
|
|
175
|
+
) {
|
|
176
|
+
return preferred;
|
|
177
|
+
}
|
|
178
|
+
const ids = listLineAccountIds(cfg);
|
|
179
|
+
if (ids.includes(DEFAULT_ACCOUNT_ID)) {
|
|
180
|
+
return DEFAULT_ACCOUNT_ID;
|
|
181
|
+
}
|
|
182
|
+
return ids[0] ?? DEFAULT_ACCOUNT_ID;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function normalizeAccountId(accountId: string | undefined): string {
|
|
186
|
+
return normalizeSharedAccountId(accountId);
|
|
187
|
+
}
|