Skip to content

Commit

Permalink
Make popup more accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
hwhmeikle committed May 25, 2022
1 parent 7e61ee7 commit 66239bf
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dist
package-lock.json
.nyc_output
coverage/
*.tsbuildinfo
*.tsbuildinfo
.vscode
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

1 change: 0 additions & 1 deletion .vscode/spellright.dict

This file was deleted.

37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
# authsignal-browser

## API

### challengeWithPopup

You will need to add the following CSS to your application if using this method:

```css
.dialog-container,
.dialog-overlay {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
}

.dialog-container {
z-index: 2;
display: flex;
}

.dialog-container[aria-hidden="true"] {
display: none;
}

.dialog-overlay {
background-color: rgba(43, 46, 56, 0.9);
}

.dialog-content {
margin: auto;
z-index: 2;
position: relative;
background-color: white;
}
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"dependencies": {
"@fingerprintjs/fingerprintjs": "^3.3.3",
"basiclightbox": "^5.0.4",
"a11y-dialog": "^7.4.0",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand Down
15 changes: 7 additions & 8 deletions src/Authsignal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as basicLightbox from "basiclightbox";
import * as FingerprintJS from "@fingerprintjs/fingerprintjs";

import {setCookie, generateId, getCookieDomain, getCookie, getHostWithProtocol, reformatDate} from "./helpers";
Expand All @@ -10,6 +9,7 @@ import {
RegisterIdentityRequest,
UserProps,
} from "./types";
import {PopupHandler} from "./PopupHandler";

export function authsignalClient(publishableKey: string, options?: AuthsignalOptions): AuthsignalClient {
const client = new AuthsignalClient();
Expand Down Expand Up @@ -77,19 +77,18 @@ export class AuthsignalClient {
return {idCookie: newId, generated: true};
}

handleChallenge(challenge: AuthsignalChallenge): Promise<boolean> {
const lightbox = basicLightbox.create(
`<iframe name="authsignal" src="${challenge.challengeUrl}" width="600" height="100%" frameborder="0"></iframe>`
);
challengeWithPopup({challengeUrl}: AuthsignalChallenge): Promise<boolean> {
const Popup = new PopupHandler();

Popup.show({challengeUrl});

lightbox.show();
return new Promise<boolean>((resolve, reject) => {
const handleChallenge = (event: MessageEvent) => {
if (event.data === "authsignal-challenge-success") {
lightbox.close();
Popup.close();
resolve(true);
} else if (event.data === "authsignal-challenge-failure") {
lightbox.close();
Popup.close();
reject(false);
}
};
Expand Down
80 changes: 80 additions & 0 deletions src/PopupHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import A11yDialog from "a11y-dialog";
import {AuthsignalChallenge} from "./types";

const DIALOG_CONTAINER_ID = "authsignal-popup";
const DIALOG_CONTENT_ID = "authsignal-popup-content";

class PopupHandler {
private popup: A11yDialog | null = null;

constructor() {
if (document.querySelector(`#${DIALOG_CONTAINER_ID}`)) {
throw new Error("Multiple instances of Authsignal popup is not supported.");
}

this.create();
}

create() {
// Dialog container
const container = document.createElement("div");
container.setAttribute("id", DIALOG_CONTAINER_ID);
container.setAttribute("aria-hidden", "true");
container.setAttribute("class", "dialog-container");

// Dialog overlay
const overlay = document.createElement("div");
overlay.setAttribute("class", "dialog-overlay");
overlay.setAttribute("data-a11y-dialog-hide", "true");
container.appendChild(overlay);

// Dialog content
const content = document.createElement("div");
content.setAttribute("class", "dialog-content");
content.setAttribute("id", DIALOG_CONTENT_ID);
container.appendChild(content);

document.body.appendChild(container);

this.popup = new A11yDialog(container);
this.popup.on("hide", this.destroy);
}

destroy() {
const dialogEl = document.querySelector(`#${DIALOG_CONTAINER_ID}`);
if (dialogEl) {
document.body.removeChild(dialogEl);
}
}

show({challengeUrl}: {challengeUrl: AuthsignalChallenge["challengeUrl"]}) {
if (!this.popup) {
throw new Error("Popup is not initialized");
}

const iframe = document.createElement("iframe");
iframe.setAttribute("name", "authsignal");
iframe.setAttribute("title", "Authsignal multi-factor authentication challenge");
iframe.setAttribute("src", challengeUrl);
iframe.setAttribute("width", "600");
iframe.setAttribute("height", "600");
iframe.setAttribute("frameborder", "0");

const dialogContent = document.querySelector(`#${DIALOG_CONTENT_ID}`);
if (dialogContent) {
dialogContent.appendChild(iframe);
}

this.popup.show();
}

close() {
if (!this.popup) {
throw new Error("Popup is not initialized");
}

this.popup.hide();
}
}

export {PopupHandler};
17 changes: 12 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@
resolved "https://registry.yarnpkg.com/@wessberg/stringutil/-/stringutil-1.0.19.tgz#baadcb6f4471fe2d46462a7d7a8294e4b45b29ad"
integrity sha512-9AZHVXWlpN8Cn9k5BC/O0Dzb9E9xfEMXzYrNunwvkUTvuK7xgQPVRZpLo+jWCOZ5r8oBa8NIrHuPEu1hzbb6bg==

a11y-dialog@^7.4.0:
version "7.4.0"
resolved "https://registry.yarnpkg.com/a11y-dialog/-/a11y-dialog-7.4.0.tgz#0641256a17925a61f07ce374db3515177c3cb9c8"
integrity sha512-NrLXedddjbIxvapn0ac488DkVuWZjCNxLxHxXPUxjNF7crdHJ4dquPwJ89dV21IPQsXtA5YHNn++Mxouu+ciuQ==
dependencies:
focusable-selectors "^0.3.1"

acorn-jsx@^5.3.2:
version "5.3.2"
resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.3.2.tgz#7ed5bb55908b3b2f1bc55c6af1653bada7f07937"
Expand Down Expand Up @@ -295,11 +302,6 @@ balanced-match@^1.0.0:
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==

basiclightbox@^5.0.4:
version "5.0.4"
resolved "https://registry.yarnpkg.com/basiclightbox/-/basiclightbox-5.0.4.tgz#5367d6d468ae44406e51b8486cfb8751f36f1fb2"
integrity sha512-EsuNWmfcFXWZOe0txKXsllYOC7bDpoaVLc4HHHlYKB/roymlZs+FBdLUU6rx2yPpnJZhulwheKdPjqr2k0+NGQ==

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down Expand Up @@ -666,6 +668,11 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.5.tgz#76c8584f4fc843db64702a6bd04ab7a8bd666da3"
integrity sha512-WIWGi2L3DyTUvUrwRKgGi9TwxQMUEqPOPQBVi71R96jZXJdFskXEmf54BoZaS1kknGODoIGASGEzBUYdyMCBJg==

focusable-selectors@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/focusable-selectors/-/focusable-selectors-0.3.1.tgz#7eacbca8dc6cc8d7f7563e5f5cc3699b91e20aaa"
integrity sha512-5JLtr0e1YJIfmnVlpLiG+av07dd0Xkf/KfswsXcei5KmLfdwOysTQsjF058ynXniujb1fvev7nql1x+CkC5ikw==

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down

0 comments on commit 66239bf

Please sign in to comment.