* Parses a manually pasted callback input (could be the full redirect URL * or just the raw code).
( input: string, expectedState: string, )
| 373 | * or just the raw code). |
| 374 | */ |
| 375 | function parseCodexCallbackInput( |
| 376 | input: string, |
| 377 | expectedState: string, |
| 378 | ): { code: string | undefined } { |
| 379 | const value = input.trim() |
| 380 | if (!value) return { code: undefined } |
| 381 | |
| 382 | try { |
| 383 | const url = new URL(value) |
| 384 | const urlState = url.searchParams.get('state') |
| 385 | if (urlState && urlState !== expectedState) { |
| 386 | throw new Error('State mismatch in pasted URL') |
| 387 | } |
| 388 | return { code: url.searchParams.get('code') ?? undefined } |
| 389 | } catch { |
| 390 | // Not a URL — treat as raw code |
| 391 | } |
| 392 | |
| 393 | if (value.includes('code=')) { |
| 394 | const params = new URLSearchParams(value) |
| 395 | return { code: params.get('code') ?? undefined } |
| 396 | } |
| 397 | |
| 398 | return { code: value } |
| 399 | } |
no test coverage detected