( code: string, verifier: string, )
| 165 | * Exchanges an authorization code for access + refresh tokens. |
| 166 | */ |
| 167 | export 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. |
no test coverage detected