@app-vitals/agent-admin 1.61.0 → 1.63.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/prisma/client/browser.ts +5 -0
- package/prisma/client/client.ts +5 -0
- package/prisma/client/internal/class.ts +14 -4
- package/prisma/client/internal/prismaNamespace.ts +94 -1
- package/prisma/client/internal/prismaNamespaceBrowser.ts +18 -0
- package/prisma/client/models/Account.ts +164 -0
- package/prisma/client/models/SlackConfigToken.ts +1574 -0
- package/prisma/client/models.ts +1 -0
- package/prisma/migrations/20261115000000_add_slack_config_token/migration.sql +34 -0
- package/prisma/schema/schema.prisma +33 -1
- package/src/slack-config-token.integration.test.ts +605 -0
- package/src/slack-config-token.ts +433 -0
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/slack-config-token.ts
|
|
3
|
+
* SlackConfigTokenService — storage for the Slack app-configuration token
|
|
4
|
+
* (refresh token plus short-lived access token), scoped like AgentTemplate:
|
|
5
|
+
* accountId null = deployment-wide default, an account row overrides it.
|
|
6
|
+
*
|
|
7
|
+
* Tokens are encrypted on write (AES-256-GCM via a strict TokenCrypto) and are
|
|
8
|
+
* never returned by any read path: reads return metadata plus `*Set` flags.
|
|
9
|
+
* save() refuses when no encryption key is configured (no plain-text fallback).
|
|
10
|
+
*
|
|
11
|
+
* getAccessToken() is the only path that decrypts. It serves the cached access
|
|
12
|
+
* token while it has more than 10 minutes left; otherwise it rotates via
|
|
13
|
+
* tooling.tokens.rotate under a row lock (SELECT ... FOR UPDATE) so replicas
|
|
14
|
+
* serialize, persists the new pair, and returns the access token only after
|
|
15
|
+
* that write commits. Slack rotation revokes the access token issued with the
|
|
16
|
+
* refresh token used, so a second rotate would break the first caller's token;
|
|
17
|
+
* the lock plus the cache re-check prevents that.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
21
|
+
import { type Clock, SystemClock } from "./clock.ts";
|
|
22
|
+
import {
|
|
23
|
+
BadGatewayError,
|
|
24
|
+
NotFoundError,
|
|
25
|
+
UnprocessableEntityError,
|
|
26
|
+
} from "./errors.ts";
|
|
27
|
+
import type { SlackConfigTokenClient } from "./slack-provisioning-client.ts";
|
|
28
|
+
import type { TokenCrypto } from "./token-crypto.ts";
|
|
29
|
+
|
|
30
|
+
/** A cached access token is reused only while it has more than this left. */
|
|
31
|
+
export const SLACK_CONFIG_ACCESS_TOKEN_MIN_REMAINING_MS = 10 * 60 * 1000;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Interactive transaction timeout for a rotation. The row lock is held across
|
|
35
|
+
* the Slack call, so this must exceed it; waiters queue on the lock inside
|
|
36
|
+
* their own transaction, so it also bounds how long they wait.
|
|
37
|
+
*/
|
|
38
|
+
const ROTATE_TX_TIMEOUT_MS = 30_000;
|
|
39
|
+
const ROTATE_TX_MAX_WAIT_MS = 10_000;
|
|
40
|
+
|
|
41
|
+
export interface GetSlackConfigAccessTokenOptions {
|
|
42
|
+
/**
|
|
43
|
+
* Rotate even when the cached access token is still valid. Used for the one
|
|
44
|
+
* retry after Slack rejected the token (token_revoked / invalid_auth).
|
|
45
|
+
*/
|
|
46
|
+
forceRotate?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* With forceRotate: the access token Slack rejected. If, once the row lock is
|
|
49
|
+
* held, the stored access token is a different still-valid token, another
|
|
50
|
+
* caller already rotated past the rejected one and that token is returned
|
|
51
|
+
* without a second rotate (which would revoke it). Without this, forceRotate
|
|
52
|
+
* always rotates.
|
|
53
|
+
*/
|
|
54
|
+
rejectedAccessToken?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface SlackConfigTokenMetadata {
|
|
58
|
+
id: string;
|
|
59
|
+
/** null = deployment-wide default. */
|
|
60
|
+
accountId: string | null;
|
|
61
|
+
refreshTokenSet: boolean;
|
|
62
|
+
accessTokenSet: boolean;
|
|
63
|
+
accessTokenExpiresAt: Date | null;
|
|
64
|
+
slackTeamId: string | null;
|
|
65
|
+
slackUserId: string | null;
|
|
66
|
+
lastRotatedAt: Date | null;
|
|
67
|
+
lastError: string | null;
|
|
68
|
+
createdAt: Date;
|
|
69
|
+
updatedAt: Date;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface SaveSlackConfigTokenInput {
|
|
73
|
+
/** Encrypted before it is stored. */
|
|
74
|
+
refreshToken: string;
|
|
75
|
+
/** Encrypted before it is stored. */
|
|
76
|
+
accessToken?: string | null;
|
|
77
|
+
accessTokenExpiresAt?: Date | null;
|
|
78
|
+
slackTeamId?: string | null;
|
|
79
|
+
slackUserId?: string | null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
interface SlackConfigTokenRow {
|
|
83
|
+
id: string;
|
|
84
|
+
accountId: string | null;
|
|
85
|
+
refreshTokenEncrypted: string;
|
|
86
|
+
accessTokenEncrypted: string | null;
|
|
87
|
+
accessTokenExpiresAt: Date | null;
|
|
88
|
+
slackTeamId: string | null;
|
|
89
|
+
slackUserId: string | null;
|
|
90
|
+
lastRotatedAt: Date | null;
|
|
91
|
+
lastError: string | null;
|
|
92
|
+
createdAt: Date;
|
|
93
|
+
updatedAt: Date;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export class SlackConfigTokenEncryptionUnavailableError extends UnprocessableEntityError {
|
|
97
|
+
constructor() {
|
|
98
|
+
super(
|
|
99
|
+
"SHIPWRIGHT_ENCRYPTION_KEY is not configured; refusing to store a Slack config token unencrypted. Set it to a 64-character hex string (openssl rand -hex 32) and retry.",
|
|
100
|
+
);
|
|
101
|
+
this.name = "SlackConfigTokenEncryptionUnavailableError";
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Serializer: metadata plus set flags. Never includes token material. */
|
|
106
|
+
export function toSlackConfigTokenMetadata(
|
|
107
|
+
row: SlackConfigTokenRow,
|
|
108
|
+
): SlackConfigTokenMetadata {
|
|
109
|
+
return {
|
|
110
|
+
id: row.id,
|
|
111
|
+
accountId: row.accountId,
|
|
112
|
+
refreshTokenSet: row.refreshTokenEncrypted.length > 0,
|
|
113
|
+
accessTokenSet: !!row.accessTokenEncrypted,
|
|
114
|
+
accessTokenExpiresAt: row.accessTokenExpiresAt,
|
|
115
|
+
slackTeamId: row.slackTeamId,
|
|
116
|
+
slackUserId: row.slackUserId,
|
|
117
|
+
lastRotatedAt: row.lastRotatedAt,
|
|
118
|
+
lastError: row.lastError,
|
|
119
|
+
createdAt: row.createdAt,
|
|
120
|
+
updatedAt: row.updatedAt,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** No SlackConfigToken row exists for the requested scope. */
|
|
125
|
+
export class SlackConfigTokenNotConfiguredError extends NotFoundError {
|
|
126
|
+
constructor(accountId: string | null) {
|
|
127
|
+
super(
|
|
128
|
+
accountId === null
|
|
129
|
+
? "No deployment-wide Slack config token is configured"
|
|
130
|
+
: `No Slack config token is configured for account ${accountId}`,
|
|
131
|
+
);
|
|
132
|
+
this.name = "SlackConfigTokenNotConfiguredError";
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Slack rejected the stored refresh token; a human must regenerate it. */
|
|
137
|
+
export class SlackConfigRefreshTokenInvalidError extends UnprocessableEntityError {
|
|
138
|
+
constructor() {
|
|
139
|
+
super(
|
|
140
|
+
"The stored Slack config refresh token is no longer valid (invalid_refresh_token). Regenerate the App Configuration token in Slack (api.slack.com/apps, Your App Configuration Tokens) and save the new refresh token in agent admin settings.",
|
|
141
|
+
);
|
|
142
|
+
this.name = "SlackConfigRefreshTokenInvalidError";
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** tooling.tokens.rotate failed for any other reason. Carries the Slack/HTTP code only. */
|
|
147
|
+
export class SlackConfigTokenRotateFailedError extends BadGatewayError {
|
|
148
|
+
constructor(public readonly code: string) {
|
|
149
|
+
super(`Slack config token rotation failed: ${code}`);
|
|
150
|
+
this.name = "SlackConfigTokenRotateFailedError";
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const REVOKED_CODES = new Set(["token_revoked", "invalid_auth"]);
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* True when `err` is Slack rejecting an access token (token_revoked or
|
|
158
|
+
* invalid_auth). Matches a `.code` (SlackConfigTokenError) and the plain
|
|
159
|
+
* `Error("Slack <method> failed: <code>")` that HttpSlackProvisioningClient's
|
|
160
|
+
* manifest calls throw.
|
|
161
|
+
*/
|
|
162
|
+
export function isSlackConfigTokenRevokedError(err: unknown): boolean {
|
|
163
|
+
if (typeof err !== "object" || err === null) return false;
|
|
164
|
+
const code = (err as { code?: unknown }).code;
|
|
165
|
+
if (typeof code === "string" && REVOKED_CODES.has(code)) return true;
|
|
166
|
+
const message = (err as { message?: unknown }).message;
|
|
167
|
+
if (typeof message !== "string") return false;
|
|
168
|
+
const m = /\bfailed: ([a-z_]+)\s*$/.exec(message);
|
|
169
|
+
return !!m && REVOKED_CODES.has(m[1] as string);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function rotateErrorCode(err: unknown): string {
|
|
173
|
+
const code = (err as { code?: unknown } | null)?.code;
|
|
174
|
+
// Codes come from Slack's `error` field or the client's own constants; keep
|
|
175
|
+
// only a safe identifier so lastError can never carry token material.
|
|
176
|
+
return typeof code === "string" && /^[a-z0-9_]{1,64}$/i.test(code)
|
|
177
|
+
? code
|
|
178
|
+
: "unknown_error";
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
type RotateOutcome =
|
|
182
|
+
| { kind: "token"; accessToken: string }
|
|
183
|
+
| { kind: "rotateFailed"; code: string };
|
|
184
|
+
|
|
185
|
+
function isUniqueViolation(err: unknown): boolean {
|
|
186
|
+
return (
|
|
187
|
+
typeof err === "object" &&
|
|
188
|
+
err !== null &&
|
|
189
|
+
(err as { code?: unknown }).code === "P2002"
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export class SlackConfigTokenService {
|
|
194
|
+
constructor(
|
|
195
|
+
private prisma: PrismaClient,
|
|
196
|
+
/** Strict crypto (see makeStrictTokenCrypto); null when no encryption key is configured. */
|
|
197
|
+
private crypto: TokenCrypto | null,
|
|
198
|
+
private clock: Clock = SystemClock(),
|
|
199
|
+
/** tooling.tokens.rotate client; required by getAccessToken() only. */
|
|
200
|
+
private rotateClient?: SlackConfigTokenClient,
|
|
201
|
+
) {}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Create or replace the token for a scope (accountId null = deployment-wide
|
|
205
|
+
* default). Resets lastError; stamps lastRotatedAt.
|
|
206
|
+
*/
|
|
207
|
+
async save(
|
|
208
|
+
accountId: string | null,
|
|
209
|
+
input: SaveSlackConfigTokenInput,
|
|
210
|
+
): Promise<SlackConfigTokenMetadata> {
|
|
211
|
+
if (!this.crypto) throw new SlackConfigTokenEncryptionUnavailableError();
|
|
212
|
+
if (!input.refreshToken) {
|
|
213
|
+
throw new UnprocessableEntityError("refreshToken is required");
|
|
214
|
+
}
|
|
215
|
+
const now = this.clock.now();
|
|
216
|
+
const data = {
|
|
217
|
+
refreshTokenEncrypted: this.crypto.encrypt(input.refreshToken),
|
|
218
|
+
accessTokenEncrypted: input.accessToken
|
|
219
|
+
? this.crypto.encrypt(input.accessToken)
|
|
220
|
+
: null,
|
|
221
|
+
accessTokenExpiresAt: input.accessTokenExpiresAt ?? null,
|
|
222
|
+
slackTeamId: input.slackTeamId ?? null,
|
|
223
|
+
slackUserId: input.slackUserId ?? null,
|
|
224
|
+
lastRotatedAt: now,
|
|
225
|
+
lastError: null,
|
|
226
|
+
updatedAt: now,
|
|
227
|
+
};
|
|
228
|
+
// Prisma has no upsert for a nullable-scoped partial unique index, so
|
|
229
|
+
// update-then-create; a concurrent create loses on the index and retries
|
|
230
|
+
// as an update.
|
|
231
|
+
const update = async () => {
|
|
232
|
+
const existing = await this.prisma.slackConfigToken.findFirst({
|
|
233
|
+
where: { accountId },
|
|
234
|
+
});
|
|
235
|
+
if (!existing) return null;
|
|
236
|
+
return this.prisma.slackConfigToken.update({
|
|
237
|
+
where: { id: existing.id },
|
|
238
|
+
data,
|
|
239
|
+
});
|
|
240
|
+
};
|
|
241
|
+
let row = await update();
|
|
242
|
+
if (!row) {
|
|
243
|
+
try {
|
|
244
|
+
row = await this.prisma.slackConfigToken.create({
|
|
245
|
+
data: { accountId, ...data },
|
|
246
|
+
});
|
|
247
|
+
} catch (err) {
|
|
248
|
+
if (!isUniqueViolation(err)) throw err;
|
|
249
|
+
row = await update();
|
|
250
|
+
if (!row) throw err;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return toSlackConfigTokenMetadata(row);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Metadata for exactly this scope (no fallback), or null. */
|
|
257
|
+
async get(
|
|
258
|
+
accountId: string | null,
|
|
259
|
+
): Promise<SlackConfigTokenMetadata | null> {
|
|
260
|
+
const row = await this.prisma.slackConfigToken.findFirst({
|
|
261
|
+
where: { accountId },
|
|
262
|
+
});
|
|
263
|
+
return row ? toSlackConfigTokenMetadata(row) : null;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Account row, else the deployment-wide default row, else null. */
|
|
267
|
+
async resolveForAccount(
|
|
268
|
+
accountId: string,
|
|
269
|
+
): Promise<SlackConfigTokenMetadata | null> {
|
|
270
|
+
return (await this.get(accountId)) ?? (await this.get(null));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async list(): Promise<SlackConfigTokenMetadata[]> {
|
|
274
|
+
const rows = await this.prisma.slackConfigToken.findMany({
|
|
275
|
+
orderBy: { createdAt: "asc" },
|
|
276
|
+
});
|
|
277
|
+
return rows.map(toSlackConfigTokenMetadata);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Delete the token for a scope. No-ops if absent. */
|
|
281
|
+
async clear(accountId: string | null): Promise<void> {
|
|
282
|
+
await this.prisma.slackConfigToken.deleteMany({ where: { accountId } });
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Server-side only: a usable Slack config access token for exactly this
|
|
287
|
+
* scope (no fallback to the default; callers resolve the scope first, e.g.
|
|
288
|
+
* via resolveForAccount). Never logged; errors never contain token values.
|
|
289
|
+
*/
|
|
290
|
+
async getAccessToken(
|
|
291
|
+
accountId: string | null,
|
|
292
|
+
opts: GetSlackConfigAccessTokenOptions = {},
|
|
293
|
+
): Promise<string> {
|
|
294
|
+
const crypto = this.crypto;
|
|
295
|
+
if (!crypto) throw new SlackConfigTokenEncryptionUnavailableError();
|
|
296
|
+
const rotateClient = this.rotateClient;
|
|
297
|
+
if (!rotateClient) {
|
|
298
|
+
throw new Error(
|
|
299
|
+
"SlackConfigTokenService was constructed without a SlackConfigTokenClient; getAccessToken() cannot rotate",
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const forceRotate = opts.forceRotate === true;
|
|
304
|
+
if (!forceRotate) {
|
|
305
|
+
const row = await this.prisma.slackConfigToken.findFirst({
|
|
306
|
+
where: { accountId },
|
|
307
|
+
});
|
|
308
|
+
if (!row) throw new SlackConfigTokenNotConfiguredError(accountId);
|
|
309
|
+
const cached = this.cachedAccessToken(row, crypto);
|
|
310
|
+
if (cached) return cached;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const outcome = await this.prisma.$transaction(
|
|
314
|
+
async (tx): Promise<RotateOutcome> => {
|
|
315
|
+
const locked =
|
|
316
|
+
accountId === null
|
|
317
|
+
? await tx.$queryRaw<
|
|
318
|
+
{ id: string }[]
|
|
319
|
+
>`SELECT id FROM "SlackConfigToken" WHERE "accountId" IS NULL FOR UPDATE`
|
|
320
|
+
: await tx.$queryRaw<
|
|
321
|
+
{ id: string }[]
|
|
322
|
+
>`SELECT id FROM "SlackConfigToken" WHERE "accountId" = ${accountId} FOR UPDATE`;
|
|
323
|
+
const id = locked[0]?.id;
|
|
324
|
+
if (!id) throw new SlackConfigTokenNotConfiguredError(accountId);
|
|
325
|
+
const row = await tx.slackConfigToken.findUniqueOrThrow({
|
|
326
|
+
where: { id },
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
// Re-check under the lock: another caller (or replica) may have
|
|
330
|
+
// rotated while we waited. A forced rotate reuses that token only if
|
|
331
|
+
// it is not the one Slack just rejected.
|
|
332
|
+
const cached = this.cachedAccessToken(row, crypto);
|
|
333
|
+
if (cached) {
|
|
334
|
+
if (!forceRotate) return { kind: "token", accessToken: cached };
|
|
335
|
+
if (
|
|
336
|
+
opts.rejectedAccessToken !== undefined &&
|
|
337
|
+
cached !== opts.rejectedAccessToken
|
|
338
|
+
) {
|
|
339
|
+
return { kind: "token", accessToken: cached };
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
let rotated: Awaited<
|
|
344
|
+
ReturnType<SlackConfigTokenClient["rotateConfigToken"]>
|
|
345
|
+
>;
|
|
346
|
+
try {
|
|
347
|
+
rotated = await rotateClient.rotateConfigToken(
|
|
348
|
+
crypto.decrypt(row.refreshTokenEncrypted),
|
|
349
|
+
);
|
|
350
|
+
} catch (err) {
|
|
351
|
+
// Record the failure and commit it; throwing here would roll the
|
|
352
|
+
// lastError write back. Token columns are left untouched.
|
|
353
|
+
const code = rotateErrorCode(err);
|
|
354
|
+
await tx.slackConfigToken.update({
|
|
355
|
+
where: { id },
|
|
356
|
+
data: { lastError: code, updatedAt: this.clock.now() },
|
|
357
|
+
});
|
|
358
|
+
return { kind: "rotateFailed", code };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Persist before returning: if this write (or the commit) fails, the
|
|
362
|
+
// caller gets an error, never an access token we failed to store.
|
|
363
|
+
// Slack refresh tokens are not single-use, so the still-stored old
|
|
364
|
+
// refresh token keeps working for the next attempt.
|
|
365
|
+
const now = this.clock.now();
|
|
366
|
+
try {
|
|
367
|
+
await tx.slackConfigToken.update({
|
|
368
|
+
where: { id },
|
|
369
|
+
data: {
|
|
370
|
+
refreshTokenEncrypted: crypto.encrypt(rotated.refreshToken),
|
|
371
|
+
accessTokenEncrypted: crypto.encrypt(rotated.accessToken),
|
|
372
|
+
accessTokenExpiresAt: new Date(rotated.exp * 1000),
|
|
373
|
+
slackTeamId: rotated.teamId,
|
|
374
|
+
slackUserId: rotated.userId,
|
|
375
|
+
lastRotatedAt: now,
|
|
376
|
+
lastError: null,
|
|
377
|
+
updatedAt: now,
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
} catch (err) {
|
|
381
|
+
throw new Error(
|
|
382
|
+
"Failed to persist the rotated Slack config token; the access token was discarded",
|
|
383
|
+
{ cause: err },
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
return { kind: "token", accessToken: rotated.accessToken };
|
|
387
|
+
},
|
|
388
|
+
{ timeout: ROTATE_TX_TIMEOUT_MS, maxWait: ROTATE_TX_MAX_WAIT_MS },
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
if (outcome.kind === "rotateFailed") {
|
|
392
|
+
if (outcome.code === "invalid_refresh_token") {
|
|
393
|
+
throw new SlackConfigRefreshTokenInvalidError();
|
|
394
|
+
}
|
|
395
|
+
throw new SlackConfigTokenRotateFailedError(outcome.code);
|
|
396
|
+
}
|
|
397
|
+
return outcome.accessToken;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Run `fn` with this scope's access token. If Slack rejects the token
|
|
402
|
+
* (token_revoked / invalid_auth), force exactly one rotate and retry `fn`
|
|
403
|
+
* once; a second failure propagates. No loop.
|
|
404
|
+
*/
|
|
405
|
+
async withSlackConfigAccessToken<T>(
|
|
406
|
+
accountId: string | null,
|
|
407
|
+
fn: (accessToken: string) => Promise<T>,
|
|
408
|
+
): Promise<T> {
|
|
409
|
+
const accessToken = await this.getAccessToken(accountId);
|
|
410
|
+
try {
|
|
411
|
+
return await fn(accessToken);
|
|
412
|
+
} catch (err) {
|
|
413
|
+
if (!isSlackConfigTokenRevokedError(err)) throw err;
|
|
414
|
+
const fresh = await this.getAccessToken(accountId, {
|
|
415
|
+
forceRotate: true,
|
|
416
|
+
rejectedAccessToken: accessToken,
|
|
417
|
+
});
|
|
418
|
+
return fn(fresh);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** Decrypted cached access token if it has more than the minimum left. */
|
|
423
|
+
private cachedAccessToken(
|
|
424
|
+
row: SlackConfigTokenRow,
|
|
425
|
+
crypto: TokenCrypto,
|
|
426
|
+
): string | null {
|
|
427
|
+
if (!row.accessTokenEncrypted || !row.accessTokenExpiresAt) return null;
|
|
428
|
+
const remaining =
|
|
429
|
+
row.accessTokenExpiresAt.getTime() - this.clock.now().getTime();
|
|
430
|
+
if (remaining <= SLACK_CONFIG_ACCESS_TOKEN_MIN_REMAINING_MS) return null;
|
|
431
|
+
return crypto.decrypt(row.accessTokenEncrypted);
|
|
432
|
+
}
|
|
433
|
+
}
|