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

Function performTokenRefresh

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

Source from the content-addressed store, hash-verified

1827
1828 // Perform the actual refresh-token grant and persist the rotated material.
1829 const performTokenRefresh = (
1830 row: ConnectionRow,
1831 provider: CredentialProvider,
1832 trigger: RefreshTrigger,
1833 ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> =>
1834 Effect.gen(function* () {
1835 const owner = row.owner as Owner;
1836 const reauth = (message: string): CredentialResolutionError =>
1837 new CredentialResolutionError({
1838 owner,
1839 integration: IntegrationSlug.make(row.integration),
1840 name: ConnectionName.make(row.name),
1841 message,
1842 reauthRequired: true,
1843 });
1844
1845 // A recorded invalid_grant is the AS's standing verdict on this grant:
1846 // re-sending it cannot succeed, so don't. Fail as reauth-required
1847 // without a token request — the reconnect mint rewrites
1848 // `provider_state` and thereby re-arms refresh. Without this gate a
1849 // dead connection re-sent its dead grant on every proactive cycle,
1850 // indefinitely (owner.com's Datadog connections: 100+ identical
1851 // rejections over two days, surfacing nothing).
1852 const reauthState = oauthReauthRequiredFromProviderState(row.provider_state);
1853 if (reauthState !== null) {
1854 yield* Effect.annotateCurrentSpan({ "executor.oauth.refresh.skipped_known_dead": true });
1855 const recordedHealth = Option.getOrNull(decodeLastHealth(row.last_health));
1856 const recordedDetail =
1857 reauthState.oauthReauthRequiredDetail ??
1858 (recordedHealth?.status === "expired" ? recordedHealth.detail : undefined);
1859 const detail =
1860 recordedDetail === undefined
1861 ? "The authorization server rejected this connection's refresh token (invalid_grant). Reconnect to continue."
1862 : recordedDetail.endsWith("Reconnect to continue.")
1863 ? recordedDetail
1864 : `${recordedDetail} Reconnect to continue.`;
1865 return yield* reauth(detail);
1866 }
1867
1868 // Load the backing app. A `first-party:` slug resolves from host config
1869 // (deployment-owned identity, in-memory secret); a stored slug loads by
1870 // the owner STORED on the connection (a Personal connection may be
1871 // backed by a shared Workspace app) — no derivation — with its secret
1872 // resolved out of the credential provider by item id.
1873 const clientSlug = String(row.oauth_client);
1874 const clientRow: RefreshClient | null = yield* Effect.gen(function* () {
1875 if (isFirstPartyOAuthClientSlug(clientSlug)) {
1876 const firstParty = firstPartyOAuthBySlug.get(clientSlug);
1877 if (!firstParty) return null;
1878 return {
1879 clientId: firstParty.clientId,
1880 clientSecret: firstParty.clientSecret,
1881 tokenUrl: firstParty.tokenUrl,
1882 grant: "authorization_code",
1883 resource: null,
1884 } satisfies RefreshClient;
1885 }
1886 const clientOwner = (row.oauth_client_owner ?? row.owner) as Owner;

Callers 1

refreshConnectionTokenFunction · 0.85

Calls 11

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