diff --git a/src/app/components/chat/chat.component.spec.ts b/src/app/components/chat/chat.component.spec.ts index 7e8c4f88..66a444c6 100644 --- a/src/app/components/chat/chat.component.spec.ts +++ b/src/app/components/chat/chat.component.spec.ts @@ -2366,6 +2366,40 @@ describe('ChatComponent', () => { }); }); + 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); + } + }); + }); + describe('updateSystemInstructionFlags', () => { function llmSpan( eventId: string, startTime: number, systemInstruction: string): any { diff --git a/src/app/components/chat/chat.component.ts b/src/app/components/chat/chat.component.ts index 3f3505aa..07d6c296 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 @@ -1568,6 +1577,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 } @@ -2416,10 +2426,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.'); }