(
row: ConnectionRow,
provider: CredentialProvider,
token: OAuth2TokenResponse,
)
| 1958 | * already consumed, and every later refresh comes back `invalid_grant` — |
| 1959 | * a connection that silently disconnects itself. */ |
| 1960 | const persistRefreshedToken = ( |
| 1961 | row: ConnectionRow, |
| 1962 | provider: CredentialProvider, |
| 1963 | token: OAuth2TokenResponse, |
| 1964 | ): Effect.Effect<void, StorageFailure> => |
| 1965 | Effect.gen(function* () { |
| 1966 | if (provider.set) { |
| 1967 | // OAuth is always single-input: the access token lives in the `token` |
| 1968 | // item. Fall back to a deterministic id if the map is somehow empty. |
| 1969 | const tokenItemId = |
| 1970 | connectionItemIds(row)[PRIMARY_INPUT_VARIABLE] ?? |
| 1971 | `connection:${row.owner}:${row.integration}:${row.name}:${PRIMARY_INPUT_VARIABLE}`; |
| 1972 | if (token.refresh_token && row.refresh_item_id) { |
| 1973 | yield* provider.set(ProviderItemId.make(row.refresh_item_id), token.refresh_token); |
| 1974 | } |
| 1975 | yield* provider.set(ProviderItemId.make(tokenItemId), token.access_token); |
| 1976 | } |
| 1977 | |
| 1978 | const nextExpiresAt = |
| 1979 | typeof token.expires_in === "number" ? Date.now() + token.expires_in * 1000 : null; |
| 1980 | const set: Record<string, unknown> = { |
| 1981 | expires_at: nextExpiresAt, |
| 1982 | updated_at: new Date(), |
| 1983 | }; |
| 1984 | if (token.scope !== undefined) set.oauth_scope = token.scope; |
| 1985 | yield* core.updateMany("connection", { |
| 1986 | where: (b: AnyCb) => |
| 1987 | b.and( |
| 1988 | byOwner(row.owner as Owner)(b), |
| 1989 | b("integration", "=", String(row.integration)), |
| 1990 | b("name", "=", String(row.name)), |
| 1991 | ), |
| 1992 | set, |
| 1993 | }); |
| 1994 | }); |
| 1995 | |
| 1996 | /** The rendered message of a typed enterprise-managed failure. */ |
| 1997 | const enterpriseManagedMessage = (cause: EnterpriseManagedMintError): string => |
no test coverage detected