feat(OpenAI Chat Model Node): Add OpenAI account OAuth support - #29184
feat(OpenAI Chat Model Node): Add OpenAI account OAuth support#29184EtienneLescot wants to merge 2 commits into
Conversation
|
Hey @EtienneLescot, Thank you for your contribution. We appreciate the time and effort you’ve taken to submit this pull request. Before we can proceed, please ensure the following: Regarding new nodes: If your node integrates with an AI service that you own or represent, please email nodes@n8n.io and we will be happy to discuss the best approach. About review timelines: Thank you again for contributing to n8n. |
|
Hey @EtienneLescot, Thank you for your contribution. We appreciate the time and effort you’ve taken to submit this pull request. Before we can proceed, please ensure the following: Regarding new nodes: If your node integrates with an AI service that you own or represent, please email nodes@n8n.io and we will be happy to discuss the best approach. About review timelines: Thank you again for contributing to n8n. |
5bc36d8 to
31b9c19
Compare
311f2e8 to
27c425f
Compare
|
Addressed the Aikido secret-scan false positives by replacing fixture-looking test values with clearly synthetic test data generated at runtime. No real secrets were present. |
e360b00 to
1af4f7d
Compare
1af4f7d to
d9e32c9
Compare
There was a problem hiding this comment.
7 issues found across 24 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/cli/src/controllers/oauth/openai-oauth2-device.controller.ts">
<violation number="1" location="packages/cli/src/controllers/oauth/openai-oauth2-device.controller.ts:77">
P2: Custom agent: **Use DTOs for Request Body Validation**
Controller endpoint reads `req.body` directly instead of using `@Body` with a `*Dto` class for standardized runtime validation.</violation>
<violation number="2" location="packages/cli/src/controllers/oauth/openai-oauth2-device.controller.ts:329">
P1: Custom agent: **Security Review**
Unescaped JSON is interpolated into an inline `<script>`, enabling script-breakout/XSS if interpolated values contain `</script>`-like payloads.</violation>
</file>
<file name="packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/OpenAiAccountChatModel.ts">
<violation number="1" location="packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/OpenAiAccountChatModel.ts:422">
P2: Streaming path never populates `assistantPhase`, so phase metadata is dropped in streamed responses while non-stream responses preserve it.</violation>
<violation number="2" location="packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/OpenAiAccountChatModel.ts:473">
P2: Finish reason is inferred only from tool-call presence, so provider terminal reasons (e.g., incomplete due to max tokens/content filter) are lost and reported as stop/tool-calls.</violation>
<violation number="3" location="packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/OpenAiAccountChatModel.ts:1092">
P2: SSE parsing is hardcoded to `\n\n` delimiters and is not compatible with valid CRLF-framed SSE streams.</violation>
</file>
<file name="packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/methods/loadModels.ts">
<violation number="1" location="packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/methods/loadModels.ts:65">
P2: OAuth model discovery uses raw fetch without proxy dispatcher, unlike the existing API-key path, which can break model loading behind required outbound proxies.</violation>
</file>
<file name="packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/LmChatOpenAi.node.ts">
<violation number="1" location="packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/LmChatOpenAi.node.ts:805">
P2: OAuth2 execution path returns early and skips the node’s normal option mapping, so many user-configured model options are silently ignored for OpenAI Account auth.</violation>
</file>
Architecture diagram
sequenceDiagram
participant UI as n8n Editor UI
participant Backend as n8n Backend (CLI)
participant OAIAuth as OpenAI Auth API
participant OAICodex as OpenAI Codex API (ChatGPT)
participant Node as OpenAI Chat Model Node
Note over UI,Backend: NEW: OAuth2 Device Login Flow
UI->>Backend: GET /openai-oauth2-credential/device-auth
Backend->>OAIAuth: POST /deviceauth/usercode (Request Challenge)
OAIAuth-->>Backend: Return {user_code, device_auth_id}
Backend-->>UI: Render HTML with Code & Verification Link
Note over UI,OAIAuth: User manually verifies code on OpenAI site
loop Polling Status
Backend->>OAIAuth: POST /deviceauth/token
alt Pending
OAIAuth-->>Backend: 403 Forbidden / 404 Not Found
else Success
OAIAuth-->>Backend: 200 OK {authorization_code}
end
end
Backend->>OAIAuth: POST /oauth/token (Exchange Code)
OAIAuth-->>Backend: Access Token
Backend->>Backend: NEW: Encrypt and save to "openAiOAuth2Api" credential
Note over UI,OAICodex: NEW: Chat Execution Flow (OAuth Path)
UI->>Node: Execute Workflow (Authentication = 'oAuth2')
Node->>Node: CHANGED: Load 'openAiOAuth2Api' credentials
Node->>Node: NEW: Instantiate OpenAiAccountChatModel
Node->>OAICodex: POST /backend-api/codex/responses
Note right of Node: Maps LangChain messages to Codex protocol
alt Streaming Response
loop Every Chunk
OAICodex-->>Node: SSE: text-delta or tool-call-delta
Node-->>UI: Stream chunk to Editor
end
else Standard Response
OAICodex-->>Node: SSE: response.completed
Node-->>UI: Return final AIMessage
end
Note over Node,OAICodex: NEW: Follow-up Prompt Logic
opt Consecutive Calls
Node->>Node: Store previous_response_id
Node->>OAICodex: POST /codex/responses (including previous_response_id)
Note right of Node: Enables stateful incremental prompts
end
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
380aae1 to
0a9e01a
Compare
fd94d64 to
a9fe2c4
Compare
f1f4cc7 to
fb5d120
Compare
|
Is this a feature that is under development? Does it mean that it's gonna be available soon or might it be dropped? |
@jadcoder this feature is ready. simply waiting to be merged by mainteners. |
|
Can we have this please? I thought n8n allows using my ChatGPT subscription for Agent nodes. |
|
such a wonderful feature, hope this gets progress. |
|
i want it!!!!!!! |
|
This PR currently has merge conflicts with Could you bring your branch up to date and resolve the conflicts? Either rebase onto Our contributing guide covers setting up the repo locally if you need it. Once you've pushed the resolved branch, this PR will automatically return to triage. If you run into trouble, reply here and we'll help. |
fb5d120 to
5782229
Compare
|
Rebased onto master, conflicts resolved and pushed. A few things changed while rebasing:
Typecheck, lint and the tests for the touched packages pass. |
Summary
Add OpenAI account OAuth support to the OpenAI Chat Model node while keeping the existing API key credential path unchanged.
This adds a dedicated
OpenAI Account (ChatGPT)credential and lets the OpenAI Chat Model node choose between API key and OAuth2 authentication. The OAuth2 credential stays aligned with n8n's existing credential UX: users still use the standard Connect button and popup/BroadcastChannel flow, while the popup hosts OpenAI's device-code login flow instead of asking users to paste an access token manually.OpenAI account tokens cannot call the public OpenAI Responses API directly, so OAuth-backed chat model execution uses the ChatGPT/Codex account backend and maps LangChain messages, streaming chunks, tool calls, tool results, model discovery, and reasoning effort into that protocol. API key execution continues to use the existing OpenAI/LangChain path.
OpenAI-specific credential UI is kept small: the OAuth redirect URL and HTTP Request domain restriction controls are hidden for this credential because they do not apply to the device-code flow.
Related Linear tickets, Github issues, and Community forum posts
No Linear ticket is linked because this is an external contribution and I do not have access to the n8n Linear workspace. The GitHub issue was auto-closed because n8n uses GitHub issues for bugs only, but it is kept here as public context for this contribution.
Review / Merge checklist
Backport to Beta,Backport to Stable, orBackport to v1(if the PR is an urgent fix that needs to be backported)Validation
pushd packages/nodes-base && pnpm test OpenAiApi.credentials.test.ts --runInBand && popdpushd packages/@n8n/nodes-langchain && pnpm test LmChatOpenAi.test.ts OpenAiAccountChatModel.test.ts LMChatOpenAi/methods/__tests__/loadModels.test.ts --runInBand && popdpushd packages/cli && pnpm test openai-oauth2-device.controller.test.ts oauth2-credential.controller.test.ts load-nodes-and-credentials.test.ts --runInBand && popdpushd packages/frontend/editor-ui && pnpm vitest run src/features/credentials/components/CredentialEdit/CredentialEdit.test.ts src/features/credentials/components/CredentialEdit/CredentialConfig.test.ts && popdpushd packages/nodes-base && NODE_OPTIONS=--max-old-space-size=8192 pnpm lint && popdpushd packages/@n8n/nodes-langchain && pnpm lint && popdpushd packages/cli && NODE_OPTIONS=--max-old-space-size=8192 pnpm lint && popdpushd packages/frontend/editor-ui && pnpm lint && popdpushd packages/cli && NODE_OPTIONS=--max-old-space-size=8192 pnpm typecheck && popdpushd packages/@n8n/nodes-langchain && pnpm typecheck && popdpushd packages/nodes-base && pnpm typecheck && popdpushd packages/frontend/editor-ui && pnpm typecheck && popdpnpm build > build.log 2>&1git diff --check origin/master..HEADhttp://localhost:5680