MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / performTokenRefresh

Function performTokenRefresh

packages/core/sdk/src/executor.ts:1799–2025  ·  view source on GitHub ↗
(
      row: ConnectionRow,
      provider: CredentialProvider,
      trigger: RefreshTrigger,
    )

Source from the content-addressed store, hash-verified

1797
1798 // Perform the actual refresh-token grant and persist the rotated material.
1799 const performTokenRefresh = (
1800 row: ConnectionRow,
1801 provider: CredentialProvider,
1802 trigger: RefreshTrigger,
1803 ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> =>
1804 Effect.gen(function* () {
1805 const owner = row.owner as Owner;
1806 const reauth = (message: string): CredentialResolutionError =>
1807 new CredentialResolutionError({
1808 owner,
1809 integration: IntegrationSlug.make(row.integration),
1810 name: ConnectionName.make(row.name),
1811 message,
1812 reauthRequired: true,
1813 });
1814
1815 // A recorded invalid_grant is the AS's standing verdict on this grant:
1816 // re-sending it cannot succeed, so don't. Fail as reauth-required
1817 // without a token request — the reconnect mint rewrites
1818 // `provider_state` and thereby re-arms refresh. Without this gate a
1819 // dead connection re-sent its dead grant on every proactive cycle,
1820 // indefinitely (owner.com's Datadog connections: 100+ identical
1821 // rejections over two days, surfacing nothing).
1822 if (oauthReauthRequiredAtFromProviderState(row.provider_state) !== null) {
1823 yield* Effect.annotateCurrentSpan({ "executor.oauth.refresh.skipped_known_dead": true });
1824 return yield* reauth(
1825 "The authorization server rejected this connection's refresh token (invalid_grant). Reconnect to continue.",
1826 );
1827 }
1828
1829 // Load the backing app by the owner STORED on the connection (a Personal
1830 // connection may be backed by a shared Workspace app) — no derivation.
1831 const clientOwner = (row.oauth_client_owner ?? row.owner) as Owner;
1832 const clientRow = yield* loadOAuthClientRow(clientOwner, String(row.oauth_client));
1833 if (!clientRow) {
1834 return yield* reauth(`OAuth client "${row.oauth_client}" is no longer registered.`);
1835 }
1836
1837 // The secret is stored in the provider (a vault item id), not inline.
1838 const clientSecret = clientRow.client_secret_item_id
1839 ? ((yield* provider.get(ProviderItemId.make(String(clientRow.client_secret_item_id)))) ??
1840 "")
1841 : "";
1842 // Re-request the scopes this connection was GRANTED (RFC 6749 §6: a
1843 // refresh must not exceed the originally-granted scope). Empty → omit
1844 // the param, which the AS treats as "same scopes as granted".
1845 const grantedScopes = row.oauth_scope
1846 ? String(row.oauth_scope).split(/\s+/).filter(Boolean)
1847 : [];
1848
1849 // Refresh against the region the code was redeemed at when one was
1850 // recorded at connect time (multi-site providers like Datadog), else
1851 // the oauth_client's configured token endpoint.
1852 const tokenUrl = row.oauth_token_url
1853 ? String(row.oauth_token_url)
1854 : String(clientRow.token_url);
1855
1856 // client_credentials (machine-to-machine) has NO refresh token — the

Callers 1

refreshConnectionTokenFunction · 0.85

Calls 10

refreshAccessTokenFunction · 0.90
reauthFunction · 0.85
loadOAuthClientRowFunction · 0.85
markRefreshGrantDeadFunction · 0.85
connectionItemIdsFunction · 0.85
byOwnerFunction · 0.85
setMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected