From d98c21947b7d989735d17d527f56e28f5eff3eed Mon Sep 17 00:00:00 2001 From: "Tianlang (Jackie) Chen" <305351710+jackiectl2@users.noreply.github.com> Date: Sat, 24 Oct 2026 01:12:19 -0400 Subject: [PATCH] fix(chat): surface OAuth authorization errors Report callback failures to the opener and show errors from popup and same-window OAuth redirects in the chat UI. Signed-off-by: Tianlang (Jackie) Chen <305351710+jackiectl2@users.noreply.github.com> --- .../components/chat/chat.component.spec.ts | 34 +++++++++++++++++++ src/app/components/chat/chat.component.ts | 15 +++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/app/components/chat/chat.component.spec.ts b/src/app/components/chat/chat.component.spec.ts index 33e6e4a8..7201d3bb 100644 --- a/src/app/components/chat/chat.component.spec.ts +++ b/src/app/components/chat/chat.component.spec.ts @@ -2364,4 +2364,38 @@ describe('ChatComponent', () => { expect(component.shouldShowEvent(stateUpdate)).toBeTrue(); }); }); + + describe('OAuth popup', () => { + it('rejects an authorization error returned by the popup', async () => { + mockSafeValuesService.windowOpen.and.returnValue({} as Window); + const authorization = component['openOAuthPopup']('https://example.com/authorize'); + + window.dispatchEvent(new MessageEvent('message', { + origin: window.location.origin, + data: {authError: 'Access denied'}, + })); + + await expectAsync(authorization).toBeRejectedWith('Access denied'); + }); + + it('shows a provider error after a same-window redirect', async () => { + const originalUrl = window.location.href; + const callbackUrl = new URL(originalUrl); + callbackUrl.searchParams.set('error', 'access_denied'); + callbackUrl.searchParams.set('error_description', 'The requested scope was denied'); + + try { + window.history.replaceState({}, '', callbackUrl.toString()); + const redirected = TestBed.createComponent(ChatComponent); + redirected.detectChanges(); + await redirected.whenStable(); + + expect(mockSnackBar.open).toHaveBeenCalledWith( + 'Authorization failed: The requested scope was denied', 'OK'); + redirected.destroy(); + } finally { + window.history.replaceState({}, '', originalUrl); + } + }); + }); }); diff --git a/src/app/components/chat/chat.component.ts b/src/app/components/chat/chat.component.ts index d1b7a2d3..cc08cd01 100644 --- a/src/app/components/chat/chat.component.ts +++ b/src/app/components/chat/chat.component.ts @@ -908,6 +908,15 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy { window.opener?.postMessage({ authResponseUrl }, window.origin); // Close the popup window.close(); + } else if (searchParams.has('error')) { + const authError = searchParams.get('error_description') || + searchParams.get('error') || 'Authorization failed'; + if (window.opener) { + window.opener.postMessage({authError}, window.origin); + window.close(); + } else { + this.openSnackBar(`Authorization failed: ${authError}`, 'OK'); + } } else if (searchParams.has('user_id_validation_state')) { // Agent Identity Auth Manager (3LO): the connector redirects the consent // popup here (the agent's continue_uri) after the user authorizes. Relay @@ -1567,6 +1576,7 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy { }) .catch((error) => { console.error('OAuth Error:', error); + this.openSnackBar(`Authorization failed: ${error}`, 'OK'); }); break; // Handle one OAuth at a time } @@ -2415,10 +2425,13 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy { if (event.origin !== window.location.origin) { return; // Ignore messages from unknown sources } - const { authResponseUrl } = event.data; + const { authResponseUrl, authError } = event.data || {}; if (authResponseUrl) { resolve(authResponseUrl); window.removeEventListener('message', listener); + } else if (authError) { + reject(authError); + window.removeEventListener('message', listener); } else { console.error('OAuth popup returned no authResponseUrl.'); }