Skip to content

Commit 9f99cdb

Browse files
fix: prevent auth redirect loop when user cancels or declines consent
Signed-off-by: Patrick Chin <8509935+thepatrickchin@users.noreply.github.com>
1 parent b1a5f6a commit 9f99cdb

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

packages/nvidia_nat_core/src/nat/front_ends/fastapi/html_snippets/auth_code_grant_cancelled.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
(function () {
4646
var returnTo = RETURN_URL_PLACEHOLDER;
4747
if (returnTo) {
48-
window.location.replace(returnTo);
48+
var url = new URL(returnTo);
49+
url.searchParams.set('oauth_auth_error', 'cancelled');
50+
window.location.replace(url.toString());
4951
} else {
5052
window.history.back();
5153
}
@@ -60,14 +62,13 @@
6062

6163

6264
def build_auth_redirect_cancelled_html(return_url: str | None = None) -> str:
63-
"""Build the authorization-cancelled HTML page.
64-
65-
Redirects back to the UI without the ``oauth_auth_completed`` query
66-
parameter so the UI's cancellation-message branch handles it.
65+
"""Build the same-page authorization-cancelled HTML page.
6766
6867
Args:
69-
return_url: The UI origin to navigate back to. Falls back to
70-
``window.history.back()`` when not provided.
68+
return_url: The URL to redirect to after cancellation. When
69+
provided the page navigates there immediately with an ``oauth_auth_error``
70+
query parameter so the UI can detect the cancellation and avoid a
71+
pre-auth redirect loop; otherwise it falls back to ``window.history.back()``.
7172
7273
Returns:
7374
An HTML string for the post-cancellation redirect page.

packages/nvidia_nat_core/src/nat/front_ends/fastapi/routes/websocket.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ async def _websocket_endpoint(websocket: WebSocket):
104104
token_store=worker._oauth_token_store,
105105
session_id=nat_session_id)
106106
handler.set_flow_handler(flow_handler)
107-
await flow_handler.pre_authenticate(worker._config.authentication)
107+
skip_pre_auth = websocket.query_params.get("skip_pre_auth") == "true"
108+
if not skip_pre_auth:
109+
try:
110+
await flow_handler.pre_authenticate(worker._config.authentication)
111+
except Exception as e:
112+
logger.info("Pre-authentication did not complete: %s", e)
108113
await handler.run()
109114

110115
return _websocket_endpoint

0 commit comments

Comments
 (0)