(
row: ConnectionRow,
provider: CredentialProvider,
token: OAuth2TokenResponse,
storedRefreshToken?: string | undefined,
)
| 2347 | * has not changed; a rotated token never matches, so the write that |
| 2348 | * actually matters is never skipped. */ |
| 2349 | const persistRefreshedToken = ( |
| 2350 | row: ConnectionRow, |
| 2351 | provider: CredentialProvider, |
| 2352 | token: OAuth2TokenResponse, |
| 2353 | storedRefreshToken?: string | undefined, |
| 2354 | ): Effect.Effect<void, StorageFailure> => |
| 2355 | Effect.gen(function* () { |
| 2356 | if (provider.set) { |
| 2357 | // OAuth is always single-input: the access token lives in the `token` |
| 2358 | // item. Fall back to a deterministic id if the map is somehow empty. |
| 2359 | const tokenItemId = |
| 2360 | connectionItemIds(row)[PRIMARY_INPUT_VARIABLE] ?? |
| 2361 | `connection:${row.owner}:${row.integration}:${row.name}:${PRIMARY_INPUT_VARIABLE}`; |
| 2362 | if ( |
| 2363 | token.refresh_token && |
| 2364 | row.refresh_item_id && |
| 2365 | token.refresh_token !== storedRefreshToken |
| 2366 | ) { |
| 2367 | yield* provider.set(ProviderItemId.make(row.refresh_item_id), token.refresh_token); |
| 2368 | } |
| 2369 | yield* provider.set(ProviderItemId.make(tokenItemId), token.access_token); |
| 2370 | } |
| 2371 | |
| 2372 | const nextExpiresAt = |
| 2373 | typeof token.expires_in === "number" ? Date.now() + token.expires_in * 1000 : null; |
| 2374 | const set: Record<string, unknown> = { |
| 2375 | expires_at: nextExpiresAt, |
| 2376 | updated_at: new Date(), |
| 2377 | }; |
| 2378 | if (token.scope !== undefined) set.oauth_scope = token.scope; |
| 2379 | yield* core.updateMany("connection", { |
| 2380 | where: (b: AnyCb) => |
| 2381 | b.and( |
| 2382 | byOwner(row.owner as Owner)(b), |
| 2383 | b("integration", "=", String(row.integration)), |
| 2384 | b("name", "=", String(row.name)), |
| 2385 | ), |
| 2386 | set, |
| 2387 | }); |
| 2388 | }); |
| 2389 | |
| 2390 | /** The rendered message of a typed enterprise-managed failure. */ |
| 2391 | const enterpriseManagedMessage = (cause: EnterpriseManagedMintError): string => |
no test coverage detected