MCPcopy Create free account
hub / github.com/heygen-com/hyperframes / parseTokenResponse

Function parseTokenResponse

packages/cli/src/auth/oauth.ts:332–367  ·  view source on GitHub ↗
(payload: unknown)

Source from the content-addressed store, hash-verified

330 */
331// fallow-ignore-next-line complexity
332export function parseTokenResponse(payload: unknown): OAuthTokens {
333 if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
334 throw ErrRefreshFailed("token endpoint returned a non-object payload");
335 }
336 const obj = payload as Record<string, unknown>;
337 const accessToken = stringField(obj, "access_token");
338 if (!accessToken) {
339 throw ErrRefreshFailed("token endpoint did not return an access_token");
340 }
341 if (!isHeaderSafe(accessToken)) {
342 throw ErrRefreshFailed("access_token contains control characters");
343 }
344
345 const out: OAuthTokens = { access_token: accessToken };
346 const refreshToken = stringField(obj, "refresh_token");
347 if (refreshToken) {
348 if (!isHeaderSafe(refreshToken)) {
349 throw ErrRefreshFailed("refresh_token contains control characters");
350 }
351 out.refresh_token = refreshToken;
352 }
353 const tokenType = stringField(obj, "token_type");
354 if (tokenType) out.token_type = tokenType;
355 const scope = stringField(obj, "scope");
356 if (scope) out.scope = scope;
357
358 const expiresIn = numericField(obj, "expires_in");
359 if (expiresIn !== undefined) {
360 // Clamp to a sensible minimum so a misbehaving / clock-skewed
361 // server returning 0 or a negative value doesn't put expires_at in
362 // the past and cause the 401-refresh path to loop.
363 const clamped = Math.max(expiresIn, MIN_EXPIRES_IN_SECONDS);
364 out.expires_at = new Date(Date.now() + clamped * 1000).toISOString();
365 }
366 return out;
367}
368
369function stringField(obj: Record<string, unknown>, key: string): string | undefined {
370 const v = obj[key];

Callers 3

refreshTokensFunction · 0.85
exchangeCodeForTokensFunction · 0.85
oauth.test.tsFile · 0.85

Calls 5

ErrRefreshFailedFunction · 0.85
isHeaderSafeFunction · 0.85
numericFieldFunction · 0.85
nowMethod · 0.80
stringFieldFunction · 0.70

Tested by

no test coverage detected