{"record":{"id":"ce429bd822d6148e","repo":"mastra-ai/mastra","slug":"missing-authorization-code-ce429b","errorCode":null,"errorMessage":"Missing authorization code","messagePattern":"Missing authorization code","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/auth/providers/openai-codex.ts","lineNumber":686,"sourceCode":"      if (result?.code) {\n        code = result.code;\n      }\n    }\n\n    // Fallback to onPrompt if still no code\n    if (!code) {\n      const input = await options.onPrompt({\n        message: 'Paste the authorization code (or full redirect URL):',\n      });\n      const parsed = parseAuthorizationInput(input);\n      if (parsed.state && parsed.state !== state) {\n        throw new Error('State mismatch');\n      }\n      code = parsed.code;\n    }\n\n    if (!code) {\n      throw new Error('Missing authorization code');\n    }\n\n    const tokenResult = await exchangeAuthorizationCode(code, verifier, server.redirectUri);\n    if (tokenResult.type !== 'success') {\n      throw new Error('Token exchange failed');\n    }\n\n    const accountId = requireAccountId(tokenResult);\n\n    return {\n      access: tokenResult.access,\n      refresh: tokenResult.refresh,\n      expires: tokenResult.expires,\n      accountId,\n    };\n  } finally {\n    server.close();\n  }","sourceCodeStart":668,"sourceCodeEnd":704,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/auth/providers/openai-codex.ts#L668-L704","documentation":"After all code-acquisition paths (browser callback, manual promise, prompt) the library still has no authorization code, so it cannot exchange anything for tokens and aborts the login. This terminal check guarantees loginOpenAICodex never proceeds to exchangeAuthorizationCode with an empty code.","triggerScenarios":"loginOpenAICodex completes callback wait, manual wait, and the onPrompt fallback with every path yielding an empty/absent code — e.g. onPrompt returns empty string, the browser never redirected to the local server, and no manual input was provided.","commonSituations":"User pressed Enter without pasting anything at the prompt; user abandoned the browser auth page; local callback server port blocked so the browser redirect never landed; headless/SSH environment where neither the browser nor the prompt is usable.","solutions":["Re-run the login and actually paste the authorization code or full redirect URL when prompted.","If headless, use the device-authorization flow (loginOpenAICodexDevice) instead of the browser callback flow.","Verify the local callback port (e.g. localhost:1455) is not firewalled so the browser redirect can reach it.","Implement options.onPrompt to reliably collect input; returning an empty string always leads to this error."],"exampleFix":"// before: prompt returns nothing in headless env\nawait loginOpenAICodex({ onPrompt: async () => '' });\n\n// after: use device login in headless environments\nawait loginOpenAICodexDevice({});","handlingStrategy":"try-catch","validationCode":"function promptReturnsCode(onPrompt: (o: { message: string }) => Promise<string>): boolean {\n  // ensure your onPrompt implementation rejects empty input before returning\n  return typeof onPrompt === 'function';\n}","typeGuard":null,"tryCatchPattern":"try {\n  await loginOpenAICodex({ onPrompt });\n} catch (e) {\n  if (e.message === 'Missing authorization code') {\n    console.error('No code provided — falling back to device login.');\n    await loginOpenAICodexDevice({});\n  } else throw e;\n}","preventionTips":["Make onPrompt re-ask instead of returning empty strings.","Ensure the local callback port is reachable so the browser redirect lands.","Use the device-authorization flow in headless/SSH environments.","Complete the browser authorization before the flow times out."],"tags":["oauth","missing-input","authorization-code","login"],"backgroundTag":"missing-oauth-authorization-code","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}