@openclaw/msteams 2026.5.2-beta.2 → 2026.5.3-beta.1

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.
Files changed (197) hide show
  1. package/dist/api.js +3 -0
  2. package/dist/channel-D7hdreTh.js +984 -0
  3. package/dist/channel-config-api.js +2 -0
  4. package/dist/channel-plugin-api.js +2 -0
  5. package/dist/channel.runtime-BC1ruIfN.js +573 -0
  6. package/dist/config-schema-B8QezH6t.js +15 -0
  7. package/dist/contract-api.js +2 -0
  8. package/dist/graph-users-9uQJepqr.js +1354 -0
  9. package/dist/index.js +22 -0
  10. package/dist/oauth-BWJyilR1.js +114 -0
  11. package/dist/oauth.token-xxpoLWy5.js +115 -0
  12. package/dist/policy-DTnU2GR7.js +142 -0
  13. package/dist/probe-D_H8yFps.js +2194 -0
  14. package/dist/resolve-allowlist-D41JSziq.js +219 -0
  15. package/dist/runtime-api-DV1iVMn1.js +28 -0
  16. package/dist/runtime-api.js +2 -0
  17. package/dist/secret-contract-BuoEXmPS.js +35 -0
  18. package/dist/secret-contract-api.js +2 -0
  19. package/dist/setup-entry.js +15 -0
  20. package/dist/setup-plugin-api.js +64 -0
  21. package/dist/setup-surface-BLkFQYIQ.js +313 -0
  22. package/dist/src-CFp1QpFd.js +4064 -0
  23. package/dist/test-api.js +2 -0
  24. package/package.json +14 -6
  25. package/api.ts +0 -3
  26. package/channel-config-api.ts +0 -1
  27. package/channel-plugin-api.ts +0 -2
  28. package/config-api.ts +0 -4
  29. package/contract-api.ts +0 -4
  30. package/index.ts +0 -20
  31. package/runtime-api.ts +0 -73
  32. package/secret-contract-api.ts +0 -5
  33. package/setup-entry.ts +0 -13
  34. package/setup-plugin-api.ts +0 -3
  35. package/src/ai-entity.ts +0 -7
  36. package/src/approval-auth.ts +0 -44
  37. package/src/attachments/bot-framework.test.ts +0 -461
  38. package/src/attachments/bot-framework.ts +0 -362
  39. package/src/attachments/download.ts +0 -311
  40. package/src/attachments/graph.test.ts +0 -416
  41. package/src/attachments/graph.ts +0 -484
  42. package/src/attachments/html.ts +0 -122
  43. package/src/attachments/payload.ts +0 -14
  44. package/src/attachments/remote-media.test.ts +0 -137
  45. package/src/attachments/remote-media.ts +0 -112
  46. package/src/attachments/shared.test.ts +0 -530
  47. package/src/attachments/shared.ts +0 -626
  48. package/src/attachments/types.ts +0 -47
  49. package/src/attachments.graph.test.ts +0 -342
  50. package/src/attachments.helpers.test.ts +0 -246
  51. package/src/attachments.test-helpers.ts +0 -17
  52. package/src/attachments.test.ts +0 -687
  53. package/src/attachments.ts +0 -18
  54. package/src/block-streaming-config.test.ts +0 -61
  55. package/src/channel-api.ts +0 -1
  56. package/src/channel.actions.test.ts +0 -742
  57. package/src/channel.directory.test.ts +0 -200
  58. package/src/channel.runtime.ts +0 -56
  59. package/src/channel.setup.ts +0 -77
  60. package/src/channel.test.ts +0 -128
  61. package/src/channel.ts +0 -1136
  62. package/src/config-schema.ts +0 -6
  63. package/src/config-ui-hints.ts +0 -12
  64. package/src/conversation-store-fs.test.ts +0 -74
  65. package/src/conversation-store-fs.ts +0 -149
  66. package/src/conversation-store-helpers.test.ts +0 -202
  67. package/src/conversation-store-helpers.ts +0 -105
  68. package/src/conversation-store-memory.ts +0 -51
  69. package/src/conversation-store.shared.test.ts +0 -225
  70. package/src/conversation-store.ts +0 -71
  71. package/src/directory-live.test.ts +0 -156
  72. package/src/directory-live.ts +0 -111
  73. package/src/doctor.ts +0 -27
  74. package/src/errors.test.ts +0 -133
  75. package/src/errors.ts +0 -246
  76. package/src/feedback-reflection-prompt.ts +0 -117
  77. package/src/feedback-reflection-store.ts +0 -114
  78. package/src/feedback-reflection.test.ts +0 -237
  79. package/src/feedback-reflection.ts +0 -283
  80. package/src/file-consent-helpers.test.ts +0 -326
  81. package/src/file-consent-helpers.ts +0 -126
  82. package/src/file-consent-invoke.ts +0 -150
  83. package/src/file-consent.test.ts +0 -363
  84. package/src/file-consent.ts +0 -287
  85. package/src/graph-chat.ts +0 -55
  86. package/src/graph-group-management.test.ts +0 -318
  87. package/src/graph-group-management.ts +0 -168
  88. package/src/graph-members.test.ts +0 -89
  89. package/src/graph-members.ts +0 -48
  90. package/src/graph-messages.actions.test.ts +0 -243
  91. package/src/graph-messages.read.test.ts +0 -391
  92. package/src/graph-messages.search.test.ts +0 -213
  93. package/src/graph-messages.test-helpers.ts +0 -50
  94. package/src/graph-messages.ts +0 -534
  95. package/src/graph-teams.test.ts +0 -215
  96. package/src/graph-teams.ts +0 -114
  97. package/src/graph-thread.test.ts +0 -246
  98. package/src/graph-thread.ts +0 -146
  99. package/src/graph-upload.test.ts +0 -258
  100. package/src/graph-upload.ts +0 -531
  101. package/src/graph-users.ts +0 -29
  102. package/src/graph.test.ts +0 -516
  103. package/src/graph.ts +0 -293
  104. package/src/inbound.test.ts +0 -221
  105. package/src/inbound.ts +0 -148
  106. package/src/index.ts +0 -4
  107. package/src/media-helpers.test.ts +0 -202
  108. package/src/media-helpers.ts +0 -105
  109. package/src/mentions.test.ts +0 -244
  110. package/src/mentions.ts +0 -114
  111. package/src/messenger.test.ts +0 -865
  112. package/src/messenger.ts +0 -605
  113. package/src/monitor-handler/access.ts +0 -125
  114. package/src/monitor-handler/inbound-media.test.ts +0 -289
  115. package/src/monitor-handler/inbound-media.ts +0 -180
  116. package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
  117. package/src/monitor-handler/message-handler.authz.test.ts +0 -669
  118. package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
  119. package/src/monitor-handler/message-handler.test-support.ts +0 -100
  120. package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -223
  121. package/src/monitor-handler/message-handler.thread-session.test.ts +0 -77
  122. package/src/monitor-handler/message-handler.ts +0 -1000
  123. package/src/monitor-handler/reaction-handler.test.ts +0 -267
  124. package/src/monitor-handler/reaction-handler.ts +0 -210
  125. package/src/monitor-handler/thread-session.ts +0 -17
  126. package/src/monitor-handler.adaptive-card.test.ts +0 -162
  127. package/src/monitor-handler.feedback-authz.test.ts +0 -314
  128. package/src/monitor-handler.file-consent.test.ts +0 -423
  129. package/src/monitor-handler.sso.test.ts +0 -563
  130. package/src/monitor-handler.test-helpers.ts +0 -180
  131. package/src/monitor-handler.ts +0 -534
  132. package/src/monitor-handler.types.ts +0 -27
  133. package/src/monitor-types.ts +0 -6
  134. package/src/monitor.lifecycle.test.ts +0 -278
  135. package/src/monitor.test.ts +0 -119
  136. package/src/monitor.ts +0 -442
  137. package/src/oauth.flow.ts +0 -77
  138. package/src/oauth.shared.ts +0 -37
  139. package/src/oauth.test.ts +0 -305
  140. package/src/oauth.token.ts +0 -158
  141. package/src/oauth.ts +0 -130
  142. package/src/outbound.test.ts +0 -130
  143. package/src/outbound.ts +0 -71
  144. package/src/pending-uploads-fs.test.ts +0 -246
  145. package/src/pending-uploads-fs.ts +0 -235
  146. package/src/pending-uploads.test.ts +0 -173
  147. package/src/pending-uploads.ts +0 -121
  148. package/src/policy.test.ts +0 -240
  149. package/src/policy.ts +0 -262
  150. package/src/polls-store-memory.ts +0 -32
  151. package/src/polls.test.ts +0 -160
  152. package/src/polls.ts +0 -323
  153. package/src/presentation.ts +0 -68
  154. package/src/probe.test.ts +0 -77
  155. package/src/probe.ts +0 -132
  156. package/src/reply-dispatcher.test.ts +0 -437
  157. package/src/reply-dispatcher.ts +0 -346
  158. package/src/reply-stream-controller.test.ts +0 -235
  159. package/src/reply-stream-controller.ts +0 -147
  160. package/src/resolve-allowlist.test.ts +0 -250
  161. package/src/resolve-allowlist.ts +0 -309
  162. package/src/revoked-context.ts +0 -17
  163. package/src/runtime.ts +0 -9
  164. package/src/sdk-types.ts +0 -59
  165. package/src/sdk.test.ts +0 -666
  166. package/src/sdk.ts +0 -884
  167. package/src/secret-contract.ts +0 -49
  168. package/src/secret-input.ts +0 -7
  169. package/src/send-context.ts +0 -231
  170. package/src/send.test.ts +0 -493
  171. package/src/send.ts +0 -637
  172. package/src/sent-message-cache.test.ts +0 -15
  173. package/src/sent-message-cache.ts +0 -56
  174. package/src/session-route.ts +0 -40
  175. package/src/setup-core.ts +0 -160
  176. package/src/setup-surface.test.ts +0 -202
  177. package/src/setup-surface.ts +0 -320
  178. package/src/sso-token-store.test.ts +0 -72
  179. package/src/sso-token-store.ts +0 -166
  180. package/src/sso.ts +0 -300
  181. package/src/storage.ts +0 -25
  182. package/src/store-fs.ts +0 -44
  183. package/src/streaming-message.test.ts +0 -262
  184. package/src/streaming-message.ts +0 -297
  185. package/src/test-runtime.ts +0 -16
  186. package/src/thread-parent-context.test.ts +0 -224
  187. package/src/thread-parent-context.ts +0 -159
  188. package/src/token-response.ts +0 -11
  189. package/src/token.test.ts +0 -259
  190. package/src/token.ts +0 -195
  191. package/src/user-agent.test.ts +0 -86
  192. package/src/user-agent.ts +0 -53
  193. package/src/webhook-timeouts.ts +0 -27
  194. package/src/welcome-card.test.ts +0 -81
  195. package/src/welcome-card.ts +0 -57
  196. package/test-api.ts +0 -1
  197. package/tsconfig.json +0 -16
package/src/sdk.test.ts DELETED
@@ -1,666 +0,0 @@
1
- import * as fs from "node:fs";
2
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3
- import {
4
- createBotFrameworkJwtValidator,
5
- createMSTeamsAdapter,
6
- createMSTeamsApp,
7
- type MSTeamsTeamsSdk,
8
- } from "./sdk.js";
9
- import type {
10
- MSTeamsCredentials,
11
- MSTeamsSecretCredentials,
12
- MSTeamsFederatedCredentials,
13
- } from "./token.js";
14
-
15
- vi.mock("openclaw/plugin-sdk/ssrf-runtime", async () => {
16
- const actual = await vi.importActual<typeof import("openclaw/plugin-sdk/ssrf-runtime")>(
17
- "openclaw/plugin-sdk/ssrf-runtime",
18
- );
19
- return {
20
- ...actual,
21
- fetchWithSsrFGuard: async (params: {
22
- url: string;
23
- init?: RequestInit;
24
- fetchImpl?: typeof fetch;
25
- }) => ({
26
- response: await (params.fetchImpl ?? fetch)(params.url, params.init),
27
- finalUrl: params.url,
28
- release: async () => {},
29
- }),
30
- };
31
- });
32
-
33
- const clientConstructorState = vi.hoisted(() => ({
34
- calls: [] as Array<{ serviceUrl: string; options: unknown }>,
35
- }));
36
-
37
- // Track jwt.verify calls to assert audience/issuer/algorithm config.
38
- const jwtState = vi.hoisted(() => ({
39
- verifyBehavior: "success" as "success" | "throw",
40
- decodedHeader: { kid: "key-1" } as { kid?: string } | null,
41
- decodedPayload: { iss: "https://api.botframework.com" } as { iss?: string } | string | null,
42
- verifyResult: { sub: "ok" } as unknown,
43
- verifyCalls: [] as Array<{ token: string; options: unknown }>,
44
- }));
45
-
46
- const jwtMockImpl = {
47
- decode: (token: string, opts?: { complete?: boolean }) => {
48
- if (opts?.complete) {
49
- return jwtState.decodedHeader ? { header: jwtState.decodedHeader } : null;
50
- }
51
- return jwtState.decodedPayload;
52
- },
53
- verify: (token: string, _key: string, options: unknown) => {
54
- jwtState.verifyCalls.push({ token, options });
55
- if (jwtState.verifyBehavior === "throw") {
56
- throw new Error("invalid signature");
57
- }
58
- return jwtState.verifyResult;
59
- },
60
- };
61
-
62
- vi.mock("jsonwebtoken", () => ({
63
- // Match jsonwebtoken@9 under dynamic ESM import from plugin package deps:
64
- // Node exposes decode as a named export, while verify is only on default.
65
- decode: jwtMockImpl.decode,
66
- default: jwtMockImpl,
67
- }));
68
-
69
- vi.mock("jwks-rsa", () => ({
70
- JwksClient: class JwksClient {
71
- async getSigningKey(_kid: string) {
72
- return { getPublicKey: () => "mock-public-key" };
73
- }
74
- },
75
- }));
76
-
77
- vi.mock("node:fs", async (importOriginal) => {
78
- const actual = await importOriginal<typeof import("node:fs")>();
79
- return {
80
- ...actual,
81
- readFileSync: vi.fn(
82
- () => "-----BEGIN RSA PRIVATE KEY-----\nfake-key\n-----END RSA PRIVATE KEY-----",
83
- ),
84
- };
85
- });
86
-
87
- const { mockGetToken } = vi.hoisted(() => {
88
- const mockGetToken = vi.fn().mockResolvedValue({ token: "mock-managed-token" });
89
- return { mockGetToken };
90
- });
91
- vi.mock("@azure/identity", () => {
92
- // Use classes so `new ...Credential()` works after vitest hoisting
93
- // (function declarations inside vi.mock factories can be transformed
94
- // into arrow functions during hoisting, which breaks `new`).
95
- class ManagedIdentityCredential {
96
- getToken = mockGetToken;
97
- }
98
- class DefaultAzureCredential {
99
- getToken = mockGetToken;
100
- }
101
- class ClientCertificateCredential {
102
- getToken = mockGetToken;
103
- }
104
- return { ManagedIdentityCredential, DefaultAzureCredential, ClientCertificateCredential };
105
- });
106
-
107
- const originalFetch = globalThis.fetch;
108
-
109
- afterEach(() => {
110
- globalThis.fetch = originalFetch;
111
- clientConstructorState.calls.length = 0;
112
- jwtState.verifyCalls.length = 0;
113
- jwtState.verifyBehavior = "success";
114
- jwtState.decodedHeader = { kid: "key-1" };
115
- jwtState.decodedPayload = { iss: "https://api.botframework.com" };
116
- jwtState.verifyResult = { sub: "ok" };
117
- vi.restoreAllMocks();
118
- });
119
-
120
- function createSdkStub(): MSTeamsTeamsSdk {
121
- class AppStub {
122
- async getBotToken() {
123
- return {
124
- toString() {
125
- return "bot-token";
126
- },
127
- };
128
- }
129
- }
130
-
131
- class ClientStub {
132
- constructor(serviceUrl: string, options: unknown) {
133
- clientConstructorState.calls.push({ serviceUrl, options });
134
- }
135
-
136
- conversations = {
137
- activities: (_conversationId: string) => ({
138
- create: async (_activity: unknown) => ({ id: "created" }),
139
- }),
140
- };
141
- }
142
-
143
- return {
144
- App: AppStub as unknown as MSTeamsTeamsSdk["App"],
145
- Client: ClientStub as unknown as MSTeamsTeamsSdk["Client"],
146
- };
147
- }
148
-
149
- describe("createMSTeamsApp", () => {
150
- it("does not crash with express 5 path-to-regexp (#55161)", async () => {
151
- // Regression test for: https://github.com/openclaw/openclaw/issues/55161
152
- // createMSTeamsApp passes a no-op httpServerAdapter to prevent the SDK from
153
- // creating its default HttpPlugin (which registers `/api*` — invalid in Express 5).
154
- const { App } = await import("@microsoft/teams.apps");
155
- const { Client } = await import("@microsoft/teams.api");
156
- const sdk: MSTeamsTeamsSdk = { App, Client };
157
- const creds: MSTeamsCredentials = {
158
- type: "secret",
159
- appId: "test-app-id",
160
- appPassword: "test-secret",
161
- tenantId: "test-tenant",
162
- };
163
-
164
- // This would throw "Missing parameter name at index 5: /api*" without the fix
165
- const app = await createMSTeamsApp(creds, sdk);
166
- expect(app).toBeDefined();
167
- // Verify token methods are available (the reason we use the App class)
168
- expect(typeof (app as unknown as Record<string, unknown>).getBotToken).toBe("function");
169
- });
170
- });
171
-
172
- describe("createMSTeamsAdapter", () => {
173
- it("provides deleteActivity in proactive continueConversation contexts", async () => {
174
- const fetchMock = vi.fn(async () => new Response(null, { status: 204 }));
175
- globalThis.fetch = fetchMock as unknown as typeof fetch;
176
-
177
- const creds = {
178
- appId: "app-id",
179
- type: "secret",
180
- appPassword: "secret",
181
- tenantId: "tenant-id",
182
- } satisfies MSTeamsCredentials;
183
- const sdk = createSdkStub();
184
- const app = new sdk.App({
185
- clientId: creds.appId,
186
- clientSecret: creds.appPassword,
187
- tenantId: creds.tenantId,
188
- });
189
- const adapter = createMSTeamsAdapter(app, sdk);
190
-
191
- await adapter.continueConversation(
192
- creds.appId,
193
- {
194
- serviceUrl: "https://example.com/",
195
- conversation: { id: "19:conversation@thread.tacv2" },
196
- channelId: "msteams",
197
- },
198
- async (ctx) => {
199
- await ctx.deleteActivity("activity-123");
200
- },
201
- );
202
-
203
- expect(fetchMock).toHaveBeenCalledWith(
204
- "https://example.com/v3/conversations/19%3Aconversation%40thread.tacv2/activities/activity-123",
205
- expect.objectContaining({
206
- method: "DELETE",
207
- headers: expect.objectContaining({
208
- Authorization: "Bearer bot-token",
209
- }),
210
- }),
211
- );
212
- });
213
-
214
- it("passes the OpenClaw User-Agent to the Bot Framework connector client", async () => {
215
- const creds = {
216
- type: "secret",
217
- appId: "app-id",
218
- appPassword: "secret",
219
- tenantId: "tenant-id",
220
- } satisfies MSTeamsCredentials;
221
- const sdk = createSdkStub();
222
- const app = new sdk.App({
223
- clientId: creds.appId,
224
- clientSecret: creds.appPassword,
225
- tenantId: creds.tenantId,
226
- });
227
- const adapter = createMSTeamsAdapter(app, sdk);
228
-
229
- await adapter.continueConversation(
230
- creds.appId,
231
- {
232
- serviceUrl: "https://service.example.com/",
233
- conversation: { id: "19:conversation@thread.tacv2" },
234
- channelId: "msteams",
235
- },
236
- async (ctx) => {
237
- await ctx.sendActivity("hello");
238
- },
239
- );
240
-
241
- expect(clientConstructorState.calls).toHaveLength(1);
242
- expect(clientConstructorState.calls[0]).toMatchObject({
243
- serviceUrl: "https://service.example.com/",
244
- options: {
245
- headers: {
246
- "User-Agent": expect.stringMatching(/^teams\.ts\[apps\]\/.+ OpenClaw\/.+$/),
247
- },
248
- },
249
- });
250
- });
251
- });
252
-
253
- describe("createBotFrameworkJwtValidator", () => {
254
- const creds = {
255
- appId: "app-id",
256
- type: "secret",
257
- appPassword: "secret",
258
- tenantId: "tenant-id",
259
- } satisfies MSTeamsCredentials;
260
-
261
- it("validates a token with Bot Framework issuer and correct audience list", async () => {
262
- jwtState.decodedPayload = { iss: "https://api.botframework.com" };
263
-
264
- const validator = await createBotFrameworkJwtValidator(creds);
265
- await expect(validator.validate("Bearer token-bf")).resolves.toBe(true);
266
-
267
- expect(jwtState.verifyCalls).toHaveLength(1);
268
- const opts = jwtState.verifyCalls[0]?.options as Record<string, unknown>;
269
- expect(opts.audience).toEqual(["app-id", "api://app-id", "https://api.botframework.com"]);
270
- expect(opts.algorithms).toEqual(["RS256"]);
271
- expect(opts.clockTolerance).toBe(300);
272
- });
273
-
274
- it("accepts tokens with aud: https://api.botframework.com (#58249)", async () => {
275
- // This is the critical fix: the old JwtValidator rejected this audience.
276
- jwtState.decodedPayload = { iss: "https://api.botframework.com" };
277
- jwtState.verifyResult = {
278
- aud: ["https://api.botframework.com"],
279
- appid: creds.appId,
280
- };
281
-
282
- const validator = await createBotFrameworkJwtValidator(creds);
283
- await expect(validator.validate("Bearer botfw-token")).resolves.toBe(true);
284
-
285
- const opts = jwtState.verifyCalls[0]?.options as Record<string, unknown>;
286
- expect((opts.audience as string[]).includes("https://api.botframework.com")).toBe(true);
287
- });
288
-
289
- it("accepts global audience tokens when azp matches the configured app id", async () => {
290
- jwtState.decodedPayload = { iss: "https://api.botframework.com" };
291
- jwtState.verifyResult = {
292
- aud: ["https://api.botframework.com"],
293
- azp: "APP-ID",
294
- };
295
-
296
- const validator = await createBotFrameworkJwtValidator(creds);
297
- await expect(validator.validate("Bearer botfw-token-azp")).resolves.toBe(true);
298
- });
299
-
300
- it("rejects global audience tokens when app binding does not match the configured app id", async () => {
301
- jwtState.decodedPayload = { iss: "https://api.botframework.com" };
302
- jwtState.verifyResult = {
303
- aud: ["https://api.botframework.com"],
304
- azp: "other-app-id",
305
- };
306
-
307
- const validator = await createBotFrameworkJwtValidator(creds);
308
- await expect(validator.validate("Bearer botfw-token-wrong-app")).resolves.toBe(false);
309
- });
310
-
311
- it("rejects non-object verified payloads", async () => {
312
- jwtState.decodedPayload = { iss: "https://api.botframework.com" };
313
- jwtState.verifyResult = "verified-string-payload";
314
-
315
- const validator = await createBotFrameworkJwtValidator(creds);
316
- await expect(validator.validate("Bearer botfw-token-string")).resolves.toBe(false);
317
- });
318
-
319
- it("validates a token with Entra issuer", async () => {
320
- jwtState.decodedPayload = { iss: `https://login.microsoftonline.com/tenant-id/v2.0` };
321
-
322
- const validator = await createBotFrameworkJwtValidator(creds);
323
- await expect(validator.validate("Bearer token-entra")).resolves.toBe(true);
324
-
325
- expect(jwtState.verifyCalls).toHaveLength(1);
326
- const opts = jwtState.verifyCalls[0]?.options as Record<string, unknown>;
327
- expect(opts.issuer as string[]).toContain("https://login.microsoftonline.com/tenant-id/v2.0");
328
- });
329
-
330
- it("validates a SingleTenant token with tenant-scoped STS Windows issuer (#64270)", async () => {
331
- // Regression for #64270: the sts.windows.net issuer was hardcoded to a
332
- // single tenant UUID, so every other SingleTenant bot deployment hit 401.
333
- // The tenant-aware form must accept the deployment's own tenant.
334
- jwtState.decodedPayload = {
335
- iss: `https://sts.windows.net/${creds.tenantId}/`,
336
- };
337
-
338
- const validator = await createBotFrameworkJwtValidator(creds);
339
- await expect(validator.validate("Bearer token-sts")).resolves.toBe(true);
340
-
341
- expect(jwtState.verifyCalls).toHaveLength(1);
342
- const opts = jwtState.verifyCalls[0]?.options as Record<string, unknown>;
343
- expect(opts.issuer as string[]).toContain(`https://sts.windows.net/${creds.tenantId}/`);
344
- });
345
-
346
- it("rejects STS Windows tokens issued by a different tenant (#64270)", async () => {
347
- // Guardrail against regressing back to a hardcoded tenant: the previously
348
- // hardcoded UUID must NOT be accepted when the bot is configured for a
349
- // different tenant. This also prevents cross-tenant token reuse.
350
- jwtState.decodedPayload = {
351
- iss: "https://sts.windows.net/d6d49420-f39b-4df7-a1dc-d59a935871db/",
352
- };
353
-
354
- const validator = await createBotFrameworkJwtValidator(creds);
355
- await expect(validator.validate("Bearer token-sts-other-tenant")).resolves.toBe(false);
356
- expect(jwtState.verifyCalls).toHaveLength(0);
357
- });
358
-
359
- it("rejects tokens with unknown issuer", async () => {
360
- jwtState.decodedPayload = { iss: "https://evil.example.com" };
361
-
362
- const validator = await createBotFrameworkJwtValidator(creds);
363
- await expect(validator.validate("Bearer token-evil")).resolves.toBe(false);
364
- expect(jwtState.verifyCalls).toHaveLength(0);
365
- });
366
-
367
- it("returns false when signature verification fails", async () => {
368
- jwtState.verifyBehavior = "throw";
369
-
370
- const validator = await createBotFrameworkJwtValidator(creds);
371
- await expect(validator.validate("Bearer token-bad")).resolves.toBe(false);
372
- });
373
-
374
- it("returns false for empty bearer token", async () => {
375
- const validator = await createBotFrameworkJwtValidator(creds);
376
- await expect(validator.validate("Bearer ")).resolves.toBe(false);
377
- expect(jwtState.verifyCalls).toHaveLength(0);
378
- });
379
-
380
- it("returns false when token has no kid header", async () => {
381
- jwtState.decodedHeader = { kid: undefined };
382
-
383
- const validator = await createBotFrameworkJwtValidator(creds);
384
- await expect(validator.validate("Bearer no-kid")).resolves.toBe(false);
385
- expect(jwtState.verifyCalls).toHaveLength(0);
386
- });
387
-
388
- it("returns false when token has no issuer claim", async () => {
389
- jwtState.decodedPayload = { iss: undefined };
390
-
391
- const validator = await createBotFrameworkJwtValidator(creds);
392
- await expect(validator.validate("Bearer no-iss")).resolves.toBe(false);
393
- expect(jwtState.verifyCalls).toHaveLength(0);
394
- });
395
- });
396
-
397
- function makeFakeSdk() {
398
- const appInstances: Record<string, unknown>[] = [];
399
- const FakeClient = function FakeClient() {};
400
- const FakeApp = class {
401
- opts: Record<string, unknown>;
402
- constructor(opts: Record<string, unknown>) {
403
- this.opts = opts;
404
- appInstances.push(opts);
405
- }
406
- };
407
- return { sdk: { App: FakeApp as any, Client: FakeClient as any }, appInstances, FakeApp };
408
- }
409
-
410
- describe("createMSTeamsApp – secret credentials", () => {
411
- it("passes clientId, clientSecret, tenantId to sdk.App", async () => {
412
- const { sdk, appInstances } = makeFakeSdk();
413
- const creds: MSTeamsSecretCredentials = {
414
- type: "secret",
415
- appId: "my-app-id",
416
- appPassword: "my-secret",
417
- tenantId: "my-tenant",
418
- };
419
- const app = await createMSTeamsApp(creds, sdk);
420
- expect(app).toBeDefined();
421
- expect(appInstances[0]).toMatchObject({
422
- clientId: "my-app-id",
423
- clientSecret: "my-secret",
424
- tenantId: "my-tenant",
425
- });
426
- });
427
- });
428
-
429
- describe("createMSTeamsApp – federated certificate credentials", () => {
430
- beforeEach(() => {
431
- vi.mocked(fs.readFileSync).mockReturnValue(
432
- "-----BEGIN RSA PRIVATE KEY-----\nfake-key\n-----END RSA PRIVATE KEY-----",
433
- );
434
- });
435
-
436
- it("reads the certificate and creates app with token function", async () => {
437
- const { sdk, appInstances } = makeFakeSdk();
438
- const creds: MSTeamsFederatedCredentials = {
439
- type: "federated",
440
- appId: "fed-app-id",
441
- tenantId: "fed-tenant",
442
- certificatePath: "/certs/bot.pem",
443
- certificateThumbprint: "AABB1122",
444
- };
445
- await createMSTeamsApp(creds, sdk);
446
- expect(fs.readFileSync).toHaveBeenCalledWith("/certs/bot.pem", "utf-8");
447
- expect(appInstances[0]).toMatchObject({
448
- clientId: "fed-app-id",
449
- tenantId: "fed-tenant",
450
- });
451
- expect(typeof appInstances[0].token).toBe("function");
452
- const token = await (appInstances[0].token as (scope: string) => Promise<string>)(
453
- "https://api.botframework.com/.default",
454
- );
455
- expect(token).toBe("mock-managed-token");
456
- });
457
-
458
- it("wraps readFileSync errors with descriptive message", async () => {
459
- vi.mocked(fs.readFileSync).mockImplementation(() => {
460
- throw new Error("ENOENT: no such file or directory");
461
- });
462
- const { sdk } = makeFakeSdk();
463
- const creds: MSTeamsFederatedCredentials = {
464
- type: "federated",
465
- appId: "fed-app-id",
466
- tenantId: "fed-tenant",
467
- certificatePath: "/missing/cert.pem",
468
- };
469
- await expect(async () => await createMSTeamsApp(creds, sdk)).rejects.toThrow(
470
- /Failed to read certificate file at '\/missing\/cert\.pem'/,
471
- );
472
- });
473
-
474
- it("throws when federated but no certificatePath and no managedIdentity", async () => {
475
- const { sdk } = makeFakeSdk();
476
- const creds: MSTeamsFederatedCredentials = {
477
- type: "federated",
478
- appId: "fed-app-id",
479
- tenantId: "fed-tenant",
480
- };
481
- await expect(async () => await createMSTeamsApp(creds, sdk)).rejects.toThrow(
482
- /certificate path or managed identity/i,
483
- );
484
- });
485
- });
486
-
487
- describe("createMSTeamsApp – federated managed identity", () => {
488
- it("creates app with token function for user-assigned MI", async () => {
489
- const { sdk, appInstances } = makeFakeSdk();
490
- const creds: MSTeamsFederatedCredentials = {
491
- type: "federated",
492
- appId: "mi-app-id",
493
- tenantId: "mi-tenant",
494
- useManagedIdentity: true,
495
- managedIdentityClientId: "mi-client-id",
496
- };
497
- await createMSTeamsApp(creds, sdk);
498
- expect(appInstances[0]).toMatchObject({ clientId: "mi-app-id", tenantId: "mi-tenant" });
499
- expect(typeof appInstances[0].token).toBe("function");
500
- const token = await (appInstances[0].token as (scope: string) => Promise<string>)(
501
- "https://api.botframework.com/.default",
502
- );
503
- expect(token).toBe("mock-managed-token");
504
- });
505
-
506
- it("creates app with token function for system-assigned MI", async () => {
507
- const { sdk, appInstances } = makeFakeSdk();
508
- const creds: MSTeamsFederatedCredentials = {
509
- type: "federated",
510
- appId: "mi-app-id",
511
- tenantId: "mi-tenant",
512
- useManagedIdentity: true,
513
- };
514
- await createMSTeamsApp(creds, sdk);
515
- expect(typeof appInstances[0].token).toBe("function");
516
- const token = await (appInstances[0].token as (scope: string) => Promise<string>)(
517
- "https://api.botframework.com/.default",
518
- );
519
- expect(token).toBe("mock-managed-token");
520
- });
521
-
522
- it("throws from token function when token acquisition fails", async () => {
523
- mockGetToken.mockResolvedValueOnce(null);
524
- const { sdk, appInstances } = makeFakeSdk();
525
- const creds: MSTeamsFederatedCredentials = {
526
- type: "federated",
527
- appId: "mi-app-id",
528
- tenantId: "mi-tenant",
529
- useManagedIdentity: true,
530
- };
531
- await createMSTeamsApp(creds, sdk);
532
- const tokenFn = appInstances[0].token as (scope: string) => Promise<string>;
533
- await expect(tokenFn("https://api.botframework.com/.default")).rejects.toThrow(
534
- /failed to acquire token/i,
535
- );
536
- });
537
- });
538
-
539
- // ── createMSTeamsAdapter tests ─────────────────────────────────────────────
540
-
541
- function makeFakeApp() {
542
- return {
543
- getBotToken: vi.fn().mockResolvedValue({ toString: () => "fake-bot-token" }),
544
- } as any;
545
- }
546
-
547
- function makeFakeApiSdk() {
548
- const createFn = vi.fn().mockResolvedValue({ id: "new-activity-id" });
549
- const FakeApp = function FakeApp() {};
550
- const FakeClient = class {
551
- conversations = {
552
- activities: (_convId: string) => ({ create: createFn }),
553
- };
554
- };
555
- return {
556
- sdk: { App: FakeApp as any, Client: FakeClient as any },
557
- createFn,
558
- };
559
- }
560
-
561
- describe("createMSTeamsAdapter – continueConversation", () => {
562
- const originalFetch = globalThis.fetch;
563
-
564
- afterEach(() => {
565
- globalThis.fetch = originalFetch;
566
- vi.restoreAllMocks();
567
- });
568
-
569
- it("provides sendActivity via REST API client in logic callback", async () => {
570
- const { sdk, createFn } = makeFakeApiSdk();
571
- const adapter = createMSTeamsAdapter(makeFakeApp(), sdk);
572
-
573
- const reference = {
574
- serviceUrl: "https://smba.trafficmanager.net/teams/",
575
- conversation: { id: "conv-123", conversationType: "personal" },
576
- channelId: "msteams",
577
- };
578
-
579
- await adapter.continueConversation("app-id", reference, async (ctx) => {
580
- await ctx.sendActivity("hello from proactive send");
581
- });
582
-
583
- expect(createFn).toHaveBeenCalledTimes(1);
584
- expect(createFn).toHaveBeenCalledWith(
585
- expect.objectContaining({ type: "message", text: "hello from proactive send" }),
586
- );
587
- });
588
-
589
- it("provides deleteActivity via REST DELETE in logic callback", async () => {
590
- const mockFetch = vi.fn().mockResolvedValue({ ok: true });
591
- globalThis.fetch = mockFetch;
592
- const { sdk } = makeFakeApiSdk();
593
- const adapter = createMSTeamsAdapter(makeFakeApp(), sdk);
594
-
595
- const reference = {
596
- serviceUrl: "https://smba.trafficmanager.net/teams/",
597
- conversation: { id: "conv-456", conversationType: "personal" },
598
- channelId: "msteams",
599
- };
600
-
601
- await adapter.continueConversation("app-id", reference, async (ctx) => {
602
- await ctx.deleteActivity("activity-789");
603
- });
604
-
605
- expect(mockFetch).toHaveBeenCalledTimes(1);
606
- const [url, opts] = mockFetch.mock.calls[0];
607
- expect(url).toContain("/v3/conversations/conv-456/activities/activity-789");
608
- expect(opts.method).toBe("DELETE");
609
- expect(opts.headers.Authorization).toBe("Bearer fake-bot-token");
610
- });
611
-
612
- it("throws when serviceUrl is missing", async () => {
613
- const { sdk } = makeFakeApiSdk();
614
- const adapter = createMSTeamsAdapter(makeFakeApp(), sdk);
615
-
616
- await expect(
617
- adapter.continueConversation("app-id", { conversation: { id: "c" } } as any, async () => {}),
618
- ).rejects.toThrow(/Missing serviceUrl/);
619
- });
620
-
621
- it("throws when conversation.id is missing", async () => {
622
- const { sdk } = makeFakeApiSdk();
623
- const adapter = createMSTeamsAdapter(makeFakeApp(), sdk);
624
-
625
- await expect(
626
- adapter.continueConversation(
627
- "app-id",
628
- { serviceUrl: "https://example.com" } as any,
629
- async () => {},
630
- ),
631
- ).rejects.toThrow(/Missing conversation\.id/);
632
- });
633
- });
634
-
635
- describe("createMSTeamsAdapter – process", () => {
636
- it("sends 200 for normal message activities", async () => {
637
- const { sdk } = makeFakeApiSdk();
638
- const adapter = createMSTeamsAdapter(makeFakeApp(), sdk);
639
-
640
- const req = { body: { type: "message", text: "hi" } };
641
- const sendFn = vi.fn();
642
- const res = { status: vi.fn(() => ({ send: sendFn })) };
643
-
644
- await adapter.process(req, res, async () => {});
645
-
646
- expect(res.status).toHaveBeenCalledWith(200);
647
- expect(sendFn).toHaveBeenCalled();
648
- });
649
-
650
- it("sends 200 immediately for invoke activities", async () => {
651
- const { sdk } = makeFakeApiSdk();
652
- const adapter = createMSTeamsAdapter(makeFakeApp(), sdk);
653
-
654
- const req = { body: { type: "invoke", name: "adaptiveCard/action" } };
655
- const sendFn = vi.fn();
656
- const res = { status: vi.fn(() => ({ send: sendFn })) };
657
-
658
- let statusCalledBeforeLogic = false;
659
- await adapter.process(req, res, async () => {
660
- statusCalledBeforeLogic = res.status.mock.calls.length > 0;
661
- });
662
-
663
- expect(statusCalledBeforeLogic).toBe(true);
664
- expect(res.status).toHaveBeenCalledWith(200);
665
- });
666
- });