MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / parseOAuthTokenResponse

Function parseOAuthTokenResponse

cli/src/utils/chatgpt-oauth.ts:26–62  ·  view source on GitHub ↗
(data: unknown)

Source from the content-addressed store, hash-verified

24import type { ChatGptOAuthCredentials } from '@codebuff/sdk'
25
26function parseOAuthTokenResponse(data: unknown): {
27 accessToken: string
28 refreshToken: string
29 expiresInMs: number
30} {
31 if (!data || typeof data !== 'object') {
32 throw new Error('Invalid token response format from ChatGPT OAuth.')
33 }
34
35 const tokenData = data as {
36 access_token?: unknown
37 refresh_token?: unknown
38 expires_in?: unknown
39 }
40
41 if (
42 typeof tokenData.access_token !== 'string' ||
43 tokenData.access_token.trim().length === 0
44 ) {
45 throw new Error('Token exchange did not return a valid access token.')
46 }
47
48 const refreshToken =
49 typeof tokenData.refresh_token === 'string' ? tokenData.refresh_token : ''
50 const expiresInMs =
51 typeof tokenData.expires_in === 'number' &&
52 Number.isFinite(tokenData.expires_in) &&
53 tokenData.expires_in > 0
54 ? tokenData.expires_in * 1000
55 : 3600 * 1000
56
57 return {
58 accessToken: tokenData.access_token,
59 refreshToken,
60 expiresInMs,
61 }
62}
63
64function toBase64Url(buffer: Buffer): string {
65 return buffer

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected