Skip to content

Commit 617e7f8

Browse files
Merge pull request #468 from leonardochaia/features/popups
Proposal: Add implicit flow through popup
2 parents 0933521 + 224f1a2 commit 617e7f8

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed

projects/lib/src/oauth-service.ts

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -781,23 +781,7 @@ export class OAuthService extends AuthConfig {
781781
this.removeSilentRefreshEventListener();
782782

783783
this.silentRefreshPostMessageEventListener = (e: MessageEvent) => {
784-
let expectedPrefix = '#';
785-
786-
if (this.silentRefreshMessagePrefix) {
787-
expectedPrefix += this.silentRefreshMessagePrefix;
788-
}
789-
790-
if (!e || !e.data || typeof e.data !== 'string') {
791-
return;
792-
}
793-
794-
const prefixedMessage: string = e.data;
795-
796-
if (!prefixedMessage.startsWith(expectedPrefix)) {
797-
return;
798-
}
799-
800-
const message = '#' + prefixedMessage.substr(expectedPrefix.length);
784+
const message = this.processMessageEventMessage(e);
801785

802786
this.tryLogin({
803787
customHashFragment: message,
@@ -895,6 +879,69 @@ export class OAuthService extends AuthConfig {
895879
.toPromise();
896880
}
897881

882+
public initImplicitFlowInPopup(options?: { height?: number, width?: number }) {
883+
options = options || {};
884+
return this.createLoginUrl(null, null, this.silentRefreshRedirectUri, false, {
885+
display: 'popup'
886+
}).then(url => {
887+
return new Promise((resolve, reject) => {
888+
let windowRef = window.open(url, '_blank', this.calculatePopupFeatures(options));
889+
890+
const cleanup = () => {
891+
window.removeEventListener('message', listener);
892+
windowRef.close();
893+
windowRef = null;
894+
};
895+
896+
const listener = (e: MessageEvent) => {
897+
const message = this.processMessageEventMessage(e);
898+
899+
this.tryLogin({
900+
customHashFragment: message,
901+
preventClearHashAfterLogin: true,
902+
}).then(() => {
903+
cleanup();
904+
resolve();
905+
}, err => {
906+
cleanup();
907+
reject(err);
908+
});
909+
};
910+
911+
window.addEventListener('message', listener);
912+
});
913+
});
914+
}
915+
916+
protected calculatePopupFeatures(options: { height?: number, width?: number }) {
917+
// Specify an static height and width and calculate centered position
918+
const height = options.height || 470;
919+
const width = options.width || 500;
920+
const left = (screen.width / 2) - (width / 2);
921+
const top = (screen.height / 2) - (height / 2);
922+
return `location=no,toolbar=no,width=${width},height=${height},top=${top},left=${left}`;
923+
}
924+
925+
protected processMessageEventMessage(e: MessageEvent) {
926+
let expectedPrefix = '#';
927+
928+
if (this.silentRefreshMessagePrefix) {
929+
expectedPrefix += this.silentRefreshMessagePrefix;
930+
}
931+
932+
if (!e || !e.data || typeof e.data !== 'string') {
933+
return;
934+
}
935+
936+
const prefixedMessage: string = e.data;
937+
938+
if (!prefixedMessage.startsWith(expectedPrefix)) {
939+
return;
940+
}
941+
942+
return '#' + prefixedMessage.substr(expectedPrefix.length);
943+
}
944+
898945
protected canPerformSessionCheck(): boolean {
899946
if (!this.sessionChecksEnabled) {
900947
return false;

0 commit comments

Comments
 (0)