@kodelyth/msteams 2026.5.42 → 2026.6.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 (177) hide show
  1. package/klaw.plugin.json +726 -2
  2. package/package.json +16 -4
  3. package/api.ts +0 -3
  4. package/channel-config-api.ts +0 -1
  5. package/channel-plugin-api.ts +0 -2
  6. package/config-api.ts +0 -4
  7. package/contract-api.ts +0 -4
  8. package/index.ts +0 -20
  9. package/runtime-api.ts +0 -66
  10. package/secret-contract-api.ts +0 -5
  11. package/setup-entry.ts +0 -13
  12. package/setup-plugin-api.ts +0 -3
  13. package/src/ai-entity.ts +0 -7
  14. package/src/approval-auth.ts +0 -44
  15. package/src/attachments/bot-framework.test.ts +0 -506
  16. package/src/attachments/bot-framework.ts +0 -348
  17. package/src/attachments/download.ts +0 -328
  18. package/src/attachments/graph.test.ts +0 -441
  19. package/src/attachments/graph.ts +0 -489
  20. package/src/attachments/html.ts +0 -122
  21. package/src/attachments/payload.ts +0 -14
  22. package/src/attachments/remote-media.test.ts +0 -187
  23. package/src/attachments/remote-media.ts +0 -86
  24. package/src/attachments/shared.test.ts +0 -547
  25. package/src/attachments/shared.ts +0 -655
  26. package/src/attachments/types.ts +0 -47
  27. package/src/attachments.graph.test.ts +0 -414
  28. package/src/attachments.helpers.test.ts +0 -245
  29. package/src/attachments.test-helpers.ts +0 -17
  30. package/src/attachments.test.ts +0 -754
  31. package/src/attachments.ts +0 -18
  32. package/src/block-streaming-config.test.ts +0 -61
  33. package/src/channel-api.ts +0 -1
  34. package/src/channel.actions.test.ts +0 -797
  35. package/src/channel.directory.test.ts +0 -176
  36. package/src/channel.message-adapter.test.ts +0 -227
  37. package/src/channel.runtime.ts +0 -56
  38. package/src/channel.setup.ts +0 -77
  39. package/src/channel.test.ts +0 -136
  40. package/src/channel.ts +0 -1176
  41. package/src/config-schema.ts +0 -6
  42. package/src/config-ui-hints.ts +0 -40
  43. package/src/conversation-store-fs.test.ts +0 -81
  44. package/src/conversation-store-fs.ts +0 -149
  45. package/src/conversation-store-helpers.test.ts +0 -202
  46. package/src/conversation-store-helpers.ts +0 -105
  47. package/src/conversation-store-memory.ts +0 -51
  48. package/src/conversation-store.shared.test.ts +0 -260
  49. package/src/conversation-store.ts +0 -71
  50. package/src/directory-live.test.ts +0 -156
  51. package/src/directory-live.ts +0 -111
  52. package/src/doctor.ts +0 -27
  53. package/src/errors.test.ts +0 -154
  54. package/src/errors.ts +0 -270
  55. package/src/feedback-reflection-prompt.ts +0 -117
  56. package/src/feedback-reflection-store.ts +0 -113
  57. package/src/feedback-reflection.test.ts +0 -237
  58. package/src/feedback-reflection.ts +0 -268
  59. package/src/file-consent-helpers.test.ts +0 -328
  60. package/src/file-consent-helpers.ts +0 -115
  61. package/src/file-consent-invoke.ts +0 -150
  62. package/src/file-consent.test.ts +0 -378
  63. package/src/file-consent.ts +0 -223
  64. package/src/graph-chat.ts +0 -36
  65. package/src/graph-group-management.test.ts +0 -332
  66. package/src/graph-group-management.ts +0 -168
  67. package/src/graph-members.test.ts +0 -89
  68. package/src/graph-members.ts +0 -48
  69. package/src/graph-messages.actions.test.ts +0 -253
  70. package/src/graph-messages.read.test.ts +0 -391
  71. package/src/graph-messages.search.test.ts +0 -227
  72. package/src/graph-messages.test-helpers.ts +0 -50
  73. package/src/graph-messages.ts +0 -534
  74. package/src/graph-teams.test.ts +0 -222
  75. package/src/graph-teams.ts +0 -114
  76. package/src/graph-thread.test.ts +0 -252
  77. package/src/graph-thread.ts +0 -146
  78. package/src/graph-upload.test.ts +0 -253
  79. package/src/graph-upload.ts +0 -531
  80. package/src/graph-users.ts +0 -29
  81. package/src/graph.test.ts +0 -540
  82. package/src/graph.ts +0 -308
  83. package/src/inbound.test.ts +0 -221
  84. package/src/inbound.ts +0 -148
  85. package/src/index.ts +0 -4
  86. package/src/media-helpers.test.ts +0 -220
  87. package/src/media-helpers.ts +0 -105
  88. package/src/mentions.test.ts +0 -254
  89. package/src/mentions.ts +0 -114
  90. package/src/messenger.test.ts +0 -961
  91. package/src/messenger.ts +0 -608
  92. package/src/monitor-handler/access.ts +0 -136
  93. package/src/monitor-handler/inbound-media.test.ts +0 -314
  94. package/src/monitor-handler/inbound-media.ts +0 -180
  95. package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
  96. package/src/monitor-handler/message-handler.authz.test.ts +0 -739
  97. package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
  98. package/src/monitor-handler/message-handler.test-support.ts +0 -99
  99. package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -225
  100. package/src/monitor-handler/message-handler.thread-session.test.ts +0 -132
  101. package/src/monitor-handler/message-handler.ts +0 -1003
  102. package/src/monitor-handler/reaction-handler.test.ts +0 -325
  103. package/src/monitor-handler/reaction-handler.ts +0 -122
  104. package/src/monitor-handler/thread-session.ts +0 -30
  105. package/src/monitor-handler.adaptive-card.test.ts +0 -158
  106. package/src/monitor-handler.feedback-authz.test.ts +0 -357
  107. package/src/monitor-handler.file-consent.test.ts +0 -443
  108. package/src/monitor-handler.sso.test.ts +0 -576
  109. package/src/monitor-handler.test-helpers.ts +0 -181
  110. package/src/monitor-handler.ts +0 -538
  111. package/src/monitor-handler.types.ts +0 -27
  112. package/src/monitor-types.ts +0 -6
  113. package/src/monitor.lifecycle.test.ts +0 -457
  114. package/src/monitor.test.ts +0 -119
  115. package/src/monitor.ts +0 -476
  116. package/src/oauth.flow.ts +0 -77
  117. package/src/oauth.shared.ts +0 -37
  118. package/src/oauth.test.ts +0 -350
  119. package/src/oauth.token.ts +0 -162
  120. package/src/oauth.ts +0 -130
  121. package/src/outbound.test.ts +0 -400
  122. package/src/outbound.ts +0 -198
  123. package/src/pending-uploads-fs.test.ts +0 -261
  124. package/src/pending-uploads-fs.ts +0 -235
  125. package/src/pending-uploads.test.ts +0 -186
  126. package/src/pending-uploads.ts +0 -121
  127. package/src/policy.test.ts +0 -156
  128. package/src/policy.ts +0 -245
  129. package/src/polls-store-memory.ts +0 -32
  130. package/src/polls.test.ts +0 -169
  131. package/src/polls.ts +0 -312
  132. package/src/presentation.ts +0 -93
  133. package/src/probe.test.ts +0 -79
  134. package/src/probe.ts +0 -132
  135. package/src/reply-dispatcher.test.ts +0 -543
  136. package/src/reply-dispatcher.ts +0 -523
  137. package/src/reply-stream-controller.test.ts +0 -424
  138. package/src/reply-stream-controller.ts +0 -334
  139. package/src/resolve-allowlist.test.ts +0 -253
  140. package/src/resolve-allowlist.ts +0 -309
  141. package/src/revoked-context.ts +0 -17
  142. package/src/runtime.ts +0 -12
  143. package/src/sdk-types.ts +0 -59
  144. package/src/sdk.test.ts +0 -727
  145. package/src/sdk.ts +0 -916
  146. package/src/secret-contract.ts +0 -49
  147. package/src/secret-input.ts +0 -7
  148. package/src/send-context.test.ts +0 -93
  149. package/src/send-context.ts +0 -269
  150. package/src/send.test.ts +0 -588
  151. package/src/send.ts +0 -697
  152. package/src/sent-message-cache.test.ts +0 -106
  153. package/src/sent-message-cache.ts +0 -174
  154. package/src/session-route.ts +0 -40
  155. package/src/setup-core.ts +0 -162
  156. package/src/setup-surface.test.ts +0 -175
  157. package/src/setup-surface.ts +0 -319
  158. package/src/sso-token-store.test.ts +0 -74
  159. package/src/sso-token-store.ts +0 -166
  160. package/src/sso.ts +0 -300
  161. package/src/storage.ts +0 -25
  162. package/src/store-fs.ts +0 -42
  163. package/src/streaming-message.test.ts +0 -323
  164. package/src/streaming-message.ts +0 -327
  165. package/src/test-runtime.ts +0 -16
  166. package/src/thread-parent-context.test.ts +0 -224
  167. package/src/thread-parent-context.ts +0 -159
  168. package/src/token-response.ts +0 -11
  169. package/src/token.test.ts +0 -268
  170. package/src/token.ts +0 -194
  171. package/src/user-agent.test.ts +0 -121
  172. package/src/user-agent.ts +0 -53
  173. package/src/webhook-timeouts.ts +0 -27
  174. package/src/welcome-card.test.ts +0 -104
  175. package/src/welcome-card.ts +0 -57
  176. package/test-api.ts +0 -1
  177. package/tsconfig.json +0 -16
@@ -1,576 +0,0 @@
1
- import { beforeAll, describe, expect, it, vi } from "vitest";
2
- import type { KlawConfig } from "../runtime-api.js";
3
- import {
4
- type MSTeamsActivityHandler,
5
- type MSTeamsMessageHandlerDeps,
6
- registerMSTeamsHandlers,
7
- } from "./monitor-handler.js";
8
- import {
9
- createActivityHandler as baseCreateActivityHandler,
10
- createMSTeamsMessageHandlerDeps,
11
- installMSTeamsTestRuntime,
12
- } from "./monitor-handler.test-helpers.js";
13
- import type { MSTeamsTurnContext } from "./sdk-types.js";
14
- import { createMSTeamsSsoTokenStoreMemory } from "./sso-token-store.js";
15
- import {
16
- type MSTeamsSsoFetch,
17
- handleSigninTokenExchangeInvoke,
18
- handleSigninVerifyStateInvoke,
19
- parseSigninTokenExchangeValue,
20
- parseSigninVerifyStateValue,
21
- } from "./sso.js";
22
-
23
- function createActivityHandler() {
24
- const run = vi.fn(async () => undefined);
25
- const handler = baseCreateActivityHandler(run);
26
- return { handler, run };
27
- }
28
-
29
- function createDepsWithoutSso(
30
- overrides: Partial<MSTeamsMessageHandlerDeps> = {},
31
- ): MSTeamsMessageHandlerDeps {
32
- const base = createMSTeamsMessageHandlerDeps();
33
- return { ...base, ...overrides };
34
- }
35
-
36
- function createSsoDeps(params: { fetchImpl: MSTeamsSsoFetch }) {
37
- const tokenStore = createMSTeamsSsoTokenStoreMemory();
38
- const tokenProvider = {
39
- getAccessToken: vi.fn(async () => "bf-service-token"),
40
- };
41
- return {
42
- sso: {
43
- tokenProvider,
44
- tokenStore,
45
- connectionName: "GraphConnection",
46
- fetchImpl: params.fetchImpl,
47
- },
48
- tokenStore,
49
- tokenProvider,
50
- };
51
- }
52
-
53
- function createRegisteredSsoHandler(sso: MSTeamsMessageHandlerDeps["sso"]) {
54
- const deps = createDepsWithoutSso({ sso });
55
- const { handler } = createActivityHandler();
56
- const registered = registerMSTeamsHandlers(handler, deps) as MSTeamsActivityHandler & {
57
- run: NonNullable<MSTeamsActivityHandler["run"]>;
58
- };
59
- return { deps, registered };
60
- }
61
-
62
- function createSigninInvokeContext(params: {
63
- name: "signin/tokenExchange" | "signin/verifyState";
64
- value: unknown;
65
- userAadId?: string;
66
- userBfId?: string;
67
- conversationId?: string;
68
- conversationType?: "personal" | "groupChat" | "channel";
69
- teamId?: string;
70
- channelName?: string;
71
- }): MSTeamsTurnContext & { sendActivity: ReturnType<typeof vi.fn> } {
72
- const conversationType = params.conversationType ?? "personal";
73
- const conversationId =
74
- params.conversationId ??
75
- (conversationType === "personal"
76
- ? "19:personal-chat"
77
- : conversationType === "channel"
78
- ? "19:channel@thread.tacv2"
79
- : "19:group@thread.tacv2");
80
-
81
- return {
82
- activity: {
83
- id: "invoke-1",
84
- type: "invoke",
85
- name: params.name,
86
- channelId: "msteams",
87
- serviceUrl: "https://service.example.test",
88
- from: {
89
- id: params.userBfId ?? "bf-user",
90
- aadObjectId: params.userAadId ?? "aad-user-guid",
91
- name: "Test User",
92
- },
93
- recipient: { id: "bot-id", name: "Bot" },
94
- conversation: {
95
- id: conversationId,
96
- conversationType,
97
- tenantId: params.teamId ? "tenant-1" : undefined,
98
- },
99
- channelData: params.teamId
100
- ? {
101
- team: { id: params.teamId, name: "Team 1" },
102
- channel: params.channelName ? { name: params.channelName } : undefined,
103
- }
104
- : {},
105
- attachments: [],
106
- value: params.value,
107
- },
108
- sendActivity: vi.fn(async () => ({ id: "ack-id" })),
109
- sendActivities: vi.fn(async () => []),
110
- updateActivity: vi.fn(async () => ({ id: "update" })),
111
- deleteActivity: vi.fn(async () => {}),
112
- } as unknown as MSTeamsTurnContext & {
113
- sendActivity: ReturnType<typeof vi.fn>;
114
- };
115
- }
116
-
117
- function createFakeFetch(handlers: Array<(url: string, init?: unknown) => unknown>) {
118
- const calls: Array<{ url: string; init?: unknown }> = [];
119
- const fetchImpl: MSTeamsSsoFetch = async (url, init) => {
120
- calls.push({ url, init });
121
- const handler = handlers.shift();
122
- if (!handler) {
123
- throw new Error("unexpected fetch call");
124
- }
125
- const response = handler(url, init) as {
126
- ok: boolean;
127
- status: number;
128
- body: unknown;
129
- };
130
- return {
131
- ok: response.ok,
132
- status: response.status,
133
- json: async () => response.body,
134
- text: async () =>
135
- typeof response.body === "string" ? response.body : JSON.stringify(response.body ?? ""),
136
- };
137
- };
138
- return { fetchImpl, calls };
139
- }
140
-
141
- function expectInvokeResponse(sendActivity: ReturnType<typeof vi.fn>, status?: number): void {
142
- const activity = sendActivity.mock.calls.find(([arg]) => {
143
- return (
144
- typeof arg === "object" &&
145
- arg !== null &&
146
- (arg as { type?: unknown }).type === "invokeResponse"
147
- );
148
- })?.[0] as { value?: { status?: unknown } } | undefined;
149
-
150
- if (!activity) {
151
- throw new Error("Expected invokeResponse activity");
152
- }
153
- if (status !== undefined) {
154
- expect(activity.value?.status).toBe(status);
155
- }
156
- }
157
-
158
- function expectLogFields(logFn: unknown, message: string, fields: Record<string, unknown>): void {
159
- const calls = (logFn as { mock?: { calls?: Array<[unknown, unknown?]> } }).mock?.calls;
160
- if (!calls) {
161
- throw new Error("Expected log mock calls");
162
- }
163
- const call = calls.find(([text]) => text === message);
164
- if (!call) {
165
- throw new Error(`Expected log message: ${message}`);
166
- }
167
- const meta = call[1] as Record<string, unknown> | undefined;
168
- if (!meta) {
169
- throw new Error(`Expected log metadata for: ${message}`);
170
- }
171
- for (const [key, value] of Object.entries(fields)) {
172
- expect(meta[key]).toEqual(value);
173
- }
174
- }
175
-
176
- function createBlockedSigninScenarios() {
177
- return [
178
- {
179
- name: "DM sender outside allowlist",
180
- cfg: {
181
- channels: {
182
- msteams: {
183
- dmPolicy: "allowlist",
184
- allowFrom: ["owner-aad"],
185
- },
186
- },
187
- } as KlawConfig,
188
- context: {
189
- userAadId: "blocked-dm-aad",
190
- },
191
- expectedDropLog: "dropping signin invoke (dm sender not allowlisted)",
192
- },
193
- {
194
- name: "channel outside route allowlist",
195
- cfg: {
196
- channels: {
197
- msteams: {
198
- groupPolicy: "allowlist",
199
- groupAllowFrom: ["blocked-channel-aad"],
200
- teams: {
201
- "team-allowlisted": {
202
- channels: {
203
- "19:allowlisted@thread.tacv2": { requireMention: false },
204
- },
205
- },
206
- },
207
- },
208
- },
209
- } as KlawConfig,
210
- context: {
211
- userAadId: "blocked-channel-aad",
212
- conversationType: "channel" as const,
213
- conversationId: "19:blocked-channel@thread.tacv2",
214
- teamId: "team-blocked",
215
- channelName: "General",
216
- },
217
- expectedDropLog: "dropping signin invoke (not in team/channel allowlist)",
218
- },
219
- {
220
- name: "group sender outside group allowlist",
221
- cfg: {
222
- channels: {
223
- msteams: {
224
- groupPolicy: "allowlist",
225
- groupAllowFrom: ["owner-aad"],
226
- },
227
- },
228
- } as KlawConfig,
229
- context: {
230
- userAadId: "blocked-group-aad",
231
- conversationType: "groupChat" as const,
232
- conversationId: "19:group-chat@thread.v2",
233
- },
234
- expectedDropLog: "dropping signin invoke (group sender not allowlisted)",
235
- },
236
- ];
237
- }
238
-
239
- describe("msteams signin invoke value parsers", () => {
240
- it("parses signin/tokenExchange values", () => {
241
- expect(
242
- parseSigninTokenExchangeValue({
243
- id: "flow-1",
244
- connectionName: "Graph",
245
- token: "eyJ...",
246
- }),
247
- ).toEqual({ id: "flow-1", connectionName: "Graph", token: "eyJ..." });
248
- });
249
-
250
- it("rejects non-object signin/tokenExchange values", () => {
251
- expect(parseSigninTokenExchangeValue(null)).toBeNull();
252
- expect(parseSigninTokenExchangeValue("nope")).toBeNull();
253
- });
254
-
255
- it("parses signin/verifyState values", () => {
256
- expect(parseSigninVerifyStateValue({ state: "123456" })).toEqual({ state: "123456" });
257
- expect(parseSigninVerifyStateValue({})).toEqual({ state: undefined });
258
- expect(parseSigninVerifyStateValue(null)).toBeNull();
259
- });
260
- });
261
-
262
- describe("handleSigninTokenExchangeInvoke", () => {
263
- it("exchanges the Teams token and persists the result", async () => {
264
- const { fetchImpl, calls } = createFakeFetch([
265
- () => ({
266
- ok: true,
267
- status: 200,
268
- body: {
269
- channelId: "msteams",
270
- connectionName: "GraphConnection",
271
- token: "delegated-graph-token",
272
- expiration: "2030-01-01T00:00:00Z",
273
- },
274
- }),
275
- ]);
276
- const { sso, tokenStore } = createSsoDeps({ fetchImpl });
277
-
278
- const result = await handleSigninTokenExchangeInvoke({
279
- value: { id: "flow-1", connectionName: "GraphConnection", token: "exchangeable-token" },
280
- user: { userId: "aad-user-guid", channelId: "msteams" },
281
- deps: sso,
282
- });
283
-
284
- expect(result).toEqual({
285
- ok: true,
286
- token: "delegated-graph-token",
287
- expiresAt: "2030-01-01T00:00:00Z",
288
- });
289
- expect(calls).toHaveLength(1);
290
- expect(calls[0]?.url).toContain("/api/usertoken/exchange");
291
- expect(calls[0]?.url).toContain("userId=aad-user-guid");
292
- expect(calls[0]?.url).toContain("connectionName=GraphConnection");
293
- expect(calls[0]?.url).toContain("channelId=msteams");
294
-
295
- const init = calls[0]?.init as {
296
- method?: string;
297
- headers?: Record<string, string>;
298
- body?: string;
299
- };
300
- expect(init?.method).toBe("POST");
301
- expect(init?.headers?.Authorization).toBe("Bearer bf-service-token");
302
- expect(JSON.parse(init?.body ?? "{}")).toEqual({ token: "exchangeable-token" });
303
-
304
- const stored = await tokenStore.get({
305
- connectionName: "GraphConnection",
306
- userId: "aad-user-guid",
307
- });
308
- expect(stored?.token).toBe("delegated-graph-token");
309
- expect(stored?.expiresAt).toBe("2030-01-01T00:00:00Z");
310
- });
311
-
312
- it("returns a service error when the User Token service rejects the exchange", async () => {
313
- const { fetchImpl } = createFakeFetch([
314
- () => ({ ok: false, status: 502, body: "bad gateway" }),
315
- ]);
316
- const { sso, tokenStore } = createSsoDeps({ fetchImpl });
317
-
318
- const result = await handleSigninTokenExchangeInvoke({
319
- value: { id: "flow-1", connectionName: "GraphConnection", token: "exchangeable-token" },
320
- user: { userId: "aad-user-guid", channelId: "msteams" },
321
- deps: sso,
322
- });
323
-
324
- expect(result.ok).toBe(false);
325
- if (!result.ok) {
326
- expect(result.code).toBe("service_error");
327
- expect(result.status).toBe(502);
328
- expect(result.message).toContain("bad gateway");
329
- }
330
- const stored = await tokenStore.get({
331
- connectionName: "GraphConnection",
332
- userId: "aad-user-guid",
333
- });
334
- expect(stored).toBeNull();
335
- });
336
-
337
- it("refuses to exchange without a user id", async () => {
338
- const { fetchImpl, calls } = createFakeFetch([]);
339
- const { sso } = createSsoDeps({ fetchImpl });
340
-
341
- const result = await handleSigninTokenExchangeInvoke({
342
- value: { id: "flow-1", connectionName: "GraphConnection", token: "exchangeable-token" },
343
- user: { userId: "", channelId: "msteams" },
344
- deps: sso,
345
- });
346
- expect(result.ok).toBe(false);
347
- if (!result.ok) {
348
- expect(result.code).toBe("missing_user");
349
- }
350
- expect(calls).toHaveLength(0);
351
- });
352
- });
353
-
354
- describe("handleSigninVerifyStateInvoke", () => {
355
- it("fetches the user token for the magic code and persists it", async () => {
356
- const { fetchImpl, calls } = createFakeFetch([
357
- () => ({
358
- ok: true,
359
- status: 200,
360
- body: {
361
- channelId: "msteams",
362
- connectionName: "GraphConnection",
363
- token: "delegated-token-2",
364
- expiration: "2031-02-03T04:05:06Z",
365
- },
366
- }),
367
- ]);
368
- const { sso, tokenStore } = createSsoDeps({ fetchImpl });
369
-
370
- const result = await handleSigninVerifyStateInvoke({
371
- value: { state: "654321" },
372
- user: { userId: "aad-user-guid", channelId: "msteams" },
373
- deps: sso,
374
- });
375
-
376
- expect(result.ok).toBe(true);
377
- expect(calls[0]?.url).toContain("/api/usertoken/GetToken");
378
- expect(calls[0]?.url).toContain("code=654321");
379
- const init = calls[0]?.init as { method?: string };
380
- expect(init?.method).toBe("GET");
381
-
382
- const stored = await tokenStore.get({
383
- connectionName: "GraphConnection",
384
- userId: "aad-user-guid",
385
- });
386
- expect(stored?.token).toBe("delegated-token-2");
387
- });
388
-
389
- it("rejects invocations without a state code", async () => {
390
- const { fetchImpl, calls } = createFakeFetch([]);
391
- const { sso } = createSsoDeps({ fetchImpl });
392
- const result = await handleSigninVerifyStateInvoke({
393
- value: { state: " " },
394
- user: { userId: "aad-user-guid", channelId: "msteams" },
395
- deps: sso,
396
- });
397
- expect(result.ok).toBe(false);
398
- if (!result.ok) {
399
- expect(result.code).toBe("missing_state");
400
- }
401
- expect(calls).toHaveLength(0);
402
- });
403
- });
404
-
405
- describe("msteams signin invoke handler registration", () => {
406
- beforeAll(() => {
407
- installMSTeamsTestRuntime();
408
- });
409
-
410
- const blockedSigninScenarios = createBlockedSigninScenarios();
411
- const invokeVariants = [
412
- {
413
- name: "signin/tokenExchange" as const,
414
- value: { id: "x", connectionName: "GraphConnection", token: "exchangeable" },
415
- },
416
- {
417
- name: "signin/verifyState" as const,
418
- value: { state: "112233" },
419
- },
420
- ];
421
-
422
- it("acks signin invokes even when sso is not configured", async () => {
423
- const deps = createDepsWithoutSso();
424
- const { handler, run } = createActivityHandler();
425
- const registered = registerMSTeamsHandlers(handler, deps) as MSTeamsActivityHandler & {
426
- run: NonNullable<MSTeamsActivityHandler["run"]>;
427
- };
428
-
429
- const ctx = createSigninInvokeContext({
430
- name: "signin/tokenExchange",
431
- value: { id: "x", connectionName: "Graph", token: "exchangeable" },
432
- });
433
-
434
- await registered.run(ctx);
435
-
436
- expectInvokeResponse(ctx.sendActivity, 200);
437
- expect(run).not.toHaveBeenCalled();
438
- expectLogFields(deps.log.debug, "signin invoke received but msteams.sso is not configured", {
439
- name: "signin/tokenExchange",
440
- });
441
- });
442
-
443
- for (const invoke of invokeVariants) {
444
- for (const scenario of blockedSigninScenarios) {
445
- it(`does not process ${invoke.name} for ${scenario.name}`, async () => {
446
- const { fetchImpl, calls } = createFakeFetch([
447
- () => ({
448
- ok: true,
449
- status: 200,
450
- body: {
451
- channelId: "msteams",
452
- connectionName: "GraphConnection",
453
- token: "delegated-graph-token",
454
- expiration: "2030-01-01T00:00:00Z",
455
- },
456
- }),
457
- ]);
458
- const { sso, tokenStore } = createSsoDeps({ fetchImpl });
459
- const deps = createDepsWithoutSso({ cfg: scenario.cfg, sso });
460
- const { handler } = createActivityHandler();
461
- const registered = registerMSTeamsHandlers(handler, deps) as MSTeamsActivityHandler & {
462
- run: NonNullable<MSTeamsActivityHandler["run"]>;
463
- };
464
-
465
- const ctx = createSigninInvokeContext({
466
- name: invoke.name,
467
- value: invoke.value,
468
- ...scenario.context,
469
- });
470
-
471
- await registered.run(ctx);
472
-
473
- expectInvokeResponse(ctx.sendActivity, 200);
474
- expect(calls).toHaveLength(0);
475
- const stored = await tokenStore.get({
476
- connectionName: "GraphConnection",
477
- userId: scenario.context.userAadId ?? "aad-user-guid",
478
- });
479
- expect(stored).toBeNull();
480
- expectLogFields(deps.log.debug, scenario.expectedDropLog, { name: invoke.name });
481
- });
482
- }
483
- }
484
-
485
- it("invokes the token exchange handler when sso is configured", async () => {
486
- const { fetchImpl } = createFakeFetch([
487
- () => ({
488
- ok: true,
489
- status: 200,
490
- body: {
491
- channelId: "msteams",
492
- connectionName: "GraphConnection",
493
- token: "delegated-graph-token",
494
- expiration: "2030-01-01T00:00:00Z",
495
- },
496
- }),
497
- ]);
498
- const { sso, tokenStore } = createSsoDeps({ fetchImpl });
499
- const { deps, registered } = createRegisteredSsoHandler(sso);
500
-
501
- const ctx = createSigninInvokeContext({
502
- name: "signin/tokenExchange",
503
- value: { id: "x", connectionName: "GraphConnection", token: "exchangeable" },
504
- });
505
-
506
- await registered.run(ctx);
507
-
508
- expectInvokeResponse(ctx.sendActivity, 200);
509
- expectLogFields(deps.log.info, "msteams sso token exchanged", {
510
- userId: "aad-user-guid",
511
- hasExpiry: true,
512
- });
513
- const stored = await tokenStore.get({
514
- connectionName: "GraphConnection",
515
- userId: "aad-user-guid",
516
- });
517
- expect(stored?.token).toBe("delegated-graph-token");
518
- });
519
-
520
- it("logs an error when the token exchange fails", async () => {
521
- const { fetchImpl } = createFakeFetch([
522
- () => ({ ok: false, status: 400, body: "bad request" }),
523
- ]);
524
- const { sso } = createSsoDeps({ fetchImpl });
525
- const { deps, registered } = createRegisteredSsoHandler(sso);
526
-
527
- const ctx = createSigninInvokeContext({
528
- name: "signin/tokenExchange",
529
- value: { id: "x", connectionName: "GraphConnection", token: "exchangeable" },
530
- });
531
-
532
- await registered.run(ctx);
533
-
534
- expectInvokeResponse(ctx.sendActivity);
535
- expectLogFields(deps.log.error, "msteams sso token exchange failed", {
536
- code: "unexpected_response",
537
- status: 400,
538
- });
539
- });
540
-
541
- it("handles signin/verifyState via the magic-code flow", async () => {
542
- const { fetchImpl } = createFakeFetch([
543
- () => ({
544
- ok: true,
545
- status: 200,
546
- body: {
547
- channelId: "msteams",
548
- connectionName: "GraphConnection",
549
- token: "delegated-token-3",
550
- },
551
- }),
552
- ]);
553
- const { sso, tokenStore } = createSsoDeps({ fetchImpl });
554
- const deps = createDepsWithoutSso({ sso });
555
- const { handler } = createActivityHandler();
556
- const registered = registerMSTeamsHandlers(handler, deps) as MSTeamsActivityHandler & {
557
- run: NonNullable<MSTeamsActivityHandler["run"]>;
558
- };
559
-
560
- const ctx = createSigninInvokeContext({
561
- name: "signin/verifyState",
562
- value: { state: "112233" },
563
- });
564
-
565
- await registered.run(ctx);
566
-
567
- expectLogFields(deps.log.info, "msteams sso verifyState succeeded", {
568
- userId: "aad-user-guid",
569
- });
570
- const stored = await tokenStore.get({
571
- connectionName: "GraphConnection",
572
- userId: "aad-user-guid",
573
- });
574
- expect(stored?.token).toBe("delegated-token-3");
575
- });
576
- });