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

Function refreshDeviceTokens

apps/cli/src/device-login.ts:331–361  ·  view source on GitHub ↗
(input: {
  readonly tokenEndpoint: string;
  readonly clientId: string;
  readonly refreshToken: string;
  readonly headers?: Readonly<Record<string, string>>;
  readonly serverOrigin?: string;
})

Source from the content-addressed store, hash-verified

329 * providers that issue refresh tokens reach here (WorkOS, which is form-encoded
330 * per RFC 8628); Better Auth's device flow issues no refresh token. */
331export const refreshDeviceTokens = async (input: {
332 readonly tokenEndpoint: string;
333 readonly clientId: string;
334 readonly refreshToken: string;
335 readonly headers?: Readonly<Record<string, string>>;
336 readonly serverOrigin?: string;
337}): Promise<DeviceTokens> => {
338 const response = await post(
339 input.tokenEndpoint,
340 {
341 grant_type: "refresh_token",
342 refresh_token: input.refreshToken,
343 client_id: input.clientId,
344 },
345 "form",
346 input,
347 );
348 const body = await readJson(response);
349 if (!response.ok) {
350 throw new DeviceLoginError(
351 `Token refresh failed: ${asString(body.error_description) ?? asString(body.error) ?? `HTTP ${response.status}`}`,
352 );
353 }
354 const accessToken = asString(body.access_token);
355 if (!accessToken) throw new DeviceLoginError("Refresh response was missing an access token.");
356 return {
357 accessToken,
358 refreshToken: asString(body.refresh_token) ?? input.refreshToken,
359 expiresAt: deriveExpiresAt({ accessToken, expiresIn: asNumber(body.expires_in) }),
360 };
361};
362
363export type BrowserOpenCommand = readonly [command: string, args: ReadonlyArray<string>];
364

Callers 2

refreshOAuthConnectionFunction · 0.90

Calls 5

readJsonFunction · 0.85
asStringFunction · 0.85
deriveExpiresAtFunction · 0.85
asNumberFunction · 0.85
postFunction · 0.70

Tested by

no test coverage detected