@openclaw/msteams 2026.5.2 → 2026.5.3-beta.2
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/dist/api.js +3 -0
- package/dist/channel-D7hdreTh.js +984 -0
- package/dist/channel-config-api.js +2 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-BC1ruIfN.js +573 -0
- package/dist/config-schema-B8QezH6t.js +15 -0
- package/dist/contract-api.js +2 -0
- package/dist/graph-users-9uQJepqr.js +1354 -0
- package/dist/index.js +22 -0
- package/dist/oauth-BWJyilR1.js +114 -0
- package/dist/oauth.token-xxpoLWy5.js +115 -0
- package/dist/policy-DTnU2GR7.js +142 -0
- package/dist/probe-D_H8yFps.js +2194 -0
- package/dist/resolve-allowlist-D41JSziq.js +219 -0
- package/dist/runtime-api-DV1iVMn1.js +28 -0
- package/dist/runtime-api.js +2 -0
- package/dist/secret-contract-BuoEXmPS.js +35 -0
- package/dist/secret-contract-api.js +2 -0
- package/dist/setup-entry.js +15 -0
- package/dist/setup-plugin-api.js +64 -0
- package/dist/setup-surface-BLkFQYIQ.js +313 -0
- package/dist/src-CFp1QpFd.js +4064 -0
- package/dist/test-api.js +2 -0
- package/package.json +14 -6
- package/api.ts +0 -3
- package/channel-config-api.ts +0 -1
- package/channel-plugin-api.ts +0 -2
- package/config-api.ts +0 -4
- package/contract-api.ts +0 -4
- package/index.ts +0 -20
- package/runtime-api.ts +0 -73
- package/secret-contract-api.ts +0 -5
- package/setup-entry.ts +0 -13
- package/setup-plugin-api.ts +0 -3
- package/src/ai-entity.ts +0 -7
- package/src/approval-auth.ts +0 -44
- package/src/attachments/bot-framework.test.ts +0 -461
- package/src/attachments/bot-framework.ts +0 -362
- package/src/attachments/download.ts +0 -311
- package/src/attachments/graph.test.ts +0 -416
- package/src/attachments/graph.ts +0 -484
- package/src/attachments/html.ts +0 -122
- package/src/attachments/payload.ts +0 -14
- package/src/attachments/remote-media.test.ts +0 -137
- package/src/attachments/remote-media.ts +0 -112
- package/src/attachments/shared.test.ts +0 -530
- package/src/attachments/shared.ts +0 -626
- package/src/attachments/types.ts +0 -47
- package/src/attachments.graph.test.ts +0 -342
- package/src/attachments.helpers.test.ts +0 -246
- package/src/attachments.test-helpers.ts +0 -17
- package/src/attachments.test.ts +0 -687
- package/src/attachments.ts +0 -18
- package/src/block-streaming-config.test.ts +0 -61
- package/src/channel-api.ts +0 -1
- package/src/channel.actions.test.ts +0 -742
- package/src/channel.directory.test.ts +0 -200
- package/src/channel.runtime.ts +0 -56
- package/src/channel.setup.ts +0 -77
- package/src/channel.test.ts +0 -128
- package/src/channel.ts +0 -1136
- package/src/config-schema.ts +0 -6
- package/src/config-ui-hints.ts +0 -12
- package/src/conversation-store-fs.test.ts +0 -74
- package/src/conversation-store-fs.ts +0 -149
- package/src/conversation-store-helpers.test.ts +0 -202
- package/src/conversation-store-helpers.ts +0 -105
- package/src/conversation-store-memory.ts +0 -51
- package/src/conversation-store.shared.test.ts +0 -225
- package/src/conversation-store.ts +0 -71
- package/src/directory-live.test.ts +0 -156
- package/src/directory-live.ts +0 -111
- package/src/doctor.ts +0 -27
- package/src/errors.test.ts +0 -133
- package/src/errors.ts +0 -246
- package/src/feedback-reflection-prompt.ts +0 -117
- package/src/feedback-reflection-store.ts +0 -114
- package/src/feedback-reflection.test.ts +0 -237
- package/src/feedback-reflection.ts +0 -283
- package/src/file-consent-helpers.test.ts +0 -326
- package/src/file-consent-helpers.ts +0 -126
- package/src/file-consent-invoke.ts +0 -150
- package/src/file-consent.test.ts +0 -363
- package/src/file-consent.ts +0 -287
- package/src/graph-chat.ts +0 -55
- package/src/graph-group-management.test.ts +0 -318
- package/src/graph-group-management.ts +0 -168
- package/src/graph-members.test.ts +0 -89
- package/src/graph-members.ts +0 -48
- package/src/graph-messages.actions.test.ts +0 -243
- package/src/graph-messages.read.test.ts +0 -391
- package/src/graph-messages.search.test.ts +0 -213
- package/src/graph-messages.test-helpers.ts +0 -50
- package/src/graph-messages.ts +0 -534
- package/src/graph-teams.test.ts +0 -215
- package/src/graph-teams.ts +0 -114
- package/src/graph-thread.test.ts +0 -246
- package/src/graph-thread.ts +0 -146
- package/src/graph-upload.test.ts +0 -258
- package/src/graph-upload.ts +0 -531
- package/src/graph-users.ts +0 -29
- package/src/graph.test.ts +0 -516
- package/src/graph.ts +0 -293
- package/src/inbound.test.ts +0 -221
- package/src/inbound.ts +0 -148
- package/src/index.ts +0 -4
- package/src/media-helpers.test.ts +0 -202
- package/src/media-helpers.ts +0 -105
- package/src/mentions.test.ts +0 -244
- package/src/mentions.ts +0 -114
- package/src/messenger.test.ts +0 -865
- package/src/messenger.ts +0 -605
- package/src/monitor-handler/access.ts +0 -125
- package/src/monitor-handler/inbound-media.test.ts +0 -289
- package/src/monitor-handler/inbound-media.ts +0 -180
- package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
- package/src/monitor-handler/message-handler.authz.test.ts +0 -669
- package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
- package/src/monitor-handler/message-handler.test-support.ts +0 -100
- package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -223
- package/src/monitor-handler/message-handler.thread-session.test.ts +0 -77
- package/src/monitor-handler/message-handler.ts +0 -1000
- package/src/monitor-handler/reaction-handler.test.ts +0 -267
- package/src/monitor-handler/reaction-handler.ts +0 -210
- package/src/monitor-handler/thread-session.ts +0 -17
- package/src/monitor-handler.adaptive-card.test.ts +0 -162
- package/src/monitor-handler.feedback-authz.test.ts +0 -314
- package/src/monitor-handler.file-consent.test.ts +0 -423
- package/src/monitor-handler.sso.test.ts +0 -563
- package/src/monitor-handler.test-helpers.ts +0 -180
- package/src/monitor-handler.ts +0 -534
- package/src/monitor-handler.types.ts +0 -27
- package/src/monitor-types.ts +0 -6
- package/src/monitor.lifecycle.test.ts +0 -278
- package/src/monitor.test.ts +0 -119
- package/src/monitor.ts +0 -442
- package/src/oauth.flow.ts +0 -77
- package/src/oauth.shared.ts +0 -37
- package/src/oauth.test.ts +0 -305
- package/src/oauth.token.ts +0 -158
- package/src/oauth.ts +0 -130
- package/src/outbound.test.ts +0 -130
- package/src/outbound.ts +0 -71
- package/src/pending-uploads-fs.test.ts +0 -246
- package/src/pending-uploads-fs.ts +0 -235
- package/src/pending-uploads.test.ts +0 -173
- package/src/pending-uploads.ts +0 -121
- package/src/policy.test.ts +0 -240
- package/src/policy.ts +0 -262
- package/src/polls-store-memory.ts +0 -32
- package/src/polls.test.ts +0 -160
- package/src/polls.ts +0 -323
- package/src/presentation.ts +0 -68
- package/src/probe.test.ts +0 -77
- package/src/probe.ts +0 -132
- package/src/reply-dispatcher.test.ts +0 -437
- package/src/reply-dispatcher.ts +0 -346
- package/src/reply-stream-controller.test.ts +0 -235
- package/src/reply-stream-controller.ts +0 -147
- package/src/resolve-allowlist.test.ts +0 -250
- package/src/resolve-allowlist.ts +0 -309
- package/src/revoked-context.ts +0 -17
- package/src/runtime.ts +0 -9
- package/src/sdk-types.ts +0 -59
- package/src/sdk.test.ts +0 -666
- package/src/sdk.ts +0 -884
- package/src/secret-contract.ts +0 -49
- package/src/secret-input.ts +0 -7
- package/src/send-context.ts +0 -231
- package/src/send.test.ts +0 -493
- package/src/send.ts +0 -637
- package/src/sent-message-cache.test.ts +0 -15
- package/src/sent-message-cache.ts +0 -56
- package/src/session-route.ts +0 -40
- package/src/setup-core.ts +0 -160
- package/src/setup-surface.test.ts +0 -202
- package/src/setup-surface.ts +0 -320
- package/src/sso-token-store.test.ts +0 -72
- package/src/sso-token-store.ts +0 -166
- package/src/sso.ts +0 -300
- package/src/storage.ts +0 -25
- package/src/store-fs.ts +0 -44
- package/src/streaming-message.test.ts +0 -262
- package/src/streaming-message.ts +0 -297
- package/src/test-runtime.ts +0 -16
- package/src/thread-parent-context.test.ts +0 -224
- package/src/thread-parent-context.ts +0 -159
- package/src/token-response.ts +0 -11
- package/src/token.test.ts +0 -259
- package/src/token.ts +0 -195
- package/src/user-agent.test.ts +0 -86
- package/src/user-agent.ts +0 -53
- package/src/webhook-timeouts.ts +0 -27
- package/src/welcome-card.test.ts +0 -81
- package/src/welcome-card.ts +0 -57
- package/test-api.ts +0 -1
- package/tsconfig.json +0 -16
package/src/oauth.test.ts
DELETED
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
-
|
|
4
|
-
vi.mock("openclaw/plugin-sdk/ssrf-runtime", () => ({
|
|
5
|
-
fetchWithSsrFGuard: async (params: {
|
|
6
|
-
url: string;
|
|
7
|
-
init?: RequestInit;
|
|
8
|
-
fetchImpl?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
9
|
-
}) => {
|
|
10
|
-
const fetchImpl = params.fetchImpl ?? globalThis.fetch;
|
|
11
|
-
const response = await fetchImpl(params.url, params.init);
|
|
12
|
-
return {
|
|
13
|
-
response,
|
|
14
|
-
finalUrl: params.url,
|
|
15
|
-
release: async () => {},
|
|
16
|
-
};
|
|
17
|
-
},
|
|
18
|
-
}));
|
|
19
|
-
|
|
20
|
-
import {
|
|
21
|
-
generatePkce,
|
|
22
|
-
generateOAuthState,
|
|
23
|
-
buildMSTeamsAuthUrl,
|
|
24
|
-
parseCallbackInput,
|
|
25
|
-
} from "./oauth.flow.js";
|
|
26
|
-
import {
|
|
27
|
-
MSTEAMS_DEFAULT_DELEGATED_SCOPES,
|
|
28
|
-
MSTEAMS_OAUTH_REDIRECT_URI,
|
|
29
|
-
buildMSTeamsAuthEndpoint,
|
|
30
|
-
buildMSTeamsTokenEndpoint,
|
|
31
|
-
} from "./oauth.shared.js";
|
|
32
|
-
import { exchangeMSTeamsCodeForTokens, refreshMSTeamsDelegatedTokens } from "./oauth.token.js";
|
|
33
|
-
|
|
34
|
-
function responseJson(body: unknown, status = 200): Response {
|
|
35
|
-
return new Response(JSON.stringify(body), {
|
|
36
|
-
status,
|
|
37
|
-
headers: { "Content-Type": "application/json" },
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
describe("generatePkce", () => {
|
|
42
|
-
it("produces a 64-char hex verifier and a base64url SHA-256 challenge", () => {
|
|
43
|
-
const { verifier, challenge } = generatePkce();
|
|
44
|
-
expect(verifier).toMatch(/^[0-9a-f]{64}$/);
|
|
45
|
-
const expected = createHash("sha256").update(verifier).digest("base64url");
|
|
46
|
-
expect(challenge).toBe(expected);
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
describe("generateOAuthState", () => {
|
|
51
|
-
it("produces a 64-char hex string separate from the PKCE verifier", () => {
|
|
52
|
-
const state = generateOAuthState();
|
|
53
|
-
expect(state).toMatch(/^[0-9a-f]{64}$/);
|
|
54
|
-
const { verifier } = generatePkce();
|
|
55
|
-
expect(state).not.toBe(verifier);
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
describe("buildMSTeamsAuthUrl", () => {
|
|
60
|
-
it("includes correct tenant, client_id, scopes, PKCE params, and redirect_uri", () => {
|
|
61
|
-
const { challenge } = generatePkce();
|
|
62
|
-
const state = generateOAuthState();
|
|
63
|
-
const url = buildMSTeamsAuthUrl({
|
|
64
|
-
tenantId: "my-tenant-id",
|
|
65
|
-
clientId: "my-client-id",
|
|
66
|
-
challenge,
|
|
67
|
-
state,
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
const parsed = new URL(url);
|
|
71
|
-
expect(parsed.origin + parsed.pathname).toBe(buildMSTeamsAuthEndpoint("my-tenant-id"));
|
|
72
|
-
expect(parsed.searchParams.get("client_id")).toBe("my-client-id");
|
|
73
|
-
expect(parsed.searchParams.get("response_type")).toBe("code");
|
|
74
|
-
expect(parsed.searchParams.get("redirect_uri")).toBe(MSTEAMS_OAUTH_REDIRECT_URI);
|
|
75
|
-
expect(parsed.searchParams.get("scope")).toBe(MSTEAMS_DEFAULT_DELEGATED_SCOPES.join(" "));
|
|
76
|
-
expect(parsed.searchParams.get("code_challenge")).toBe(challenge);
|
|
77
|
-
expect(parsed.searchParams.get("code_challenge_method")).toBe("S256");
|
|
78
|
-
expect(parsed.searchParams.get("state")).toBe(state);
|
|
79
|
-
expect(parsed.searchParams.get("prompt")).toBe("consent");
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it("does not expose the PKCE verifier in the URL", () => {
|
|
83
|
-
const { verifier, challenge } = generatePkce();
|
|
84
|
-
const state = generateOAuthState();
|
|
85
|
-
const url = buildMSTeamsAuthUrl({
|
|
86
|
-
tenantId: "t",
|
|
87
|
-
clientId: "c",
|
|
88
|
-
challenge,
|
|
89
|
-
state,
|
|
90
|
-
});
|
|
91
|
-
expect(url).not.toContain(verifier);
|
|
92
|
-
expect(url).toContain(`state=${state}`);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("uses custom scopes when provided", () => {
|
|
96
|
-
const url = buildMSTeamsAuthUrl({
|
|
97
|
-
tenantId: "t",
|
|
98
|
-
clientId: "c",
|
|
99
|
-
challenge: "ch",
|
|
100
|
-
state: "s",
|
|
101
|
-
scopes: ["User.Read", "offline_access"],
|
|
102
|
-
});
|
|
103
|
-
const parsed = new URL(url);
|
|
104
|
-
expect(parsed.searchParams.get("scope")).toBe("User.Read offline_access");
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
describe("parseCallbackInput", () => {
|
|
109
|
-
const expectedState = "expected-state-value";
|
|
110
|
-
|
|
111
|
-
it("extracts code and state from a valid callback URL", () => {
|
|
112
|
-
const input = `${MSTEAMS_OAUTH_REDIRECT_URI}?code=abc123&state=${expectedState}`;
|
|
113
|
-
const result = parseCallbackInput(input, expectedState);
|
|
114
|
-
expect(result).toEqual({ code: "abc123", state: expectedState });
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it("returns error when code is missing from URL", () => {
|
|
118
|
-
const input = `${MSTEAMS_OAUTH_REDIRECT_URI}?state=${expectedState}`;
|
|
119
|
-
const result = parseCallbackInput(input, expectedState);
|
|
120
|
-
expect(result).toEqual({ error: "Missing 'code' parameter in URL" });
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it("rejects bare authorization codes to prevent CSRF bypass", () => {
|
|
124
|
-
const result = parseCallbackInput("bare-code-value", expectedState);
|
|
125
|
-
expect(result).toEqual({
|
|
126
|
-
error:
|
|
127
|
-
"Paste the full redirect URL (including code and state parameters), not just the authorization code.",
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it("returns error on empty input", () => {
|
|
132
|
-
const result = parseCallbackInput("", expectedState);
|
|
133
|
-
expect(result).toEqual({ error: "No input provided" });
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it("returns error when state is missing from a valid URL (CSRF protection)", () => {
|
|
137
|
-
const input = `${MSTEAMS_OAUTH_REDIRECT_URI}?code=abc123`;
|
|
138
|
-
const result = parseCallbackInput(input, expectedState);
|
|
139
|
-
expect(result).toEqual({
|
|
140
|
-
error: "Missing 'state' parameter in URL. Paste the full redirect URL.",
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it("rejects bare codes even when expectedState is empty", () => {
|
|
145
|
-
const result = parseCallbackInput("bare-code", "");
|
|
146
|
-
expect(result).toEqual({
|
|
147
|
-
error:
|
|
148
|
-
"Paste the full redirect URL (including code and state parameters), not just the authorization code.",
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
describe("exchangeMSTeamsCodeForTokens", () => {
|
|
154
|
-
let fetchSpy: ReturnType<typeof vi.fn>;
|
|
155
|
-
|
|
156
|
-
beforeEach(() => {
|
|
157
|
-
fetchSpy = vi.fn();
|
|
158
|
-
vi.stubGlobal("fetch", fetchSpy);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
afterEach(() => {
|
|
162
|
-
vi.unstubAllGlobals();
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it("exchanges an authorization code for delegated tokens", async () => {
|
|
166
|
-
const now = Date.now();
|
|
167
|
-
fetchSpy.mockResolvedValueOnce(
|
|
168
|
-
responseJson({
|
|
169
|
-
access_token: "at-123",
|
|
170
|
-
refresh_token: "rt-456",
|
|
171
|
-
expires_in: 3600,
|
|
172
|
-
scope: "ChatMessage.Send offline_access",
|
|
173
|
-
}),
|
|
174
|
-
);
|
|
175
|
-
|
|
176
|
-
const tokens = await exchangeMSTeamsCodeForTokens({
|
|
177
|
-
tenantId: "tenant-1",
|
|
178
|
-
clientId: "client-1",
|
|
179
|
-
clientSecret: "secret-1", // pragma: allowlist secret
|
|
180
|
-
code: "auth-code",
|
|
181
|
-
verifier: "pkce-verifier",
|
|
182
|
-
});
|
|
183
|
-
const afterExchange = Date.now();
|
|
184
|
-
|
|
185
|
-
expect(tokens.accessToken).toBe("at-123");
|
|
186
|
-
expect(tokens.refreshToken).toBe("rt-456");
|
|
187
|
-
expect(tokens.scopes).toEqual(["ChatMessage.Send", "offline_access"]);
|
|
188
|
-
// expiresAt should be roughly now + 3600s - 300s
|
|
189
|
-
expect(tokens.expiresAt).toBeGreaterThanOrEqual(now + 3300 * 1000 - 1000);
|
|
190
|
-
expect(tokens.expiresAt).toBeLessThanOrEqual(afterExchange + 3300 * 1000 + 2000);
|
|
191
|
-
|
|
192
|
-
// Verify the request was well-formed
|
|
193
|
-
expect(fetchSpy).toHaveBeenCalledOnce();
|
|
194
|
-
const [url, init] = fetchSpy.mock.calls[0] as [string, RequestInit];
|
|
195
|
-
expect(url).toBe(buildMSTeamsTokenEndpoint("tenant-1"));
|
|
196
|
-
const body = new URLSearchParams(init.body as string);
|
|
197
|
-
expect(body.get("client_id")).toBe("client-1");
|
|
198
|
-
expect(body.get("client_secret")).toBe("secret-1");
|
|
199
|
-
expect(body.get("grant_type")).toBe("authorization_code");
|
|
200
|
-
expect(body.get("code")).toBe("auth-code");
|
|
201
|
-
expect(body.get("code_verifier")).toBe("pkce-verifier");
|
|
202
|
-
expect(body.get("redirect_uri")).toBe(MSTEAMS_OAUTH_REDIRECT_URI);
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it("throws on a 400 error response", async () => {
|
|
206
|
-
fetchSpy.mockResolvedValueOnce(
|
|
207
|
-
new Response(JSON.stringify({ error: "invalid_grant" }), {
|
|
208
|
-
status: 400,
|
|
209
|
-
headers: { "Content-Type": "application/json" },
|
|
210
|
-
}),
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
await expect(
|
|
214
|
-
exchangeMSTeamsCodeForTokens({
|
|
215
|
-
tenantId: "t",
|
|
216
|
-
clientId: "c",
|
|
217
|
-
clientSecret: "s", // pragma: allowlist secret
|
|
218
|
-
code: "bad-code",
|
|
219
|
-
verifier: "v",
|
|
220
|
-
}),
|
|
221
|
-
).rejects.toThrow(/MSTeams token exchange failed \(400\)/);
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
describe("refreshMSTeamsDelegatedTokens", () => {
|
|
226
|
-
let fetchSpy: ReturnType<typeof vi.fn>;
|
|
227
|
-
|
|
228
|
-
beforeEach(() => {
|
|
229
|
-
fetchSpy = vi.fn();
|
|
230
|
-
vi.stubGlobal("fetch", fetchSpy);
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
afterEach(() => {
|
|
234
|
-
vi.unstubAllGlobals();
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
it("refreshes tokens using refresh_token grant and keeps old refresh token when Azure omits it", async () => {
|
|
238
|
-
const now = Date.now();
|
|
239
|
-
fetchSpy.mockResolvedValueOnce(
|
|
240
|
-
responseJson({
|
|
241
|
-
access_token: "new-at",
|
|
242
|
-
// Azure sometimes does not return a new refresh_token
|
|
243
|
-
expires_in: 3600,
|
|
244
|
-
scope: "ChatMessage.Send offline_access",
|
|
245
|
-
}),
|
|
246
|
-
);
|
|
247
|
-
|
|
248
|
-
const tokens = await refreshMSTeamsDelegatedTokens({
|
|
249
|
-
tenantId: "tenant-1",
|
|
250
|
-
clientId: "client-1",
|
|
251
|
-
clientSecret: "secret-1", // pragma: allowlist secret
|
|
252
|
-
refreshToken: "original-rt",
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
expect(tokens.accessToken).toBe("new-at");
|
|
256
|
-
// Old refresh token should be preserved
|
|
257
|
-
expect(tokens.refreshToken).toBe("original-rt");
|
|
258
|
-
expect(tokens.scopes).toEqual(["ChatMessage.Send", "offline_access"]);
|
|
259
|
-
expect(tokens.expiresAt).toBeGreaterThanOrEqual(now + 3300 * 1000 - 1000);
|
|
260
|
-
|
|
261
|
-
// Verify the request body includes refresh_token grant type
|
|
262
|
-
const [, init] = fetchSpy.mock.calls[0] as [string, RequestInit];
|
|
263
|
-
const body = new URLSearchParams(init.body as string);
|
|
264
|
-
expect(body.get("grant_type")).toBe("refresh_token");
|
|
265
|
-
expect(body.get("refresh_token")).toBe("original-rt");
|
|
266
|
-
expect(body.get("client_secret")).toBe("secret-1");
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
it("uses new refresh token when Azure returns one", async () => {
|
|
270
|
-
fetchSpy.mockResolvedValueOnce(
|
|
271
|
-
responseJson({
|
|
272
|
-
access_token: "new-at",
|
|
273
|
-
refresh_token: "new-rt",
|
|
274
|
-
expires_in: 3600,
|
|
275
|
-
}),
|
|
276
|
-
);
|
|
277
|
-
|
|
278
|
-
const tokens = await refreshMSTeamsDelegatedTokens({
|
|
279
|
-
tenantId: "t",
|
|
280
|
-
clientId: "c",
|
|
281
|
-
clientSecret: "s", // pragma: allowlist secret
|
|
282
|
-
refreshToken: "old-rt",
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
expect(tokens.refreshToken).toBe("new-rt");
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
it("throws on a 401 error response", async () => {
|
|
289
|
-
fetchSpy.mockResolvedValueOnce(
|
|
290
|
-
new Response(JSON.stringify({ error: "invalid_grant" }), {
|
|
291
|
-
status: 401,
|
|
292
|
-
headers: { "Content-Type": "application/json" },
|
|
293
|
-
}),
|
|
294
|
-
);
|
|
295
|
-
|
|
296
|
-
await expect(
|
|
297
|
-
refreshMSTeamsDelegatedTokens({
|
|
298
|
-
tenantId: "t",
|
|
299
|
-
clientId: "c",
|
|
300
|
-
clientSecret: "s", // pragma: allowlist secret
|
|
301
|
-
refreshToken: "expired-rt",
|
|
302
|
-
}),
|
|
303
|
-
).rejects.toThrow(/MSTeams token refresh failed \(401\)/);
|
|
304
|
-
});
|
|
305
|
-
});
|
package/src/oauth.token.ts
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
|
|
2
|
-
import {
|
|
3
|
-
MSTEAMS_DEFAULT_DELEGATED_SCOPES,
|
|
4
|
-
MSTEAMS_DEFAULT_TOKEN_FETCH_TIMEOUT_MS,
|
|
5
|
-
MSTEAMS_OAUTH_REDIRECT_URI,
|
|
6
|
-
buildMSTeamsTokenEndpoint,
|
|
7
|
-
type MSTeamsDelegatedTokens,
|
|
8
|
-
} from "./oauth.shared.js";
|
|
9
|
-
|
|
10
|
-
/** Five-minute buffer subtracted from token expiry to avoid edge-case clock drift. */
|
|
11
|
-
const EXPIRY_BUFFER_MS = 5 * 60 * 1000;
|
|
12
|
-
|
|
13
|
-
type MSTeamsTokenResponse = {
|
|
14
|
-
access_token: string;
|
|
15
|
-
refresh_token?: string;
|
|
16
|
-
expires_in: number;
|
|
17
|
-
scope?: string;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
function createMSTeamsTokenBody(params: {
|
|
21
|
-
clientId: string;
|
|
22
|
-
clientSecret: string;
|
|
23
|
-
grantType: string;
|
|
24
|
-
scopes: readonly string[];
|
|
25
|
-
values?: Record<string, string>;
|
|
26
|
-
}): URLSearchParams {
|
|
27
|
-
const body = new URLSearchParams({
|
|
28
|
-
client_id: params.clientId,
|
|
29
|
-
client_secret: params.clientSecret,
|
|
30
|
-
grant_type: params.grantType,
|
|
31
|
-
scope: [...params.scopes].join(" "),
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
for (const [key, value] of Object.entries(params.values ?? {})) {
|
|
35
|
-
body.set(key, value);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return body;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async function fetchMSTeamsTokens(params: {
|
|
42
|
-
tokenUrl: string;
|
|
43
|
-
body: URLSearchParams;
|
|
44
|
-
auditContext: string;
|
|
45
|
-
failureLabel: string;
|
|
46
|
-
}): Promise<MSTeamsTokenResponse> {
|
|
47
|
-
const currentFetch = globalThis.fetch;
|
|
48
|
-
const { response, release } = await fetchWithSsrFGuard({
|
|
49
|
-
url: params.tokenUrl,
|
|
50
|
-
fetchImpl: async (input, guardedInit) => await currentFetch(input, guardedInit),
|
|
51
|
-
init: {
|
|
52
|
-
method: "POST",
|
|
53
|
-
headers: {
|
|
54
|
-
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
|
55
|
-
Accept: "application/json",
|
|
56
|
-
},
|
|
57
|
-
body: params.body,
|
|
58
|
-
signal: AbortSignal.timeout(MSTEAMS_DEFAULT_TOKEN_FETCH_TIMEOUT_MS),
|
|
59
|
-
},
|
|
60
|
-
auditContext: params.auditContext,
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
try {
|
|
64
|
-
if (!response.ok) {
|
|
65
|
-
const errorText = await response.text();
|
|
66
|
-
throw new Error(`MSTeams ${params.failureLabel} failed (${response.status}): ${errorText}`);
|
|
67
|
-
}
|
|
68
|
-
return (await response.json()) as MSTeamsTokenResponse;
|
|
69
|
-
} finally {
|
|
70
|
-
await release();
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async function requestMSTeamsDelegatedTokens(params: {
|
|
75
|
-
tenantId: string;
|
|
76
|
-
clientId: string;
|
|
77
|
-
clientSecret: string;
|
|
78
|
-
scopes?: readonly string[];
|
|
79
|
-
grantType: string;
|
|
80
|
-
values: Record<string, string>;
|
|
81
|
-
auditContext: string;
|
|
82
|
-
failureLabel: string;
|
|
83
|
-
resolveRefreshToken: (data: MSTeamsTokenResponse) => string;
|
|
84
|
-
}): Promise<MSTeamsDelegatedTokens> {
|
|
85
|
-
const scopes = params.scopes ?? MSTEAMS_DEFAULT_DELEGATED_SCOPES;
|
|
86
|
-
const body = createMSTeamsTokenBody({
|
|
87
|
-
clientId: params.clientId,
|
|
88
|
-
clientSecret: params.clientSecret,
|
|
89
|
-
grantType: params.grantType,
|
|
90
|
-
scopes,
|
|
91
|
-
values: params.values,
|
|
92
|
-
});
|
|
93
|
-
const data = await fetchMSTeamsTokens({
|
|
94
|
-
tokenUrl: buildMSTeamsTokenEndpoint(params.tenantId),
|
|
95
|
-
body,
|
|
96
|
-
auditContext: params.auditContext,
|
|
97
|
-
failureLabel: params.failureLabel,
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
return {
|
|
101
|
-
accessToken: data.access_token,
|
|
102
|
-
refreshToken: params.resolveRefreshToken(data),
|
|
103
|
-
expiresAt: Date.now() + data.expires_in * 1000 - EXPIRY_BUFFER_MS,
|
|
104
|
-
scopes: data.scope ? data.scope.split(" ") : [...scopes],
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export async function exchangeMSTeamsCodeForTokens(params: {
|
|
109
|
-
tenantId: string;
|
|
110
|
-
clientId: string;
|
|
111
|
-
clientSecret: string;
|
|
112
|
-
code: string;
|
|
113
|
-
verifier: string;
|
|
114
|
-
scopes?: readonly string[];
|
|
115
|
-
}): Promise<MSTeamsDelegatedTokens> {
|
|
116
|
-
return await requestMSTeamsDelegatedTokens({
|
|
117
|
-
tenantId: params.tenantId,
|
|
118
|
-
clientId: params.clientId,
|
|
119
|
-
clientSecret: params.clientSecret,
|
|
120
|
-
grantType: "authorization_code",
|
|
121
|
-
scopes: params.scopes,
|
|
122
|
-
values: {
|
|
123
|
-
code: params.code,
|
|
124
|
-
redirect_uri: MSTEAMS_OAUTH_REDIRECT_URI,
|
|
125
|
-
code_verifier: params.verifier,
|
|
126
|
-
},
|
|
127
|
-
auditContext: "msteams-oauth-token-exchange",
|
|
128
|
-
failureLabel: "token exchange",
|
|
129
|
-
resolveRefreshToken: (data) => {
|
|
130
|
-
if (!data.refresh_token) {
|
|
131
|
-
throw new Error("No refresh token received from Azure AD. Please try again.");
|
|
132
|
-
}
|
|
133
|
-
return data.refresh_token;
|
|
134
|
-
},
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export async function refreshMSTeamsDelegatedTokens(params: {
|
|
139
|
-
tenantId: string;
|
|
140
|
-
clientId: string;
|
|
141
|
-
clientSecret: string;
|
|
142
|
-
refreshToken: string;
|
|
143
|
-
scopes?: readonly string[];
|
|
144
|
-
}): Promise<MSTeamsDelegatedTokens> {
|
|
145
|
-
return await requestMSTeamsDelegatedTokens({
|
|
146
|
-
tenantId: params.tenantId,
|
|
147
|
-
clientId: params.clientId,
|
|
148
|
-
clientSecret: params.clientSecret,
|
|
149
|
-
grantType: "refresh_token",
|
|
150
|
-
scopes: params.scopes,
|
|
151
|
-
values: {
|
|
152
|
-
refresh_token: params.refreshToken,
|
|
153
|
-
},
|
|
154
|
-
auditContext: "msteams-oauth-token-refresh",
|
|
155
|
-
failureLabel: "token refresh",
|
|
156
|
-
resolveRefreshToken: (data) => data.refresh_token ?? params.refreshToken,
|
|
157
|
-
});
|
|
158
|
-
}
|
package/src/oauth.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildMSTeamsAuthUrl,
|
|
3
|
-
generateOAuthState,
|
|
4
|
-
generatePkce,
|
|
5
|
-
parseCallbackInput,
|
|
6
|
-
shouldUseManualOAuthFlow,
|
|
7
|
-
waitForLocalCallback,
|
|
8
|
-
} from "./oauth.flow.js";
|
|
9
|
-
import {
|
|
10
|
-
MSTEAMS_DEFAULT_DELEGATED_SCOPES,
|
|
11
|
-
MSTEAMS_OAUTH_CALLBACK_PORT,
|
|
12
|
-
type MSTeamsDelegatedOAuthContext,
|
|
13
|
-
type MSTeamsDelegatedTokens,
|
|
14
|
-
} from "./oauth.shared.js";
|
|
15
|
-
import { exchangeMSTeamsCodeForTokens } from "./oauth.token.js";
|
|
16
|
-
|
|
17
|
-
export type { MSTeamsDelegatedOAuthContext, MSTeamsDelegatedTokens };
|
|
18
|
-
|
|
19
|
-
export async function loginMSTeamsDelegated(
|
|
20
|
-
ctx: MSTeamsDelegatedOAuthContext,
|
|
21
|
-
params: {
|
|
22
|
-
tenantId: string;
|
|
23
|
-
clientId: string;
|
|
24
|
-
clientSecret: string;
|
|
25
|
-
scopes?: readonly string[];
|
|
26
|
-
},
|
|
27
|
-
): Promise<MSTeamsDelegatedTokens> {
|
|
28
|
-
const scopes = params.scopes ?? MSTEAMS_DEFAULT_DELEGATED_SCOPES;
|
|
29
|
-
const needsManual = shouldUseManualOAuthFlow(ctx.isRemote);
|
|
30
|
-
|
|
31
|
-
await ctx.note(
|
|
32
|
-
needsManual
|
|
33
|
-
? [
|
|
34
|
-
"You are running in a remote/VPS environment.",
|
|
35
|
-
"A URL will be shown for you to open in your LOCAL browser.",
|
|
36
|
-
"After signing in, copy the redirect URL and paste it back here.",
|
|
37
|
-
].join("\n")
|
|
38
|
-
: [
|
|
39
|
-
"Browser will open for Microsoft authentication.",
|
|
40
|
-
`Sign in to grant delegated permissions for MSTeams.`,
|
|
41
|
-
`The callback will be captured automatically on localhost:${MSTEAMS_OAUTH_CALLBACK_PORT}.`,
|
|
42
|
-
].join("\n"),
|
|
43
|
-
"MSTeams Delegated OAuth",
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
const { verifier, challenge } = generatePkce();
|
|
47
|
-
const state = generateOAuthState();
|
|
48
|
-
const authUrl = buildMSTeamsAuthUrl({
|
|
49
|
-
tenantId: params.tenantId,
|
|
50
|
-
clientId: params.clientId,
|
|
51
|
-
challenge,
|
|
52
|
-
state,
|
|
53
|
-
scopes,
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
if (needsManual) {
|
|
57
|
-
return manualFlow(ctx, authUrl, state, verifier, params);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
ctx.progress.update("Complete sign-in in browser...");
|
|
61
|
-
try {
|
|
62
|
-
await ctx.openUrl(authUrl);
|
|
63
|
-
} catch {
|
|
64
|
-
ctx.log(`\nOpen this URL in your browser:\n\n${authUrl}\n`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
const { code } = await waitForLocalCallback({
|
|
69
|
-
expectedState: state,
|
|
70
|
-
timeoutMs: 5 * 60 * 1000,
|
|
71
|
-
onProgress: (msg) => ctx.progress.update(msg),
|
|
72
|
-
});
|
|
73
|
-
ctx.progress.update("Exchanging authorization code for tokens...");
|
|
74
|
-
return await exchangeMSTeamsCodeForTokens({
|
|
75
|
-
tenantId: params.tenantId,
|
|
76
|
-
clientId: params.clientId,
|
|
77
|
-
clientSecret: params.clientSecret,
|
|
78
|
-
code,
|
|
79
|
-
verifier,
|
|
80
|
-
scopes,
|
|
81
|
-
});
|
|
82
|
-
} catch (err) {
|
|
83
|
-
// EADDRINUSE or other listen errors: fall back to manual flow
|
|
84
|
-
if (
|
|
85
|
-
err instanceof Error &&
|
|
86
|
-
(err.message.includes("EADDRINUSE") ||
|
|
87
|
-
err.message.includes("port") ||
|
|
88
|
-
err.message.includes("listen"))
|
|
89
|
-
) {
|
|
90
|
-
ctx.progress.update("Local callback server failed. Switching to manual mode...");
|
|
91
|
-
return manualFlow(ctx, authUrl, state, verifier, params, err);
|
|
92
|
-
}
|
|
93
|
-
throw err;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async function manualFlow(
|
|
98
|
-
ctx: MSTeamsDelegatedOAuthContext,
|
|
99
|
-
authUrl: string,
|
|
100
|
-
state: string,
|
|
101
|
-
verifier: string,
|
|
102
|
-
params: {
|
|
103
|
-
tenantId: string;
|
|
104
|
-
clientId: string;
|
|
105
|
-
clientSecret: string;
|
|
106
|
-
scopes?: readonly string[];
|
|
107
|
-
},
|
|
108
|
-
cause?: Error,
|
|
109
|
-
): Promise<MSTeamsDelegatedTokens> {
|
|
110
|
-
ctx.progress.update("OAuth URL ready");
|
|
111
|
-
ctx.log(`\nOpen this URL in your LOCAL browser:\n\n${authUrl}\n`);
|
|
112
|
-
ctx.progress.update("Waiting for you to paste the callback URL...");
|
|
113
|
-
const callbackInput = await ctx.prompt("Paste the redirect URL here: ");
|
|
114
|
-
const parsed = parseCallbackInput(callbackInput, state);
|
|
115
|
-
if ("error" in parsed) {
|
|
116
|
-
throw new Error(parsed.error, cause ? { cause } : undefined);
|
|
117
|
-
}
|
|
118
|
-
if (parsed.state !== state) {
|
|
119
|
-
throw new Error("OAuth state mismatch - please try again", cause ? { cause } : undefined);
|
|
120
|
-
}
|
|
121
|
-
ctx.progress.update("Exchanging authorization code for tokens...");
|
|
122
|
-
return exchangeMSTeamsCodeForTokens({
|
|
123
|
-
tenantId: params.tenantId,
|
|
124
|
-
clientId: params.clientId,
|
|
125
|
-
clientSecret: params.clientSecret,
|
|
126
|
-
code: parsed.code,
|
|
127
|
-
verifier,
|
|
128
|
-
scopes: params.scopes,
|
|
129
|
-
});
|
|
130
|
-
}
|