@app-vitals/agent-admin 1.58.0 → 1.60.0
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/package.json +2 -2
- package/src/fixtures/slack-provisioning-http-cassette.json +31 -0
- package/src/github-enterprise-api.ts +21 -16
- package/src/github-lookup-client.ts +54 -0
- package/src/github-lookup-client.unit.test.ts +59 -0
- package/src/github-lookup-repositories.integration.test.ts +83 -0
- package/src/github-lookup-repositories.ts +18 -3
- package/src/slack-provisioning-client.integration.test.ts +88 -0
- package/src/slack-provisioning-client.ts +117 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-admin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.60.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/app-vitals/agent.git",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@anthropic-ai/sdk": "0.137.0",
|
|
45
|
-
"@app-vitals/agent-lib": "1.
|
|
45
|
+
"@app-vitals/agent-lib": "1.60.0",
|
|
46
46
|
"@hono/zod-openapi": "^1.0.0",
|
|
47
47
|
"@prisma/adapter-pg": "^7.10.0",
|
|
48
48
|
"@prisma/client": "^7.10.0",
|
|
@@ -94,5 +94,36 @@
|
|
|
94
94
|
"authTest_missing_user_id": {
|
|
95
95
|
"status": 200,
|
|
96
96
|
"body": { "ok": true, "team_id": "T0123456789" }
|
|
97
|
+
},
|
|
98
|
+
"rotateConfigToken_success": {
|
|
99
|
+
"status": 200,
|
|
100
|
+
"body": {
|
|
101
|
+
"ok": true,
|
|
102
|
+
"token": "xoxb-rotated-access-token",
|
|
103
|
+
"refresh_token": "xoxe-1-rotated-refresh-token",
|
|
104
|
+
"team_id": "T0123456789",
|
|
105
|
+
"user_id": "U0AALR8M69X",
|
|
106
|
+
"iat": 1234567890,
|
|
107
|
+
"exp": 1234571490
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
"rotateConfigToken_invalid_refresh_token": {
|
|
111
|
+
"status": 200,
|
|
112
|
+
"body": { "ok": false, "error": "invalid_refresh_token" }
|
|
113
|
+
},
|
|
114
|
+
"rotateConfigToken_token_revoked": {
|
|
115
|
+
"status": 200,
|
|
116
|
+
"body": { "ok": false, "error": "token_revoked" }
|
|
117
|
+
},
|
|
118
|
+
"rotateConfigToken_http_error": {
|
|
119
|
+
"status": 500,
|
|
120
|
+
"body": { "ok": false, "error": "internal_error" }
|
|
121
|
+
},
|
|
122
|
+
"rotateConfigToken_missing_fields": {
|
|
123
|
+
"status": 200,
|
|
124
|
+
"body": {
|
|
125
|
+
"ok": true,
|
|
126
|
+
"token": "xoxb-rotated-access-token"
|
|
127
|
+
}
|
|
97
128
|
}
|
|
98
129
|
}
|
|
@@ -498,7 +498,7 @@ export function createGithubLookupService(
|
|
|
498
498
|
> & {
|
|
499
499
|
/** Test seam: replaces the clients built from the stored Apps. */
|
|
500
500
|
clients?: {
|
|
501
|
-
installer
|
|
501
|
+
installer?: LookupInstallerPort;
|
|
502
502
|
lookup: LookupRepositoriesPort;
|
|
503
503
|
};
|
|
504
504
|
},
|
|
@@ -511,27 +511,32 @@ export function createGithubLookupService(
|
|
|
511
511
|
deps.appService.get("installer"),
|
|
512
512
|
deps.appService.get("lookup"),
|
|
513
513
|
]);
|
|
514
|
-
if (!
|
|
515
|
-
|
|
516
|
-
}
|
|
517
|
-
const [installerKey, lookupKey] = await Promise.all([
|
|
518
|
-
deps.appService.getPrivateKey("installer"),
|
|
519
|
-
deps.appService.getPrivateKey("lookup"),
|
|
520
|
-
]);
|
|
521
|
-
if (!installerKey || !lookupKey)
|
|
522
|
-
return { configured: false, repos: [] };
|
|
514
|
+
if (!lookup) return { configured: false, repos: [] };
|
|
515
|
+
const lookupKey = await deps.appService.getPrivateKey("lookup");
|
|
516
|
+
if (!lookupKey) return { configured: false, repos: [] };
|
|
523
517
|
const common = {
|
|
524
518
|
apiBase: deps.apiBase,
|
|
525
519
|
fetchFn: deps.fetchFn,
|
|
526
520
|
now: deps.now,
|
|
527
521
|
};
|
|
522
|
+
// An installer with an enterprise slug supplies the org list; without
|
|
523
|
+
// one the lookup App lists the orgs it is installed on.
|
|
524
|
+
const installerKey = installer?.enterpriseSlug
|
|
525
|
+
? await deps.appService.getPrivateKey("installer")
|
|
526
|
+
: null;
|
|
527
|
+
if (installer?.enterpriseSlug && !installerKey) {
|
|
528
|
+
return { configured: false, repos: [] };
|
|
529
|
+
}
|
|
528
530
|
clients = {
|
|
529
|
-
installer:
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
531
|
+
installer:
|
|
532
|
+
installer?.enterpriseSlug && installerKey
|
|
533
|
+
? new GithubEnterpriseClient({
|
|
534
|
+
appId: installer.appId,
|
|
535
|
+
enterprise: installer.enterpriseSlug,
|
|
536
|
+
privateKey: installerKey,
|
|
537
|
+
...common,
|
|
538
|
+
})
|
|
539
|
+
: undefined,
|
|
535
540
|
lookup: new GithubLookupClient({
|
|
536
541
|
appId: lookup.appId,
|
|
537
542
|
privateKey: lookupKey,
|
|
@@ -81,6 +81,12 @@ export interface LookupToken {
|
|
|
81
81
|
repositorySelection?: "all" | "selected";
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
export interface LookupInstallation {
|
|
85
|
+
id: number;
|
|
86
|
+
/** The account (organization) login the App is installed on. */
|
|
87
|
+
login: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
84
90
|
export interface GithubLookupClientOptions {
|
|
85
91
|
appId: string;
|
|
86
92
|
privateKey: string;
|
|
@@ -165,6 +171,49 @@ export class GithubLookupClient {
|
|
|
165
171
|
return token;
|
|
166
172
|
}
|
|
167
173
|
|
|
174
|
+
/**
|
|
175
|
+
* List the organizations the lookup App is installed on (GET
|
|
176
|
+
* /app/installations, App JWT, paginated). User-account installations are
|
|
177
|
+
* dropped: the per-org read path only works for organizations.
|
|
178
|
+
*/
|
|
179
|
+
async listInstallations(): Promise<LookupInstallation[]> {
|
|
180
|
+
const jwt = signGithubAppJwt({
|
|
181
|
+
appId: this.appId,
|
|
182
|
+
privateKey: this.privateKey,
|
|
183
|
+
now: this.now(),
|
|
184
|
+
});
|
|
185
|
+
const all: LookupInstallation[] = [];
|
|
186
|
+
for (let page = 1; page <= MAX_PAGES; page++) {
|
|
187
|
+
const res = await this.request<RawInstallation[]>(
|
|
188
|
+
"GET",
|
|
189
|
+
`/app/installations?per_page=${PER_PAGE}&page=${page}`,
|
|
190
|
+
jwt,
|
|
191
|
+
"GitHub App installations",
|
|
192
|
+
);
|
|
193
|
+
const rows = res.data;
|
|
194
|
+
if (!Array.isArray(rows)) {
|
|
195
|
+
throw new GithubEnterpriseError(
|
|
196
|
+
"unexpected",
|
|
197
|
+
"GitHub App installations: response is not an installation list",
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
for (const r of rows) {
|
|
201
|
+
if (
|
|
202
|
+
typeof r.id === "number" &&
|
|
203
|
+
r.account?.type === "Organization" &&
|
|
204
|
+
typeof r.account.login === "string"
|
|
205
|
+
) {
|
|
206
|
+
all.push({ id: r.id, login: r.account.login });
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const link = res.headers.get("link");
|
|
210
|
+
const hasNext =
|
|
211
|
+
link !== null ? /rel="next"/.test(link) : rows.length >= PER_PAGE;
|
|
212
|
+
if (!hasNext) break;
|
|
213
|
+
}
|
|
214
|
+
return all;
|
|
215
|
+
}
|
|
216
|
+
|
|
168
217
|
/** List every repository the lookup App can see on `org` (paginated). */
|
|
169
218
|
async listInstallationRepositories(org: string): Promise<LookupRepository[]> {
|
|
170
219
|
const { token } = await this.getOrgToken(org);
|
|
@@ -302,6 +351,11 @@ export class GithubLookupClient {
|
|
|
302
351
|
}
|
|
303
352
|
}
|
|
304
353
|
|
|
354
|
+
interface RawInstallation {
|
|
355
|
+
id?: number;
|
|
356
|
+
account?: { login?: string; type?: string } | null;
|
|
357
|
+
}
|
|
358
|
+
|
|
305
359
|
interface RawRepository {
|
|
306
360
|
id: number;
|
|
307
361
|
name: string;
|
|
@@ -91,3 +91,62 @@ describe("GithubLookupClient errors", () => {
|
|
|
91
91
|
throw new Error("expected rejection");
|
|
92
92
|
});
|
|
93
93
|
});
|
|
94
|
+
|
|
95
|
+
describe("GithubLookupClient.listInstallations", () => {
|
|
96
|
+
const { privateKey } = generateKeyPairSync("rsa", {
|
|
97
|
+
modulusLength: 2048,
|
|
98
|
+
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
|
99
|
+
publicKeyEncoding: { type: "spki", format: "pem" },
|
|
100
|
+
});
|
|
101
|
+
const inst = (id: number, login: string, type = "Organization") => ({
|
|
102
|
+
id,
|
|
103
|
+
account: { login, type },
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("paginates with the App JWT and keeps only organization installs", async () => {
|
|
107
|
+
const page1 = Array.from({ length: 100 }, (_, i) => inst(i, `org${i}`));
|
|
108
|
+
const seen: string[] = [];
|
|
109
|
+
const client = new GithubLookupClient({
|
|
110
|
+
appId: "555",
|
|
111
|
+
privateKey,
|
|
112
|
+
fetchFn: (async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
113
|
+
const u = new URL(input.toString());
|
|
114
|
+
expect(u.pathname).toBe("/app/installations");
|
|
115
|
+
expect(
|
|
116
|
+
String(new Headers(init?.headers).get("authorization")),
|
|
117
|
+
).toStartWith("Bearer ey");
|
|
118
|
+
seen.push(u.searchParams.get("page") ?? "");
|
|
119
|
+
return u.searchParams.get("page") === "1"
|
|
120
|
+
? Response.json(page1, {
|
|
121
|
+
headers: { link: '<https://x/?page=2>; rel="next"' },
|
|
122
|
+
})
|
|
123
|
+
: Response.json([inst(900, "last"), inst(901, "someone", "User")]);
|
|
124
|
+
}) as unknown as typeof fetch,
|
|
125
|
+
});
|
|
126
|
+
const out = await client.listInstallations();
|
|
127
|
+
expect(seen).toEqual(["1", "2"]);
|
|
128
|
+
expect(out).toHaveLength(101);
|
|
129
|
+
expect(out.at(-1)).toEqual({ id: 900, login: "last" });
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("rejects on a GitHub error", async () => {
|
|
133
|
+
const client = new GithubLookupClient({
|
|
134
|
+
appId: "555",
|
|
135
|
+
privateKey,
|
|
136
|
+
fetchFn: (async () =>
|
|
137
|
+
Response.json({ message: "boom" }, { status: 500 })) as unknown as typeof fetch,
|
|
138
|
+
});
|
|
139
|
+
await expect(client.listInstallations()).rejects.toBeInstanceOf(
|
|
140
|
+
GithubEnterpriseError,
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("rejects a non-list response", async () => {
|
|
145
|
+
const client = new GithubLookupClient({
|
|
146
|
+
appId: "555",
|
|
147
|
+
privateKey,
|
|
148
|
+
fetchFn: (async () => Response.json({})) as unknown as typeof fetch,
|
|
149
|
+
});
|
|
150
|
+
await expect(client.listInstallations()).rejects.toThrow("not an installation list");
|
|
151
|
+
});
|
|
152
|
+
});
|
|
@@ -53,6 +53,14 @@ function replay(repos: Handler) {
|
|
|
53
53
|
expires_at: "2026-11-09T13:00:00Z",
|
|
54
54
|
});
|
|
55
55
|
}
|
|
56
|
+
if (u.pathname === "/app/installations") {
|
|
57
|
+
return Response.json(
|
|
58
|
+
Object.entries(INSTALLATIONS).map(([login, id]) => ({
|
|
59
|
+
id,
|
|
60
|
+
account: { login, type: "Organization" },
|
|
61
|
+
})),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
56
64
|
if (u.pathname === "/installation/repositories") {
|
|
57
65
|
const bearer = String(new Headers(init?.headers).get("authorization"));
|
|
58
66
|
return repos(u, Number(bearer.replace(/\D/g, "")));
|
|
@@ -175,4 +183,79 @@ describe("githubLookup.listRepositories", () => {
|
|
|
175
183
|
});
|
|
176
184
|
await expect(svc.listRepositories()).rejects.toThrow("limited");
|
|
177
185
|
});
|
|
186
|
+
|
|
187
|
+
describe("without an installer", () => {
|
|
188
|
+
const lookupApp = { appId: "555", enterpriseSlug: null };
|
|
189
|
+
const stored = (installer: unknown) =>
|
|
190
|
+
({
|
|
191
|
+
get: async (kind: string) =>
|
|
192
|
+
kind === "lookup" ? lookupApp : installer,
|
|
193
|
+
getPrivateKey: async (kind: string) =>
|
|
194
|
+
kind === "lookup" || installer ? privateKey : null,
|
|
195
|
+
}) as unknown as GitHubEnterpriseAppService;
|
|
196
|
+
|
|
197
|
+
it("lists repos from every org the lookup App is installed on", async () => {
|
|
198
|
+
const svc = createGithubLookupService({
|
|
199
|
+
appService: stored(null),
|
|
200
|
+
fetchFn: replay((_u, id) =>
|
|
201
|
+
Response.json({
|
|
202
|
+
repositories:
|
|
203
|
+
id === 1 ? [repo("acme", "api", false, 10)] : [repo("beta", "web", false, 20)],
|
|
204
|
+
}),
|
|
205
|
+
),
|
|
206
|
+
now: () => NOW,
|
|
207
|
+
});
|
|
208
|
+
expect(await svc.listRepositories()).toEqual({
|
|
209
|
+
configured: true,
|
|
210
|
+
repos: [
|
|
211
|
+
{ fullName: "acme/api", archived: false },
|
|
212
|
+
{ fullName: "beta/web", archived: false },
|
|
213
|
+
],
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("lists only the selected repos of a selected-scope installation", async () => {
|
|
218
|
+
const svc = createGithubLookupService({
|
|
219
|
+
appService: stored(null),
|
|
220
|
+
fetchFn: replay((_u, id) =>
|
|
221
|
+
Response.json({
|
|
222
|
+
repositories: id === 1 ? [repo("acme", "only-this")] : [],
|
|
223
|
+
}),
|
|
224
|
+
),
|
|
225
|
+
now: () => NOW,
|
|
226
|
+
});
|
|
227
|
+
expect((await svc.listRepositories()).repos).toEqual([
|
|
228
|
+
{ fullName: "acme/only-this", archived: false },
|
|
229
|
+
]);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("rejects on a GitHub failure instead of returning an empty list", async () => {
|
|
233
|
+
const svc = createGithubLookupService({
|
|
234
|
+
appService: stored(null),
|
|
235
|
+
fetchFn: (async () =>
|
|
236
|
+
Response.json({ message: "boom" }, { status: 500 })) as unknown as typeof fetch,
|
|
237
|
+
now: () => NOW,
|
|
238
|
+
});
|
|
239
|
+
await expect(svc.listRepositories()).rejects.toBeInstanceOf(GithubEnterpriseError);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("an installer with a slug but no key stays unconfigured", async () => {
|
|
243
|
+
const svc = createGithubLookupService({
|
|
244
|
+
appService: {
|
|
245
|
+
get: async (kind: string) =>
|
|
246
|
+
kind === "lookup" ? lookupApp : { appId: "1", enterpriseSlug: "ent" },
|
|
247
|
+
getPrivateKey: async (kind: string) =>
|
|
248
|
+
kind === "lookup" ? privateKey : null,
|
|
249
|
+
} as unknown as GitHubEnterpriseAppService,
|
|
250
|
+
});
|
|
251
|
+
expect(await svc.listRepositories()).toEqual({ configured: false, repos: [] });
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it("with an installer configured the org list still comes from it", async () => {
|
|
255
|
+
const svc = service(["acme"], replay(() => Response.json({ repositories: [repo("acme", "api")] })));
|
|
256
|
+
expect((await svc.listRepositories()).repos).toEqual([
|
|
257
|
+
{ fullName: "acme/api", archived: false },
|
|
258
|
+
]);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
178
261
|
});
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* admin/src/github-lookup-repositories.ts
|
|
3
3
|
* The lookup-App repository lister exposed to extensions as
|
|
4
4
|
* AdminPlatformServices.githubLookup: every repository the lookup App can see,
|
|
5
|
-
* across the organizations the installer App can reach
|
|
5
|
+
* across the organizations the installer App can reach, or, with no
|
|
6
|
+
* installer configured, the organizations the lookup App is installed on.
|
|
6
7
|
*
|
|
7
8
|
* A GitHub failure rejects; it is never reported as an empty list. An org
|
|
8
9
|
* without the lookup App installed (not_found) is skipped. The stored-App
|
|
@@ -35,14 +36,19 @@ export interface LookupInstallerPort {
|
|
|
35
36
|
/** The lookup-side client surface the lister needs. */
|
|
36
37
|
export interface LookupRepositoriesPort {
|
|
37
38
|
listInstallationRepositories(org: string): Promise<LookupRepository[]>;
|
|
39
|
+
/** Required only when no installer is configured (the App lists its own orgs). */
|
|
40
|
+
listInstallations?(): Promise<{ login: string }[]>;
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
export async function listLookupRepositories(
|
|
41
|
-
installer: LookupInstallerPort,
|
|
44
|
+
installer: LookupInstallerPort | undefined,
|
|
42
45
|
lookup: LookupRepositoriesPort,
|
|
43
46
|
): Promise<GithubLookupRepository[]> {
|
|
44
47
|
const repos = new Map<string, GithubLookupRepository>();
|
|
45
|
-
|
|
48
|
+
const orgs = installer
|
|
49
|
+
? await installer.listInstallableOrganizations()
|
|
50
|
+
: await listOwnInstallations(lookup);
|
|
51
|
+
for (const { login } of orgs) {
|
|
46
52
|
let rows: LookupRepository[];
|
|
47
53
|
try {
|
|
48
54
|
rows = await lookup.listInstallationRepositories(login);
|
|
@@ -58,3 +64,12 @@ export async function listLookupRepositories(
|
|
|
58
64
|
}
|
|
59
65
|
return [...repos.values()];
|
|
60
66
|
}
|
|
67
|
+
|
|
68
|
+
async function listOwnInstallations(
|
|
69
|
+
lookup: LookupRepositoriesPort,
|
|
70
|
+
): Promise<{ login: string }[]> {
|
|
71
|
+
if (!lookup.listInstallations) {
|
|
72
|
+
throw new Error("lookup client cannot list its own installations");
|
|
73
|
+
}
|
|
74
|
+
return lookup.listInstallations();
|
|
75
|
+
}
|
|
@@ -295,6 +295,94 @@ describe("HttpSlackProvisioningClient — authTest", () => {
|
|
|
295
295
|
});
|
|
296
296
|
});
|
|
297
297
|
|
|
298
|
+
// ─── rotateConfigToken ──────────────────────────────────────────────────────
|
|
299
|
+
|
|
300
|
+
describe("HttpSlackProvisioningClient — rotateConfigToken", () => {
|
|
301
|
+
it("POSTs form-encoded refresh_token to tooling.tokens.rotate and returns rotated tokens", async () => {
|
|
302
|
+
const { client, lastRequest } = makeClient("rotateConfigToken_success");
|
|
303
|
+
const result = await client.rotateConfigToken("xoxe-1-test-refresh-token");
|
|
304
|
+
|
|
305
|
+
const req = lastRequest();
|
|
306
|
+
expect(req.method).toBe("POST");
|
|
307
|
+
expect(req.url).toBe("https://slack.com/api/tooling.tokens.rotate");
|
|
308
|
+
expect(req.headers.get("content-type")).toBe(
|
|
309
|
+
"application/x-www-form-urlencoded",
|
|
310
|
+
);
|
|
311
|
+
const params = new URLSearchParams(req.body);
|
|
312
|
+
expect(params.get("refresh_token")).toBe("xoxe-1-test-refresh-token");
|
|
313
|
+
|
|
314
|
+
expect(result).toEqual({
|
|
315
|
+
accessToken: "xoxb-rotated-access-token",
|
|
316
|
+
refreshToken: "xoxe-1-rotated-refresh-token",
|
|
317
|
+
teamId: "T0123456789",
|
|
318
|
+
userId: "U0AALR8M69X",
|
|
319
|
+
iat: 1234567890,
|
|
320
|
+
exp: 1234571490,
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it("throws SlackConfigTokenError with invalid_refresh_token code when Slack returns that error", async () => {
|
|
325
|
+
const { client } = makeClient("rotateConfigToken_invalid_refresh_token");
|
|
326
|
+
try {
|
|
327
|
+
await client.rotateConfigToken("xoxe-1-test-refresh-token");
|
|
328
|
+
expect.unreachable("Should have thrown");
|
|
329
|
+
} catch (err: unknown) {
|
|
330
|
+
expect(err instanceof Error).toBe(true);
|
|
331
|
+
const error = err as Error;
|
|
332
|
+
expect(error.message).not.toContain("xoxe-1-test-refresh-token");
|
|
333
|
+
if (err && typeof err === "object" && "code" in err) {
|
|
334
|
+
expect((err as any).code).toBe("invalid_refresh_token");
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it("throws SlackConfigTokenError with token_revoked code when Slack returns that error", async () => {
|
|
340
|
+
const { client } = makeClient("rotateConfigToken_token_revoked");
|
|
341
|
+
try {
|
|
342
|
+
await client.rotateConfigToken("xoxe-1-test-refresh-token");
|
|
343
|
+
expect.unreachable("Should have thrown");
|
|
344
|
+
} catch (err: unknown) {
|
|
345
|
+
expect(err instanceof Error).toBe(true);
|
|
346
|
+
const error = err as Error;
|
|
347
|
+
expect(error.message).not.toContain("xoxe-1-test-refresh-token");
|
|
348
|
+
if (err && typeof err === "object" && "code" in err) {
|
|
349
|
+
expect((err as any).code).toBe("token_revoked");
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it("throws SlackConfigTokenError with http_error code on HTTP error", async () => {
|
|
355
|
+
const { client } = makeClient("rotateConfigToken_http_error");
|
|
356
|
+
try {
|
|
357
|
+
await client.rotateConfigToken("xoxe-1-test-refresh-token");
|
|
358
|
+
expect.unreachable("Should have thrown");
|
|
359
|
+
} catch (err: unknown) {
|
|
360
|
+
expect(err instanceof Error).toBe(true);
|
|
361
|
+
const error = err as Error;
|
|
362
|
+
expect(error.message).toMatch(/Slack tooling\.tokens\.rotate HTTP error: 500/);
|
|
363
|
+
if (err && typeof err === "object" && "code" in err) {
|
|
364
|
+
expect((err as any).code).toBe("http_error");
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("throws SlackConfigTokenError when response is missing required fields", async () => {
|
|
370
|
+
const { client } = makeClient("rotateConfigToken_missing_fields");
|
|
371
|
+
try {
|
|
372
|
+
await client.rotateConfigToken("xoxe-1-test-refresh-token");
|
|
373
|
+
expect.unreachable("Should have thrown");
|
|
374
|
+
} catch (err: unknown) {
|
|
375
|
+
expect(err instanceof Error).toBe(true);
|
|
376
|
+
const error = err as Error;
|
|
377
|
+
expect(error.message).toContain("response missing required fields");
|
|
378
|
+
expect(error.message).not.toContain("xoxe-1-test-refresh-token");
|
|
379
|
+
if (err && typeof err === "object" && "code" in err) {
|
|
380
|
+
expect((err as any).code).toBe("missing_response_fields");
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
|
|
298
386
|
// ─── Default construction (no opts) ─────────────────────────────────────────
|
|
299
387
|
|
|
300
388
|
describe("HttpSlackProvisioningClient — default construction", () => {
|
|
@@ -9,6 +9,23 @@
|
|
|
9
9
|
* directly in the response — no separate oauth.v2.access exchange needed.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
// ─── Errors ───────────────────────────────────────────────────────────────────
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Typed error for Slack config token rotation failures.
|
|
16
|
+
* Errors include Slack API errors and HTTP errors.
|
|
17
|
+
* Error messages never contain token values.
|
|
18
|
+
*/
|
|
19
|
+
export class SlackConfigTokenError extends Error {
|
|
20
|
+
constructor(
|
|
21
|
+
public readonly code: string,
|
|
22
|
+
message: string,
|
|
23
|
+
) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.name = "SlackConfigTokenError";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
12
29
|
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
13
30
|
|
|
14
31
|
interface AppManifestDisplayInfo {
|
|
@@ -112,6 +129,31 @@ export interface SlackProvisioningClient {
|
|
|
112
129
|
authTest(botToken: string): Promise<{ userId: string }>;
|
|
113
130
|
}
|
|
114
131
|
|
|
132
|
+
// ─── SlackConfigTokenClient ───────────────────────────────────────────────────
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Client for Slack config token rotation operations.
|
|
136
|
+
* Used to refresh expired or rotating OAuth tokens via tooling.tokens.rotate.
|
|
137
|
+
*/
|
|
138
|
+
export interface SlackConfigTokenClient {
|
|
139
|
+
/**
|
|
140
|
+
* Call tooling.tokens.rotate with a refresh token to get a new access token.
|
|
141
|
+
* Returns the new access token, refresh token, team ID, user ID, and timestamps.
|
|
142
|
+
* Throws SlackConfigTokenError on Slack API errors (invalid_refresh_token, token_revoked, etc.)
|
|
143
|
+
* or on HTTP errors.
|
|
144
|
+
*/
|
|
145
|
+
rotateConfigToken(
|
|
146
|
+
refreshToken: string,
|
|
147
|
+
): Promise<{
|
|
148
|
+
accessToken: string;
|
|
149
|
+
refreshToken: string;
|
|
150
|
+
teamId: string;
|
|
151
|
+
userId: string;
|
|
152
|
+
iat: number;
|
|
153
|
+
exp: number;
|
|
154
|
+
}>;
|
|
155
|
+
}
|
|
156
|
+
|
|
115
157
|
// ─── Agent manifest ───────────────────────────────────────────────────────────
|
|
116
158
|
|
|
117
159
|
/**
|
|
@@ -199,7 +241,9 @@ export function buildAgentManifest(
|
|
|
199
241
|
*
|
|
200
242
|
* The constructor takes optional Slack API URL overrides for testing/staging.
|
|
201
243
|
*/
|
|
202
|
-
export class HttpSlackProvisioningClient
|
|
244
|
+
export class HttpSlackProvisioningClient
|
|
245
|
+
implements SlackProvisioningClient, SlackConfigTokenClient
|
|
246
|
+
{
|
|
203
247
|
private readonly apiBase: string;
|
|
204
248
|
private readonly fetchFn: typeof fetch;
|
|
205
249
|
|
|
@@ -408,4 +452,76 @@ export class HttpSlackProvisioningClient implements SlackProvisioningClient {
|
|
|
408
452
|
signingSecret: data.credentials.signing_secret,
|
|
409
453
|
};
|
|
410
454
|
}
|
|
455
|
+
|
|
456
|
+
async rotateConfigToken(
|
|
457
|
+
refreshToken: string,
|
|
458
|
+
): Promise<{
|
|
459
|
+
accessToken: string;
|
|
460
|
+
refreshToken: string;
|
|
461
|
+
teamId: string;
|
|
462
|
+
userId: string;
|
|
463
|
+
iat: number;
|
|
464
|
+
exp: number;
|
|
465
|
+
}> {
|
|
466
|
+
const url = `${this.apiBase}/tooling.tokens.rotate`;
|
|
467
|
+
const params = new URLSearchParams({
|
|
468
|
+
refresh_token: refreshToken,
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
const resp = await this.fetchFn(url, {
|
|
472
|
+
method: "POST",
|
|
473
|
+
headers: {
|
|
474
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
475
|
+
},
|
|
476
|
+
body: params.toString(),
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
if (!resp.ok) {
|
|
480
|
+
throw new SlackConfigTokenError(
|
|
481
|
+
"http_error",
|
|
482
|
+
`Slack tooling.tokens.rotate HTTP error: ${resp.status} ${resp.statusText}`,
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const data = (await resp.json()) as {
|
|
487
|
+
ok: boolean;
|
|
488
|
+
error?: string;
|
|
489
|
+
token?: string;
|
|
490
|
+
refresh_token?: string;
|
|
491
|
+
team_id?: string;
|
|
492
|
+
user_id?: string;
|
|
493
|
+
iat?: number;
|
|
494
|
+
exp?: number;
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
if (!data.ok) {
|
|
498
|
+
throw new SlackConfigTokenError(
|
|
499
|
+
data.error ?? "unknown_error",
|
|
500
|
+
`Slack tooling.tokens.rotate failed: ${data.error}`,
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
if (
|
|
505
|
+
!data.token ||
|
|
506
|
+
!data.refresh_token ||
|
|
507
|
+
!data.team_id ||
|
|
508
|
+
!data.user_id ||
|
|
509
|
+
data.iat === undefined ||
|
|
510
|
+
data.exp === undefined
|
|
511
|
+
) {
|
|
512
|
+
throw new SlackConfigTokenError(
|
|
513
|
+
"missing_response_fields",
|
|
514
|
+
"Slack tooling.tokens.rotate response missing required fields",
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return {
|
|
519
|
+
accessToken: data.token,
|
|
520
|
+
refreshToken: data.refresh_token,
|
|
521
|
+
teamId: data.team_id,
|
|
522
|
+
userId: data.user_id,
|
|
523
|
+
iat: data.iat,
|
|
524
|
+
exp: data.exp,
|
|
525
|
+
};
|
|
526
|
+
}
|
|
411
527
|
}
|