(
row: ConnectionRow,
provider: CredentialProvider,
trigger: RefreshTrigger,
)
| 2135 | |
| 2136 | // Perform the actual refresh-token grant and persist the rotated material. |
| 2137 | const performTokenRefresh = ( |
| 2138 | row: ConnectionRow, |
| 2139 | provider: CredentialProvider, |
| 2140 | trigger: RefreshTrigger, |
| 2141 | ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> => |
| 2142 | Effect.gen(function* () { |
| 2143 | const owner = row.owner as Owner; |
| 2144 | const reauth = (message: string): CredentialResolutionError => |
| 2145 | new CredentialResolutionError({ |
| 2146 | owner, |
| 2147 | integration: IntegrationSlug.make(row.integration), |
| 2148 | name: ConnectionName.make(row.name), |
| 2149 | message, |
| 2150 | reauthRequired: true, |
| 2151 | }); |
| 2152 | |
| 2153 | // A recorded invalid_grant is the AS's standing verdict on this grant: |
| 2154 | // re-sending it cannot succeed, so don't. Fail as reauth-required |
| 2155 | // without a token request — the reconnect mint rewrites |
| 2156 | // `provider_state` and thereby re-arms refresh. Without this gate a |
| 2157 | // dead connection re-sent its dead grant on every proactive cycle, |
| 2158 | // indefinitely (owner.com's Datadog connections: 100+ identical |
| 2159 | // rejections over two days, surfacing nothing). |
| 2160 | const reauthState = oauthReauthRequiredFromProviderState(row.provider_state); |
| 2161 | if (reauthState !== null) { |
| 2162 | yield* Effect.annotateCurrentSpan({ "executor.oauth.refresh.skipped_known_dead": true }); |
| 2163 | const recordedHealth = Option.getOrNull(decodeLastHealth(row.last_health)); |
| 2164 | const recordedDetail = |
| 2165 | reauthState.oauthReauthRequiredDetail ?? |
| 2166 | (recordedHealth?.status === "expired" ? recordedHealth.detail : undefined); |
| 2167 | const detail = |
| 2168 | recordedDetail === undefined |
| 2169 | ? "The authorization server rejected this connection's refresh token (invalid_grant). Reconnect to continue." |
| 2170 | : recordedDetail.endsWith("Reconnect to continue.") |
| 2171 | ? recordedDetail |
| 2172 | : `${recordedDetail} Reconnect to continue.`; |
| 2173 | return yield* reauth(detail); |
| 2174 | } |
| 2175 | |
| 2176 | // Load the backing app. A `first-party:` slug resolves from host config |
| 2177 | // (deployment-owned identity, in-memory secret); a stored slug loads by |
| 2178 | // the owner STORED on the connection (a Personal connection may be |
| 2179 | // backed by a shared Workspace app) — no derivation — with its secret |
| 2180 | // resolved out of the credential provider by item id. |
| 2181 | const clientSlug = String(row.oauth_client); |
| 2182 | const clientRow: RefreshClient | null = yield* Effect.gen(function* () { |
| 2183 | if (isFirstPartyOAuthClientSlug(clientSlug)) { |
| 2184 | const firstParty = firstPartyOAuthBySlug.get(clientSlug); |
| 2185 | if (!firstParty) return null; |
| 2186 | return { |
| 2187 | clientId: firstParty.clientId, |
| 2188 | clientSecret: firstParty.clientSecret, |
| 2189 | tokenUrl: firstParty.tokenUrl, |
| 2190 | grant: "authorization_code", |
| 2191 | resource: null, |
| 2192 | } satisfies RefreshClient; |
| 2193 | } |
| 2194 | const clientOwner = (row.oauth_client_owner ?? row.owner) as Owner; |
no test coverage detected