(
row: ConnectionRow,
provider: CredentialProvider,
token: OAuth2TokenResponse,
storedRefreshToken?: string | undefined,
)
| 2227 | * has not changed; a rotated token never matches, so the write that |
| 2228 | * actually matters is never skipped. */ |
| 2229 | const persistRefreshedToken = ( |
| 2230 | row: ConnectionRow, |
| 2231 | provider: CredentialProvider, |
| 2232 | token: OAuth2TokenResponse, |
| 2233 | storedRefreshToken?: string | undefined, |
| 2234 | ): Effect.Effect<void, StorageFailure> => |
| 2235 | Effect.gen(function* () { |
| 2236 | if (provider.set) { |
| 2237 | // OAuth is always single-input: the access token lives in the `token` |
| 2238 | // item. Fall back to a deterministic id if the map is somehow empty. |
| 2239 | const tokenItemId = |
| 2240 | connectionItemIds(row)[PRIMARY_INPUT_VARIABLE] ?? |
| 2241 | `connection:${row.owner}:${row.integration}:${row.name}:${PRIMARY_INPUT_VARIABLE}`; |
| 2242 | if ( |
| 2243 | token.refresh_token && |
| 2244 | row.refresh_item_id && |
| 2245 | token.refresh_token !== storedRefreshToken |
| 2246 | ) { |
| 2247 | yield* provider.set(ProviderItemId.make(row.refresh_item_id), token.refresh_token); |
| 2248 | } |
| 2249 | yield* provider.set(ProviderItemId.make(tokenItemId), token.access_token); |
| 2250 | } |
| 2251 | |
| 2252 | const nextExpiresAt = |
| 2253 | typeof token.expires_in === "number" ? Date.now() + token.expires_in * 1000 : null; |
| 2254 | const set: Record<string, unknown> = { |
| 2255 | expires_at: nextExpiresAt, |
| 2256 | updated_at: new Date(), |
| 2257 | }; |
| 2258 | if (token.scope !== undefined) set.oauth_scope = token.scope; |
| 2259 | yield* core.updateMany("connection", { |
| 2260 | where: (b: AnyCb) => |
| 2261 | b.and( |
| 2262 | byOwner(row.owner as Owner)(b), |
| 2263 | b("integration", "=", String(row.integration)), |
| 2264 | b("name", "=", String(row.name)), |
| 2265 | ), |
| 2266 | set, |
| 2267 | }); |
| 2268 | }); |
| 2269 | |
| 2270 | /** The rendered message of a typed enterprise-managed failure. */ |
| 2271 | const enterpriseManagedMessage = (cause: EnterpriseManagedMintError): string => |
no test coverage detected