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

Function refreshAccessToken

packages/core/sdk/src/oauth-helpers.ts:1456–1561  ·  view source on GitHub ↗
(
  input: RefreshAccessTokenInput,
)

Source from the content-addressed store, hash-verified

1454};
1455
1456export const refreshAccessToken = (
1457 input: RefreshAccessTokenInput,
1458): Effect.Effect<OAuth2TokenResponse, OAuth2Error> => {
1459 const requestedScopes = input.scopes && input.scopes.length > 0 ? input.scopes : undefined;
1460 const scopeParameter = (scopes: readonly string[] | undefined): string | undefined =>
1461 scopes === undefined ? undefined : scopes.join(input.scopeSeparator ?? " ");
1462
1463 const attempt = (
1464 scopes: readonly string[] | undefined,
1465 ): Effect.Effect<OAuth2TokenResponse, OAuth2Error> =>
1466 Effect.tryPromise({
1467 try: async () => {
1468 const as = asFromTokenUrlAndIssuer(input.tokenUrl, input.issuerUrl, {
1469 idTokenSigningAlgValuesSupported: input.idTokenSigningAlgValuesSupported,
1470 endpointUrlPolicy: input.endpointUrlPolicy,
1471 });
1472 const client: oauth.Client = { client_id: input.clientId };
1473 const clientAuth = pickClientAuth(
1474 input.clientSecret,
1475 input.clientAuth ?? DEFAULT_CLIENT_AUTH_METHOD,
1476 );
1477 const scope = scopeParameter(scopes);
1478 const extraParams = new URLSearchParams();
1479 if (scope !== undefined) {
1480 extraParams.set("scope", scope);
1481 }
1482 if (input.resource) {
1483 extraParams.set("resource", input.resource);
1484 }
1485 const additionalParameters =
1486 Array.from(extraParams.keys()).length > 0 ? extraParams : undefined;
1487 if (input.requestFormat === "json") {
1488 const response = await jsonTokenEndpointRequest({
1489 tokenUrl: input.tokenUrl,
1490 clientId: input.clientId,
1491 clientSecret: input.clientSecret,
1492 clientAuth: input.clientAuth ?? DEFAULT_CLIENT_AUTH_METHOD,
1493 grantType: "refresh_token",
1494 parameters: {
1495 refresh_token: input.refreshToken,
1496 ...(scope !== undefined ? { scope } : {}),
1497 ...(input.resource ? { resource: input.resource } : {}),
1498 },
1499 timeoutMs: input.timeoutMs,
1500 endpointUrlPolicy: input.endpointUrlPolicy,
1501 fetch: input.fetch,
1502 });
1503 return await processTokenEndpointResponse(as, client, response);
1504 }
1505 const response = await oauth.refreshTokenGrantRequest(
1506 as,
1507 client,
1508 clientAuth,
1509 input.refreshToken,
1510 {
1511 ...oauth4webapiRequestOptions(
1512 input.tokenUrl,
1513 input.timeoutMs,

Callers 2

performTokenRefreshFunction · 0.90

Calls 3

retryWithoutScopeFunction · 0.85
withTokenRequestSpanFunction · 0.85
attemptFunction · 0.70

Tested by

no test coverage detected