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