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

Function performTokenRefresh

packages/core/sdk/src/executor.ts:1482–1618  ·  view source on GitHub ↗
(
      row: ConnectionRow,
      provider: CredentialProvider,
    )

Source from the content-addressed store, hash-verified

1480
1481 // Perform the actual refresh-token grant and persist the rotated material.
1482 const performTokenRefresh = (
1483 row: ConnectionRow,
1484 provider: CredentialProvider,
1485 ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> =>
1486 Effect.gen(function* () {
1487 const owner = row.owner as Owner;
1488 const reauth = (message: string): CredentialResolutionError =>
1489 new CredentialResolutionError({
1490 owner,
1491 integration: IntegrationSlug.make(row.integration),
1492 name: ConnectionName.make(row.name),
1493 message,
1494 reauthRequired: true,
1495 });
1496
1497 // Load the backing app by the owner STORED on the connection (a Personal
1498 // connection may be backed by a shared Workspace app) — no derivation.
1499 const clientOwner = (row.oauth_client_owner ?? row.owner) as Owner;
1500 const clientRow = yield* loadOAuthClientRow(clientOwner, String(row.oauth_client));
1501 if (!clientRow) {
1502 return yield* reauth(`OAuth client "${row.oauth_client}" is no longer registered.`);
1503 }
1504
1505 // The secret is stored in the provider (a vault item id), not inline.
1506 const clientSecret = clientRow.client_secret_item_id
1507 ? ((yield* provider.get(ProviderItemId.make(String(clientRow.client_secret_item_id)))) ??
1508 "")
1509 : "";
1510 // Re-request the scopes this connection was GRANTED (RFC 6749 §6: a
1511 // refresh must not exceed the originally-granted scope). Empty → omit
1512 // the param, which the AS treats as "same scopes as granted".
1513 const grantedScopes = row.oauth_scope
1514 ? String(row.oauth_scope).split(/\s+/).filter(Boolean)
1515 : [];
1516
1517 // Refresh against the region the code was redeemed at when one was
1518 // recorded at connect time (multi-site providers like Datadog), else
1519 // the oauth_client's configured token endpoint.
1520 const tokenUrl = row.oauth_token_url
1521 ? String(row.oauth_token_url)
1522 : String(clientRow.token_url);
1523
1524 // client_credentials (machine-to-machine) has NO refresh token — the
1525 // token is RE-MINTED from the client id/secret. The authorization_code
1526 // path below needs a stored refresh token. Branching on grant here is
1527 // what keeps a client_credentials connection (e.g. DealCloud) from
1528 // demanding a re-auth on a credential that has no human to re-auth.
1529 const token =
1530 String(clientRow.grant) === "client_credentials"
1531 ? yield* exchangeClientCredentials({
1532 tokenUrl,
1533 clientId: String(clientRow.client_id),
1534 clientSecret,
1535 scopes: grantedScopes,
1536 resource: clientRow.resource ? String(clientRow.resource) : undefined,
1537 endpointUrlPolicy: config.oauthEndpointUrlPolicy,
1538 fetch: config.fetch,
1539 }).pipe(

Callers 1

refreshConnectionTokenFunction · 0.85

Calls 8

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

Tested by

no test coverage detected