MCPcopy Create free account
hub / github.com/freecodexyz/free-code / exchangeCodexCode

Function exchangeCodexCode

src/services/oauth/codex-client.ts:167–193  ·  view source on GitHub ↗
(
  code: string,
  verifier: string,
)

Source from the content-addressed store, hash-verified

165 * Exchanges an authorization code for access + refresh tokens.
166 */
167export async function exchangeCodexCode(
168 code: string,
169 verifier: string,
170): Promise<CodexTokens> {
171 const result = await postToTokenUrl(
172 new URLSearchParams({
173 grant_type: 'authorization_code',
174 client_id: CODEX_CLIENT_ID,
175 code,
176 code_verifier: verifier,
177 redirect_uri: CODEX_REDIRECT_URI,
178 }),
179 )
180 if (result.type !== 'success') {
181 throw new Error('Codex token exchange failed. Please try again.')
182 }
183 const accountId = extractCodexAccountId(result.access)
184 if (!accountId) {
185 throw new Error('Failed to extract accountId from Codex token.')
186 }
187 return {
188 accessToken: result.access,
189 refreshToken: result.refresh,
190 expiresAt: result.expires,
191 accountId,
192 }
193}
194
195/**
196 * Refreshes an expired Codex access token.

Callers 1

runCodexOAuthFlowFunction · 0.85

Calls 2

postToTokenUrlFunction · 0.85
extractCodexAccountIdFunction · 0.85

Tested by

no test coverage detected