(
row: ConnectionRow,
provider: CredentialProvider,
trigger: RefreshTrigger,
)
| 1746 | |
| 1747 | // Perform the actual refresh-token grant and persist the rotated material. |
| 1748 | const performTokenRefresh = ( |
| 1749 | row: ConnectionRow, |
| 1750 | provider: CredentialProvider, |
| 1751 | trigger: RefreshTrigger, |
| 1752 | ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> => |
| 1753 | Effect.gen(function* () { |
| 1754 | const owner = row.owner as Owner; |
| 1755 | const reauth = (message: string): CredentialResolutionError => |
| 1756 | new CredentialResolutionError({ |
| 1757 | owner, |
| 1758 | integration: IntegrationSlug.make(row.integration), |
| 1759 | name: ConnectionName.make(row.name), |
| 1760 | message, |
| 1761 | reauthRequired: true, |
| 1762 | }); |
| 1763 | |
| 1764 | // Load the backing app by the owner STORED on the connection (a Personal |
| 1765 | // connection may be backed by a shared Workspace app) — no derivation. |
| 1766 | const clientOwner = (row.oauth_client_owner ?? row.owner) as Owner; |
| 1767 | const clientRow = yield* loadOAuthClientRow(clientOwner, String(row.oauth_client)); |
| 1768 | if (!clientRow) { |
| 1769 | return yield* reauth(`OAuth client "${row.oauth_client}" is no longer registered.`); |
| 1770 | } |
| 1771 | |
| 1772 | // The secret is stored in the provider (a vault item id), not inline. |
| 1773 | const clientSecret = clientRow.client_secret_item_id |
| 1774 | ? ((yield* provider.get(ProviderItemId.make(String(clientRow.client_secret_item_id)))) ?? |
| 1775 | "") |
| 1776 | : ""; |
| 1777 | // Re-request the scopes this connection was GRANTED (RFC 6749 §6: a |
| 1778 | // refresh must not exceed the originally-granted scope). Empty → omit |
| 1779 | // the param, which the AS treats as "same scopes as granted". |
| 1780 | const grantedScopes = row.oauth_scope |
| 1781 | ? String(row.oauth_scope).split(/\s+/).filter(Boolean) |
| 1782 | : []; |
| 1783 | |
| 1784 | // Refresh against the region the code was redeemed at when one was |
| 1785 | // recorded at connect time (multi-site providers like Datadog), else |
| 1786 | // the oauth_client's configured token endpoint. |
| 1787 | const tokenUrl = row.oauth_token_url |
| 1788 | ? String(row.oauth_token_url) |
| 1789 | : String(clientRow.token_url); |
| 1790 | |
| 1791 | // client_credentials (machine-to-machine) has NO refresh token — the |
| 1792 | // token is RE-MINTED from the client id/secret. The authorization_code |
| 1793 | // path below needs a stored refresh token. Branching on grant here is |
| 1794 | // what keeps a client_credentials connection (e.g. DealCloud) from |
| 1795 | // demanding a re-auth on a credential that has no human to re-auth. |
| 1796 | const token = |
| 1797 | String(clientRow.grant) === "client_credentials" |
| 1798 | ? yield* exchangeClientCredentials({ |
| 1799 | tokenUrl, |
| 1800 | clientId: String(clientRow.client_id), |
| 1801 | clientSecret, |
| 1802 | scopes: grantedScopes, |
| 1803 | resource: clientRow.resource ? String(clientRow.resource) : undefined, |
| 1804 | endpointUrlPolicy: config.oauthEndpointUrlPolicy, |
| 1805 | fetch: config.fetch, |
no test coverage detected