-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: oauth oidc popup-based redirect (#188)
- Loading branch information
Showing
3 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
class OAuth2OidcWindow { | ||
public readonly url: string; | ||
public readonly redirectOrigin: string; | ||
|
||
private window: WindowProxy | null = null | ||
private promise: Promise<boolean> | null = null | ||
private _iid: number | null = null; | ||
|
||
constructor(url: string, redirectOrigin: string) { | ||
this.url = url; | ||
this.redirectOrigin = redirectOrigin; | ||
} | ||
|
||
open() { | ||
const { url } = this; | ||
this.window = window.open(url, 'oauth2-oidc', 'popup=1,width=800,height=600'); | ||
} | ||
|
||
close() { | ||
this.cancel(); | ||
this.window?.close(); | ||
} | ||
|
||
poll() { | ||
this.promise = new Promise((resolve, reject) => { | ||
this._iid = window.setInterval(() => { | ||
try { | ||
const popup = this.window; | ||
if (!popup || popup.closed !== false) { | ||
this.close(); | ||
reject(new Error('The popup was closed by user')); | ||
return; | ||
} | ||
if (popup.location.origin !== this.redirectOrigin) { | ||
return; | ||
} | ||
resolve(true); | ||
this.close(); | ||
} catch (error) { | ||
/* | ||
* Ignore DOMException: Blocked a frame with origin from accessing a | ||
* cross-origin frame. | ||
*/ | ||
} | ||
}, 500); | ||
}); | ||
} | ||
|
||
cancel() { | ||
if (this._iid) { | ||
window.clearInterval(this._iid); | ||
this._iid = null; | ||
} | ||
} | ||
|
||
then(onSuccess: () => void) { | ||
return this.promise?.then(onSuccess); | ||
} | ||
|
||
catch(onError: (err: Error) => void) { | ||
return this.promise?.catch(onError); | ||
} | ||
|
||
static open(url: string, redirectOrigin: string) { | ||
const popup = new OAuth2OidcWindow(url, redirectOrigin); | ||
popup.open(); | ||
popup.poll(); | ||
return popup; | ||
} | ||
} | ||
|
||
export default OAuth2OidcWindow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters