Skip to content

Add option to enable redirect-based OAuth flow#1835

Open
thepatrickchin wants to merge 13 commits intoNVIDIA:developfrom
thepatrickchin:feat/redirect-auth
Open

Add option to enable redirect-based OAuth flow#1835
thepatrickchin wants to merge 13 commits intoNVIDIA:developfrom
thepatrickchin:feat/redirect-auth

Conversation

@thepatrickchin
Copy link
Copy Markdown
Member

@thepatrickchin thepatrickchin commented Apr 2, 2026

Description

This PR adds a use_redirect_auth option in auth provider configurations to enable redirect-based OAuth flows while preserving popup flow by default.

This features depends on the following NAT-UI PR: NVIDIA/NeMo-Agent-Toolkit-UI#127

Closes #1834

By Submitting this PR I confirm:

  • I am familiar with the Contributing Guidelines.
  • We require that all contributors "sign-off" on their commits. This certifies that the contribution is your original work, or you have rights to submit it under the same license, or a compatible license.
    • Any contribution which contains commits that are not Signed-Off will not be accepted.
  • When the PR is ready for review, new or existing tests cover these changes.
  • When the PR is ready for review, the documentation is up to date with these changes.

Summary by CodeRabbit

  • New Features

    • Configurable OAuth flow mode (use_redirect_auth) to choose redirect vs popup; redirect mode supports optional return-URL and returns HTML for success/error/cancel flows.
  • Bug Fixes

    • Consent UI now uses explicit Authorize/Cancel buttons so cancellations reliably return an access_denied response to clients.
  • Documentation

    • API auth docs updated with use_redirect_auth and its default (popup).
  • Tests

    • Added tests covering popup vs redirect flows, return-URL behavior, origin allowlisting, and cancel/error UX.

@thepatrickchin thepatrickchin requested a review from a team as a code owner April 2, 2026 06:05
@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot Bot commented Apr 2, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 2, 2026

Walkthrough

Adds an opt-in redirect-based OAuth2 authorization flow via a new boolean config use_redirect_auth (default False). Updates provider config, interactive prompt model, WebSocket and HTTP handlers, origin allowlist logic, HTML success/error/cancel snippet builders, example OAuth2 server patch/Dockerfile, and tests to support popup vs redirect behaviors and propagate an allowed-origin-derived return_url.

Changes

Cohort / File(s) Summary
Documentation
docs/source/components/auth/api-authentication.md
Documented new use_redirect_auth field and default (False, popup).
Provider config & example
packages/nvidia_nat_core/src/nat/authentication/oauth2/oauth2_auth_code_flow_provider_config.py, examples/front_ends/simple_auth/src/nat_simple_auth/configs/config.yml
Added exported boolean use_redirect_auth: bool = False to provider config; example config sets it to false.
Interactive model & tests
packages/nvidia_nat_core/src/nat/data_models/interactive.py, packages/nvidia_nat_core/tests/nat/builder/test_interactive.py
Added _HumanPromptOAuthConsent.use_redirect: bool = False and tests for default/explicit behavior and text passthrough.
WebSocket flow handler & tests
packages/nvidia_nat_core/src/nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py, packages/nvidia_nat_core/tests/nat/front_ends/auth_flow_handlers/test_websocket_flow_handler.py
FlowState gained return_url; handler ctor accepts return_url; enforces presence when use_redirect_auth is true; passes use_redirect into prompts; tests added for popup, redirect, and missing-return-url error.
FastAPI routes & origin handling
packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py, packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/websocket.py, packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_websocket_route_origin.py
Added _is_origin_allowed(); compute return_url from Origin when allowed and pass into WebSocket handler; updated auth callback to return 200 HTML using new builders for success/error/cancel when redirect mode enabled.
HTML snippet builders & tests
packages/nvidia_nat_core/src/nat/front_ends/fastapi/html_snippets/auth_code_grant_success.py, .../auth_code_grant_error.py, .../auth_code_grant_cancelled.py, packages/nvidia_nat_core/tests/nat/front_ends/fastapi/*
Added popup constants and builder functions that JSON-encode and HTML/JS-escape optional return_url; builders produce redirect-or-history-fallback HTML for success/error/cancel; added tests validating embedding, escaping, and script-injection resilience.
Example OAuth2 server patch & Dockerfile
examples/front_ends/simple_auth/patches/oauth2-server.patch, examples/front_ends/simple_auth/Dockerfile
Apply patch during image build to require explicit Authorize/Cancel buttons so cancel yields error=access_denied; Dockerfile copies and applies the patch.
New/updated tests (routes & integration)
packages/nvidia_nat_core/tests/... (multiple files)
Added/updated tests covering origin allowlist, auth redirect route behaviors (success/error/cancel), websocket flows (popup vs redirect), HTML snippet builders, and model defaults.

Sequence Diagrams

sequenceDiagram
    participant Browser as Browser (Client)
    participant UI as Frontend UI
    participant WS as WebSocket Handler
    participant OAuth as OAuth Server

    rect rgba(0,150,100,0.5)
        note over Browser,UI: Popup-based OAuth (use_redirect_auth: false)
    end

    Browser->>WS: Open WebSocket
    WS->>UI: Send OAuth prompt (use_redirect=false)
    UI->>UI: Open authorization URL in popup
    Browser->>OAuth: Authorize (popup)
    OAuth-->>Browser: Redirect callback (code)
    Browser->>WS: GET /auth/redirect?code=...
    WS->>OAuth: Exchange code for token
    OAuth-->>WS: Return token
    WS->>UI: Send success message (popup closes)
Loading
sequenceDiagram
    participant Browser as Browser (Client)
    participant UI as Frontend UI
    participant WS as WebSocket Handler
    participant OAuth as OAuth Server

    rect rgba(100,0,150,0.5)
        note over Browser,OAuth: Redirect-based OAuth (use_redirect_auth: true)
    end

    Browser->>WS: Open WebSocket
    WS->>UI: Send OAuth prompt (use_redirect=true) + return_url
    UI->>Browser: Full-page redirect to authorization URL
    Browser->>OAuth: Authorize (redirect)
    OAuth-->>Browser: Redirect callback (code)
    Browser->>WS: GET /auth/redirect?code=...
    WS->>OAuth: Exchange code for token
    OAuth-->>WS: Return token
    WS->>Browser: Return success HTML (redirect to return_url with marker)
    Browser->>Browser: Resume original session (redirect target)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 76.36% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and concisely summarizes the main change: adding a configuration option for redirect-based OAuth flows, which is the core objective of the changeset.
Linked Issues check ✅ Passed The PR fully implements the requirements from issue #1834: adds use_redirect_auth configuration option (false by default), supports redirect-based OAuth flow as alternative to popup-based flow, includes proper error handling, and maintains backward compatibility.
Out of Scope Changes check ✅ Passed All changes are within scope: config field additions, OAuth flow handler modifications, HTML templates for redirect-based auth, test coverage, and documentation updates all directly support the redirect-auth feature requirement.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@thepatrickchin thepatrickchin changed the title Feat/redirect auth Add option to enable redirect-based OAuth flow Apr 2, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@packages/nvidia_nat_core/src/nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py`:
- Around line 120-121: The current WebSocketFlowHandler starts a same-tab
redirect flow even when config.use_popup_auth is False and self._return_url is
None; add a guard in the handler before constructing FlowState to fail fast: if
config.use_popup_auth is False and self._return_url is None, raise a clear
exception (or return an HTTP/WebSocket error) so the flow does not start without
a validated return URL; update the block where return_url is set and
FlowState(config=config, return_url=return_url) is created (references:
FlowState, config.use_popup_auth, self._return_url, websocket_flow_handler) to
implement this validation and error handling.

In `@packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py`:
- Around line 107-110: The redirect-mode failure paths currently leave the
browser on a raw /auth/redirect error page; update the exception handlers in the
auth redirect route (the function handling flow_state and the except blocks for
OAuthError, httpx.HTTPError, and generic Exception) to mirror the success
behavior: when flow_state.config exists and flow_state.config.use_popup_auth is
False use build_auth_redirect_success_html (or a new
build_auth_redirect_error_html) to construct a redirect-capable error HTML using
flow_state.return_url, otherwise fall back to the existing
AUTH_REDIRECT_SUCCESS_HTML/ERROR constant; ensure the same flow_state,
use_popup_auth, build_auth_redirect_* helper and AUTH_REDIRECT_* constants are
referenced so failures perform a UI redirect like successes.

In `@packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/websocket.py`:
- Around line 75-81: The origin membership check currently uses a literal
comparison against worker.front_end_config.cors.allow_origins which fails when
allow_origins contains the wildcard "*" and ignores allow_origin_regex; update
the logic before constructing WebSocketAuthenticationFlowHandler so that: if
allow_origins includes "*" accept any non-empty origin, otherwise check origin
equality against the list; additionally, if
worker.front_end_config.cors.allow_origin_regex is set, test origin against that
regex (treat a matching regex as allowed). Use the same variables (origin,
allowed_origins, allow_origin_regex) and pass the computed return_url into
WebSocketAuthenticationFlowHandler as before.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7940c9bf-52f2-4d57-886c-0a4ab5fae921

📥 Commits

Reviewing files that changed from the base of the PR and between 2490e5d and 33db54c.

📒 Files selected for processing (15)
  • docs/source/components/auth/api-authentication.md
  • examples/front_ends/simple_auth/Dockerfile
  • examples/front_ends/simple_auth/patches/oauth2-server.patch
  • examples/front_ends/simple_auth/src/nat_simple_auth/configs/config.yml
  • packages/nvidia_nat_core/src/nat/authentication/oauth2/oauth2_auth_code_flow_provider_config.py
  • packages/nvidia_nat_core/src/nat/data_models/interactive.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/html_snippets/auth_code_grant_cancelled.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/html_snippets/auth_code_grant_success.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/websocket.py
  • packages/nvidia_nat_core/tests/nat/builder/test_interactive.py
  • packages/nvidia_nat_core/tests/nat/front_ends/auth_flow_handlers/test_websocket_flow_handler.py
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_auth_code_grant_cancelled.py
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_auth_code_grant_success.py

Comment thread packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py (1)

84-123: 🛠️ Refactor suggestion | 🟠 Major

Use logger.exception() to preserve stack traces in these catch-and-respond blocks.

The three exception handlers (lines 85, 99, 112) catch exceptions without re-raising them, so they must use logger.exception() instead of logger.error() to capture the full stack trace information. This is mandatory per coding guidelines.

♻️ Suggested fix
-        except OAuthError as e:
-            logger.error("OAuth error during token exchange for state %s: %s (%s)", state, e.error, e.description)
+        except OAuthError as e:
+            logger.exception("OAuth error during token exchange for state %s: %s (%s)", state, e.error, e.description)
             if not flow_state.future.done():
                 flow_state.future.set_exception(
                     RuntimeError(f"Authorization server rejected request: {e.error} ({e.description})"))
             if flow_state.config and flow_state.config.use_redirect_auth:
                 error_html = build_auth_redirect_error_html(flow_state.return_url)
@@
-        except httpx.HTTPError as e:
-            logger.error("Network error during token fetch for state %s: %s", state, e)
+        except httpx.HTTPError as e:
+            logger.exception("Network error during token fetch for state %s: %s", state, e)
             if not flow_state.future.done():
                 flow_state.future.set_exception(RuntimeError(f"Network error during token fetch: {e}"))
             if flow_state.config and flow_state.config.use_redirect_auth:
                 error_html = build_auth_redirect_error_html(flow_state.return_url)
@@
-        except Exception as e:
-            logger.error("Unexpected error during authentication for state %s: %s", state, e)
+        except Exception as e:
+            logger.exception("Unexpected error during authentication for state %s: %s", state, e)
             if not flow_state.future.done():
                 flow_state.future.set_exception(RuntimeError(f"Authentication failed: {e}"))
             if flow_state.config and flow_state.config.use_redirect_auth:
                 error_html = build_auth_redirect_error_html(flow_state.return_url)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py` around
lines 84 - 123, Replace the three logger.error calls in the exception handlers
that catch OAuthError, httpx.HTTPError, and the generic Exception with
logger.exception so the stack traces are preserved; update the handlers around
the symbols OAuthError, httpx.HTTPError, and the final except Exception (the
blocks that reference flow_state, build_auth_redirect_error_html, and
AUTH_REDIRECT_ERROR_HTML) to call logger.exception with the same message format
and parameters instead of logger.error, leaving the rest of the error handling
(setting flow_state.future, building error_html, and returning the HTMLResponse)
unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py`:
- Around line 53-71: The code currently treats every OAuth callback error as a
user cancellation; change the logic to only treat error == "access_denied" as a
cancellation and keep the existing cancellation responses
(build_auth_redirect_cancelled_html, AUTH_REDIRECT_CANCELLED_POPUP_HTML,
flow_state.future.set_exception(...)) only in that branch; for all other error
values log the error (include error_description), set flow_state.future with a
clear exception mentioning the actual error (e.g. RuntimeError(f"OAuth error:
{error} ({error_description})")), call worker._remove_flow(state) as now, and
forward the request to the normal error UX path instead of the cancelled HTML
(i.e. do not return the cancelled HTML for non-access_denied errors).

---

Outside diff comments:
In `@packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py`:
- Around line 84-123: Replace the three logger.error calls in the exception
handlers that catch OAuthError, httpx.HTTPError, and the generic Exception with
logger.exception so the stack traces are preserved; update the handlers around
the symbols OAuthError, httpx.HTTPError, and the final except Exception (the
blocks that reference flow_state, build_auth_redirect_error_html, and
AUTH_REDIRECT_ERROR_HTML) to call logger.exception with the same message format
and parameters instead of logger.error, leaving the rest of the error handling
(setting flow_state.future, building error_html, and returning the HTMLResponse)
unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: afa34493-7660-47ca-8f78-9d28245e697c

📥 Commits

Reviewing files that changed from the base of the PR and between 33db54c and ce7ec06.

📒 Files selected for processing (12)
  • docs/source/components/auth/api-authentication.md
  • examples/front_ends/simple_auth/src/nat_simple_auth/configs/config.yml
  • packages/nvidia_nat_core/src/nat/authentication/oauth2/oauth2_auth_code_flow_provider_config.py
  • packages/nvidia_nat_core/src/nat/data_models/interactive.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/html_snippets/auth_code_grant_error.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/websocket.py
  • packages/nvidia_nat_core/tests/nat/builder/test_interactive.py
  • packages/nvidia_nat_core/tests/nat/front_ends/auth_flow_handlers/test_websocket_flow_handler.py
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_auth_code_grant_error.py
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_websocket_route_origin.py
✅ Files skipped from review due to trivial changes (2)
  • docs/source/components/auth/api-authentication.md
  • packages/nvidia_nat_core/tests/nat/builder/test_interactive.py
🚧 Files skipped from review as they are similar to previous changes (3)
  • examples/front_ends/simple_auth/src/nat_simple_auth/configs/config.yml
  • packages/nvidia_nat_core/src/nat/authentication/oauth2/oauth2_auth_code_flow_provider_config.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py

Comment thread packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py (1)

101-140: ⚠️ Potential issue | 🟠 Major

Use logger.exception() to preserve full stack traces in exception handlers.

Lines 102, 116, and 129 catch exceptions and log them with logger.error() but do not re-raise. Per coding guidelines, you must use logger.exception() to capture the full stack trace. The broad except Exception at line 128 (Ruff BLE001) is a secondary concern—prioritize fixing the logging pattern first.

Suggested fix
         except OAuthError as e:
-            logger.error("OAuth error during token exchange for state %s: %s (%s)", state, e.error, e.description)
+            logger.exception("OAuth error during token exchange for state %s: %s (%s)", state, e.error, e.description)
             if not flow_state.future.done():
                 flow_state.future.set_exception(
                     RuntimeError(f"Authorization server rejected request: {e.error} ({e.description})"))
             if flow_state.config and flow_state.config.use_redirect_auth:
                 error_html = build_auth_redirect_error_html(flow_state.return_url)
             else:
                 error_html = AUTH_REDIRECT_ERROR_HTML
             return HTMLResponse(content=error_html,
                                 status_code=200,
                                 headers={
                                     "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-cache"
                                 })
         except httpx.HTTPError as e:
-            logger.error("Network error during token fetch for state %s: %s", state, e)
+            logger.exception("Network error during token fetch for state %s: %s", state, e)
             if not flow_state.future.done():
                 flow_state.future.set_exception(RuntimeError(f"Network error during token fetch: {e}"))
             if flow_state.config and flow_state.config.use_redirect_auth:
                 error_html = build_auth_redirect_error_html(flow_state.return_url)
             else:
                 error_html = AUTH_REDIRECT_ERROR_HTML
             return HTMLResponse(content=error_html,
                                 status_code=200,
                                 headers={
                                     "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-cache"
                                 })
         except Exception as e:
-            logger.error("Unexpected error during authentication for state %s: %s", state, e)
+            logger.exception("Unexpected error during authentication for state %s: %s", state, e)
             if not flow_state.future.done():
                 flow_state.future.set_exception(RuntimeError(f"Authentication failed: {e}"))
             if flow_state.config and flow_state.config.use_redirect_auth:
                 error_html = build_auth_redirect_error_html(flow_state.return_url)
             else:
                 error_html = AUTH_REDIRECT_ERROR_HTML
             return HTMLResponse(content=error_html,
                                 status_code=200,
                                 headers={
                                     "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-cache"
                                 })
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py` around
lines 101 - 140, The exception handlers in the auth route use logger.error which
loses stack traces; update the three handlers catching OAuthError,
httpx.HTTPError, and the generic Exception in the function handling the token
exchange so they call logger.exception(...) instead of logger.error(...),
leaving the message text and interpolation symbols (state, e, e.error,
e.description) intact; keep the existing flow_state.future.set_exception(...)
behavior and the HTMLResponse construction unchanged—only replace the logging
calls to use logger.exception to preserve full stack traces for OAuthError,
httpx.HTTPError, and the broad Exception handler.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py`:
- Around line 60-71: The code currently chooses redirect-cancel HTML whenever
flow_state.return_url is set, which ignores the configured popup vs redirect
mode; update the branching to use the flow state's mode flag
(flow_state.use_redirect_auth) instead of presence of flow_state.return_url.
Specifically, in the error == "access_denied" block, return
build_auth_redirect_cancelled_html(flow_state.return_url) only when
flow_state.use_redirect_auth is True (and return_url exists), otherwise return
AUTH_REDIRECT_CANCELLED_POPUP_HTML; keep the existing future.set_exception
behavior unchanged and reference flow_state, flow_state.use_redirect_auth,
flow_state.return_url, build_auth_redirect_cancelled_html, and
AUTH_REDIRECT_CANCELLED_POPUP_HTML when making the fix.

In
`@packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_auth_redirect_route.py`:
- Around line 77-96: Add a new test function (mirroring
test_access_denied_popup_returns_cancelled_html) that constructs
FlowState(config=_POPUP_CONFIG, return_url=_RETURN_URL), obtains a worker via
_make_worker(flow_state), calls _get with {"state": "teststate", "error":
"access_denied"}, and asserts response.status_code == 200 and that
"AUTH_CANCELLED" is present in response.text while "AUTH_ERROR" and any redirect
markers like _RETURN_URL.replace("/", "\\u002f") or "oauth_auth_completed" are
not; use the same helpers (_make_worker, _get) and constants (_POPUP_CONFIG,
_RETURN_URL, FlowState) to ensure popup mode remains independent of return_url.

---

Outside diff comments:
In `@packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py`:
- Around line 101-140: The exception handlers in the auth route use logger.error
which loses stack traces; update the three handlers catching OAuthError,
httpx.HTTPError, and the generic Exception in the function handling the token
exchange so they call logger.exception(...) instead of logger.error(...),
leaving the message text and interpolation symbols (state, e, e.error,
e.description) intact; keep the existing flow_state.future.set_exception(...)
behavior and the HTMLResponse construction unchanged—only replace the logging
calls to use logger.exception to preserve full stack traces for OAuthError,
httpx.HTTPError, and the broad Exception handler.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b57f31f0-f84d-4b39-ba5d-4ed124604737

📥 Commits

Reviewing files that changed from the base of the PR and between ce7ec06 and c792ba3.

📒 Files selected for processing (3)
  • examples/front_ends/simple_auth/src/nat_simple_auth/configs/config.yml
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_auth_redirect_route.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • examples/front_ends/simple_auth/src/nat_simple_auth/configs/config.yml

Comment thread packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py
Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
- "same-page" may be misleading as the popup is what keeps the user on the same page.

Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
- Fail fast when redirect auth has no validated return URL.
- Handle redirect-mode failures the same way as redirect-mode success.
- Properly handle wildcard "*" in allow_origins

Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
- Only treat access_denied as a user cancellation.

Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
- Gate cancellation UX with use_redirect_auth, not return_url.
- Add a popup-mode regression test when return_url is present.

Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@packages/nvidia_nat_core/tests/nat/front_ends/auth_flow_handlers/test_websocket_flow_handler.py`:
- Around line 173-179: Replace the NON-BREAKING HYPHEN in the comment "all
flow‑state cleaned up" with a regular HYPHEN-MINUS so tooling/encoding won't
break; locate the comment near the assertion checking worker._outstanding_flows
== {} in the test_websocket_flow_handler test and update the comment text to
"all flow-state cleaned up" (ensure file saved with UTF-8 and run tests/lint to
confirm no stray non-ASCII hyphen remains).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 84c61824-fad3-4044-8db0-d56a251fa79f

📥 Commits

Reviewing files that changed from the base of the PR and between c792ba3 and 1221391.

📒 Files selected for processing (19)
  • docs/source/components/auth/api-authentication.md
  • examples/front_ends/simple_auth/Dockerfile
  • examples/front_ends/simple_auth/patches/oauth2-server.patch
  • examples/front_ends/simple_auth/src/nat_simple_auth/configs/config.yml
  • packages/nvidia_nat_core/src/nat/authentication/oauth2/oauth2_auth_code_flow_provider_config.py
  • packages/nvidia_nat_core/src/nat/data_models/interactive.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/html_snippets/auth_code_grant_cancelled.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/html_snippets/auth_code_grant_error.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/html_snippets/auth_code_grant_success.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/auth.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/websocket.py
  • packages/nvidia_nat_core/tests/nat/builder/test_interactive.py
  • packages/nvidia_nat_core/tests/nat/front_ends/auth_flow_handlers/test_websocket_flow_handler.py
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_auth_code_grant_cancelled.py
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_auth_code_grant_error.py
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_auth_code_grant_success.py
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_auth_redirect_route.py
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_websocket_route_origin.py
✅ Files skipped from review due to trivial changes (4)
  • docs/source/components/auth/api-authentication.md
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_websocket_route_origin.py
  • packages/nvidia_nat_core/src/nat/authentication/oauth2/oauth2_auth_code_flow_provider_config.py
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_auth_code_grant_error.py
🚧 Files skipped from review as they are similar to previous changes (8)
  • packages/nvidia_nat_core/src/nat/data_models/interactive.py
  • examples/front_ends/simple_auth/Dockerfile
  • examples/front_ends/simple_auth/patches/oauth2-server.patch
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/html_snippets/auth_code_grant_success.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/html_snippets/auth_code_grant_error.py
  • packages/nvidia_nat_core/src/nat/front_ends/fastapi/html_snippets/auth_code_grant_cancelled.py
  • examples/front_ends/simple_auth/src/nat_simple_auth/configs/config.yml
  • packages/nvidia_nat_core/tests/nat/front_ends/fastapi/test_auth_code_grant_cancelled.py

Comment on lines +173 to 179
assert received_messages[0].use_redirect is False, "Default use_redirect_auth should emit use_redirect=False"
token_val = ctx.headers["Authorization"].split()[1]
assert token_val in mock_server.tokens, "token not issued by mock server"

# all flow‑state cleaned up
assert worker._outstanding_flows == {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix ambiguous hyphen character in comment.

Line 177 contains a NON-BREAKING HYPHEN () instead of a regular HYPHEN-MINUS (-). This can cause issues with tooling and should be replaced.

🔧 Proposed fix
-    # all flow‑state cleaned up
+    # all flow-state cleaned up
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
assert received_messages[0].use_redirect is False, "Default use_redirect_auth should emit use_redirect=False"
token_val = ctx.headers["Authorization"].split()[1]
assert token_val in mock_server.tokens, "token not issued by mock server"
# all flowstate cleaned up
assert worker._outstanding_flows == {}
assert received_messages[0].use_redirect is False, "Default use_redirect_auth should emit use_redirect=False"
token_val = ctx.headers["Authorization"].split()[1]
assert token_val in mock_server.tokens, "token not issued by mock server"
# all flow-state cleaned up
assert worker._outstanding_flows == {}
🧰 Tools
🪛 Ruff (0.15.10)

[warning] 177-177: Comment contains ambiguous (NON-BREAKING HYPHEN). Did you mean - (HYPHEN-MINUS)?

(RUF003)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@packages/nvidia_nat_core/tests/nat/front_ends/auth_flow_handlers/test_websocket_flow_handler.py`
around lines 173 - 179, Replace the NON-BREAKING HYPHEN in the comment "all
flow‑state cleaned up" with a regular HYPHEN-MINUS so tooling/encoding won't
break; locate the comment near the assertion checking worker._outstanding_flows
== {} in the test_websocket_flow_handler test and update the comment text to
"all flow-state cleaned up" (ensure file saved with UTF-8 and run tests/lint to
confirm no stray non-ASCII hyphen remains).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add option to enable redirect-based OAuth flow

1 participant