Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/app/components/chat/chat.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 14 additions & 1 deletion src/app/components/chat/chat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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.');
}
Expand Down
Loading