Skip to content

Commit 6dc7b03

Browse files
committed
prevent loading the login client before we need it.
1 parent 7611b4d commit 6dc7b03

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

src/components/mfaDevices/mfaDevices.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,21 @@ import QRCode from 'qrcode';
66
import Styles from '../../bootstrap';
77

88
const logger = console;
9-
const loginClient = new LoginClient({ applicationId: 'app_authress-mfa-devices', authressLoginHostUrl: window.location.origin }, logger);
9+
10+
let cachedLoginClient;
11+
function getLoginClient() {
12+
if (!cachedLoginClient) {
13+
cachedLoginClient = new LoginClient({
14+
applicationId: 'app_authress-mfa-devices',
15+
authressLoginHostUrl: window.location.origin,
16+
// We skip the background credentials check because on this screen they should always already have credentials, otherwise the user should not have been directed here in the first place.
17+
// * And we need to avoid that to prevent the authressLoginHostUrl being set to localhost from causing a problem, and attempting to connect to localhost to load the credentials.
18+
// * Realistically this should be off in every case because the user should not have been asked to load mfa devices if they aren't logged in. But if the user does get here and they aren't logged in, this will be a problem, so we do at least sometimes want to refresh the auth token
19+
skipBackgroundCredentialsCheck: window.location.hostname !== 'localhost' }, logger);
20+
}
21+
22+
return cachedLoginClient;
23+
}
1024

1125
const states = {
1226
LOADING: 'LOADING',
@@ -58,7 +72,7 @@ export default class MfaDevices extends LitElement {
5872
async fetchDevices() {
5973
try {
6074
// Note: this waits until there is a session before attempting to fetch devices
61-
this.devices = await loginClient.getDevices();
75+
this.devices = await getLoginClient().getDevices();
6276

6377
this.state = states.LIST;
6478
this.requestUpdate();
@@ -74,7 +88,7 @@ export default class MfaDevices extends LitElement {
7488

7589
setTimeout(async () => {
7690
try {
77-
await loginClient.deleteDevice(deviceId);
91+
await getLoginClient().deleteDevice(deviceId);
7892
this.devices = this.devices.filter(d => d.deviceId !== deviceId);
7993
this.state = states.LIST;
8094
} catch (error) {
@@ -110,7 +124,7 @@ export default class MfaDevices extends LitElement {
110124

111125
try {
112126
const params = { name: this.deviceName || 'Mobile Authenticator', type: 'TOTP', totp: { secret: this.encodedSecret, verificationCode: this.totpCode } };
113-
const result = await loginClient.registerDevice(params);
127+
const result = await getLoginClient().registerDevice(params);
114128
this.devices.push(result);
115129
this.state = states.LIST;
116130
this.requestUpdate();
@@ -151,7 +165,7 @@ export default class MfaDevices extends LitElement {
151165

152166
await new Promise(resolve => setTimeout(resolve, 10));
153167
try {
154-
const result = await loginClient.registerDevice({ name: deviceName });
168+
const result = await getLoginClient().registerDevice({ name: deviceName });
155169
this.devices.push(result);
156170
this.state = states.LIST;
157171
return;
@@ -575,7 +589,7 @@ export default class MfaDevices extends LitElement {
575589

576590
displayQrCodeForNewAuthenticator() {
577591
const generateQrCode = async () => {
578-
const userIdentity = await loginClient.getUserIdentity();
592+
const userIdentity = await getLoginClient().getUserIdentity();
579593
const deviceString = `${userIdentity?.sub || ''} - ${this.deviceName || window.location.hostname}`;
580594

581595
const secret = (window.crypto || window.msCrypto).getRandomValues(new Uint8Array(24));

0 commit comments

Comments
 (0)