(
row: ConnectionRow,
provider: CredentialProvider,
trigger: RefreshTrigger,
)
| 2560 | |
| 2561 | // Perform the actual refresh-token grant and persist the rotated material. |
| 2562 | const performTokenRefresh = ( |
| 2563 | row: ConnectionRow, |
| 2564 | provider: CredentialProvider, |
| 2565 | trigger: RefreshTrigger, |
| 2566 | ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> => |
| 2567 | Effect.gen(function* () { |
| 2568 | const owner = row.owner as Owner; |
| 2569 | const reauth = ( |
| 2570 | message: string, |
| 2571 | options?: { |
| 2572 | readonly credentialMissing?: boolean; |
| 2573 | readonly blockedByAdmin?: boolean; |
| 2574 | }, |
| 2575 | ): CredentialResolutionError => |
| 2576 | new CredentialResolutionError({ |
| 2577 | owner, |
| 2578 | integration: IntegrationSlug.make(row.integration), |
| 2579 | name: ConnectionName.make(row.name), |
| 2580 | message, |
| 2581 | reauthRequired: true, |
| 2582 | ...(options?.credentialMissing === true ? { credentialMissing: true } : {}), |
| 2583 | ...(options?.blockedByAdmin === true ? { blockedByAdmin: true } : {}), |
| 2584 | }); |
| 2585 | |
| 2586 | // A recorded invalid_grant is the AS's standing verdict on this grant: |
| 2587 | // re-sending it cannot succeed, so don't. Fail as reauth-required |
| 2588 | // without a token request — the reconnect mint rewrites |
| 2589 | // `provider_state` and thereby re-arms refresh. Without this gate a |
| 2590 | // dead connection re-sent its dead grant on every proactive cycle, |
| 2591 | // indefinitely (owner.com's Datadog connections: 100+ identical |
| 2592 | // rejections over two days, surfacing nothing). |
| 2593 | const reauthState = oauthReauthRequiredFromProviderState(row.provider_state); |
| 2594 | if (reauthState !== null) { |
| 2595 | yield* Effect.annotateCurrentSpan({ |
| 2596 | "executor.oauth.refresh.skipped_known_dead": true, |
| 2597 | }); |
| 2598 | const recordedHealth = Option.getOrNull(decodeLastHealth(row.last_health)); |
| 2599 | const recordedDetail = |
| 2600 | reauthState.oauthReauthRequiredDetail ?? |
| 2601 | (recordedHealth?.status === "expired" ? recordedHealth.detail : undefined); |
| 2602 | const detail = |
| 2603 | recordedDetail === undefined |
| 2604 | ? "The authorization server rejected this connection's refresh token (invalid_grant). Reconnect to continue." |
| 2605 | : recordedDetail.endsWith("Reconnect to continue.") |
| 2606 | ? recordedDetail |
| 2607 | : `${recordedDetail} Reconnect to continue.`; |
| 2608 | // An admin-policy denial recorded on the dead grant must survive |
| 2609 | // reconstruction: without the flag, callers past the first denial |
| 2610 | // would show ordinary reconnect guidance — the interactive OAuth |
| 2611 | // route the policy contract forbids offering. |
| 2612 | return yield* reauth(detail, { |
| 2613 | blockedByAdmin: recordedDeadGrantReason(reauthState) === "blocked_by_admin", |
| 2614 | }); |
| 2615 | } |
| 2616 | |
| 2617 | // Load the backing app. A `first-party:` slug resolves from host config |
| 2618 | // (deployment-owned identity, in-memory secret); a stored slug loads by |
| 2619 | // the owner STORED on the connection (a Personal connection may be |
no test coverage detected