Skip to content

Added popup related error handling for implicit grant #647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2020
Merged
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
4 changes: 3 additions & 1 deletion projects/lib/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export type EventType =
| 'session_changed'
| 'session_error'
| 'session_terminated'
| 'logout';
| 'logout'
| 'popup_closed'
| 'popup_blocked';

export abstract class OAuthEvent {
constructor(readonly type: EventType) {}
Expand Down
64 changes: 43 additions & 21 deletions projects/lib/src/oauth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,27 +926,49 @@ export class OAuthService extends AuthConfig implements OnDestroy {
display: 'popup'
}).then(url => {
return new Promise((resolve, reject) => {
/**
* Error handling section
*/
const checkForPopupClosedInterval = 500;
let windowRef = window.open(url, '_blank', this.calculatePopupFeatures(options));
let checkForPopupClosedTimer: any;
const checkForPopupClosed = () => {
if (!windowRef || windowRef.closed) {
cleanup();
reject(new OAuthErrorEvent('popup_closed', {}));
}
};
if (!windowRef) {
reject(new OAuthErrorEvent('popup_blocked', {}));
} else {
checkForPopupClosedTimer = window.setInterval(checkForPopupClosed, checkForPopupClosedInterval);
}

const cleanup = () => {
window.clearInterval(checkForPopupClosedTimer);
window.removeEventListener('message', listener);
windowRef.close();
if (windowRef !== null) {
windowRef.close();
}
windowRef = null;
};

const listener = (e: MessageEvent) => {
const message = this.processMessageEventMessage(e);

this.tryLogin({
customHashFragment: message,
preventClearHashAfterLogin: true,
}).then(() => {
cleanup();
resolve();
}, err => {
cleanup();
reject(err);
});
if (message && message !== null) {
this.tryLogin({
customHashFragment: message,
preventClearHashAfterLogin: true,
}).then(() => {
cleanup();
resolve();
}, err => {
cleanup();
reject(err);
});
} else {
console.log('false event firing');
}
};

window.addEventListener('message', listener);
Expand Down Expand Up @@ -1264,7 +1286,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
}

return url;

}

initImplicitFlowInternal(
Expand Down Expand Up @@ -1503,32 +1525,32 @@ export class OAuthService extends AuthConfig implements OnDestroy {
(tokenResponse) => {
this.debug('refresh tokenResponse', tokenResponse);
this.storeAccessTokenResponse(
tokenResponse.access_token,
tokenResponse.refresh_token,
tokenResponse.access_token,
tokenResponse.refresh_token,
tokenResponse.expires_in,
tokenResponse.scope);

if (this.oidc && tokenResponse.id_token) {
this.processIdToken(tokenResponse.id_token, tokenResponse.access_token).
this.processIdToken(tokenResponse.id_token, tokenResponse.access_token).
then(result => {
this.storeIdToken(result);

this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
this.eventsSubject.next(new OAuthSuccessEvent('token_refreshed'));

resolve(tokenResponse);
})
.catch(reason => {
this.eventsSubject.next(new OAuthErrorEvent('token_validation_error', reason));
console.error('Error validating tokens');
console.error(reason);

reject(reason);
});
} else {
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
this.eventsSubject.next(new OAuthSuccessEvent('token_refreshed'));

resolve(tokenResponse);
}
},
Expand Down Expand Up @@ -1688,7 +1710,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
): boolean {
const savedNonce = this._storage.getItem('nonce');
if (savedNonce !== nonceInState) {

const err = 'Validating access_token failed, wrong state/nonce.';
console.error(err, savedNonce, nonceInState);
return false;
Expand Down