bitwarden-estensione-browser/src/app/settings/two-factor-webauthn.compone...

166 lines
6.0 KiB
TypeScript
Raw Normal View History

2018-06-27 23:50:31 +02:00
import {
Component,
2018-10-08 20:23:30 +02:00
NgZone,
2018-06-27 23:50:31 +02:00
} from '@angular/core';
import { ToasterService } from 'angular2-toaster';
import { ApiService } from 'jslib-common/abstractions/api.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { LogService } from 'jslib-common/abstractions/log.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { UserVerificationService } from 'jslib-common/abstractions/userVerification.service';
2018-06-27 23:50:31 +02:00
import { TwoFactorProviderType } from 'jslib-common/enums/twoFactorProviderType';
2018-10-08 20:23:30 +02:00
import { SecretVerificationRequest } from 'jslib-common/models/request/secretVerificationRequest';
import { UpdateTwoFactorWebAuthnDeleteRequest } from 'jslib-common/models/request/updateTwoFactorWebAuthnDeleteRequest';
import { UpdateTwoFactorWebAuthnRequest } from 'jslib-common/models/request/updateTwoFactorWebAuthnRequest';
2018-07-21 06:26:16 +02:00
import {
ChallengeResponse,
2021-03-16 17:44:31 +01:00
TwoFactorWebAuthnResponse,
} from 'jslib-common/models/response/twoFactorWebAuthnResponse';
2018-06-27 23:50:31 +02:00
import { TwoFactorBaseComponent } from './two-factor-base.component';
2018-06-27 23:50:31 +02:00
@Component({
2021-03-16 17:44:31 +01:00
selector: 'app-two-factor-webauthn',
templateUrl: 'two-factor-webauthn.component.html',
2018-06-27 23:50:31 +02:00
})
2021-03-16 17:44:31 +01:00
export class TwoFactorWebAuthnComponent extends TwoFactorBaseComponent {
type = TwoFactorProviderType.WebAuthn;
2018-10-08 20:23:30 +02:00
name: string;
keys: any[];
keyIdAvailable: number = null;
keysConfiguredCount = 0;
2021-03-16 17:44:31 +01:00
webAuthnError: boolean;
webAuthnListening: boolean;
webAuthnResponse: PublicKeyCredential;
2018-10-08 20:23:30 +02:00
challengePromise: Promise<ChallengeResponse>;
2018-06-27 23:50:31 +02:00
formPromise: Promise<any>;
constructor(apiService: ApiService, i18nService: I18nService,
toasterService: ToasterService, platformUtilsService: PlatformUtilsService,
private ngZone: NgZone, logService: LogService, userVerificationService: UserVerificationService) {
super(apiService, i18nService, toasterService, platformUtilsService, logService, userVerificationService);
2018-06-27 23:50:31 +02:00
}
auth(authResponse: any) {
super.auth(authResponse);
this.processResponse(authResponse.response);
2018-06-27 23:50:31 +02:00
}
async submit() {
2021-03-16 17:44:31 +01:00
if (this.webAuthnResponse == null || this.keyIdAvailable == null) {
2018-10-08 20:23:30 +02:00
// Should never happen.
return Promise.reject();
2018-06-27 23:50:31 +02:00
}
const request = await this.buildRequestModel(UpdateTwoFactorWebAuthnRequest);
2021-03-16 17:44:31 +01:00
request.deviceResponse = this.webAuthnResponse;
2018-10-08 20:23:30 +02:00
request.id = this.keyIdAvailable;
request.name = this.name;
return super.enable(async () => {
2021-03-16 17:44:31 +01:00
this.formPromise = this.apiService.putTwoFactorWebAuthn(request);
const response = await this.formPromise;
await this.processResponse(response);
});
}
2018-10-08 20:23:30 +02:00
disable() {
return super.disable(this.formPromise);
}
async remove(key: any) {
if (this.keysConfiguredCount <= 1 || key.removePromise != null) {
return;
}
2021-03-16 17:44:31 +01:00
const name = key.name != null ? key.name : this.i18nService.t('webAuthnkeyX', key.id);
2018-10-08 20:23:30 +02:00
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t('removeU2fConfirmation'), name,
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
if (!confirmed) {
2018-06-27 23:50:31 +02:00
return;
}
const request = await this.buildRequestModel(UpdateTwoFactorWebAuthnDeleteRequest);
2018-10-08 20:23:30 +02:00
request.id = key.id;
try {
2021-03-16 17:44:31 +01:00
key.removePromise = this.apiService.deleteTwoFactorWebAuthn(request);
2018-10-08 20:23:30 +02:00
const response = await key.removePromise;
key.removePromise = null;
await this.processResponse(response);
} catch (e) {
this.logService.error(e);
}
2018-10-08 20:23:30 +02:00
}
2018-06-27 23:50:31 +02:00
2018-10-08 20:23:30 +02:00
async readKey() {
if (this.keyIdAvailable == null) {
return;
}
const request = await this.buildRequestModel(SecretVerificationRequest);
2018-10-08 20:23:30 +02:00
try {
2021-03-16 17:44:31 +01:00
this.challengePromise = this.apiService.getTwoFactorWebAuthnChallenge(request);
2018-10-08 20:23:30 +02:00
const challenge = await this.challengePromise;
this.readDevice(challenge);
} catch (e) {
this.logService.error(e);
}
2018-10-08 20:23:30 +02:00
}
2021-03-16 17:44:31 +01:00
private readDevice(webAuthnChallenge: ChallengeResponse) {
2018-06-27 23:50:31 +02:00
// tslint:disable-next-line
console.log('listening for key...');
2021-03-16 17:44:31 +01:00
this.resetWebAuthn(true);
navigator.credentials.create({
publicKey: webAuthnChallenge,
}).then((data: PublicKeyCredential) => {
2018-10-08 20:23:30 +02:00
this.ngZone.run(() => {
2021-03-16 17:44:31 +01:00
this.webAuthnListening = false;
this.webAuthnResponse = data;
2018-10-08 20:23:30 +02:00
});
2021-03-16 17:44:31 +01:00
}).catch(err => {
// tslint:disable-next-line
console.error(err);
this.resetWebAuthn(false);
// TODO: Should we display the actual error?
this.webAuthnError = true;
});
2018-10-08 20:23:30 +02:00
}
2018-06-27 23:50:31 +02:00
2021-03-16 17:44:31 +01:00
private resetWebAuthn(listening = false) {
this.webAuthnResponse = null;
this.webAuthnError = false;
this.webAuthnListening = listening;
2018-06-27 23:50:31 +02:00
}
2021-03-16 17:44:31 +01:00
private processResponse(response: TwoFactorWebAuthnResponse) {
this.resetWebAuthn();
2018-10-08 20:23:30 +02:00
this.keys = [];
this.keyIdAvailable = null;
this.name = null;
this.keysConfiguredCount = 0;
for (let i = 1; i <= 5; i++) {
if (response.keys != null) {
const key = response.keys.filter(k => k.id === i);
2018-10-08 20:23:30 +02:00
if (key.length > 0) {
this.keysConfiguredCount++;
this.keys.push({
id: i, name: key[0].name,
configured: true,
2021-03-16 17:44:31 +01:00
migrated: key[0].migrated,
2018-10-08 20:23:30 +02:00
removePromise: null,
});
continue;
}
}
2021-03-16 17:44:31 +01:00
this.keys.push({ id: i, name: null, configured: false, removePromise: null });
2018-10-08 20:23:30 +02:00
if (this.keyIdAvailable == null) {
this.keyIdAvailable = i;
}
}
2018-06-27 23:50:31 +02:00
this.enabled = response.enabled;
}
}