mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 08:43:33 +00:00
consolidate 2fa component functionality
This commit is contained in:
@@ -1,51 +1,41 @@
|
||||
import {
|
||||
Component,
|
||||
EventEmitter,
|
||||
OnDestroy,
|
||||
OnInit,
|
||||
Output,
|
||||
} from '@angular/core';
|
||||
|
||||
import { ToasterService } from 'angular2-toaster';
|
||||
import { Angulartics2 } from 'angulartics2';
|
||||
|
||||
import { ApiService } from 'jslib/abstractions/api.service';
|
||||
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
||||
import { I18nService } from 'jslib/abstractions/i18n.service';
|
||||
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
||||
|
||||
import { PasswordVerificationRequest } from 'jslib/models/request/passwordVerificationRequest';
|
||||
import { TwoFactorProviderRequest } from 'jslib/models/request/twoFactorProviderRequest';
|
||||
|
||||
import { TwoFactorProviderType } from 'jslib/enums/twoFactorProviderType';
|
||||
import { UpdateTwoFactorU2fRequest } from 'jslib/models/request/updateTwoFactorU2fRequest';
|
||||
import { TwoFactorU2fResponse } from 'jslib/models/response/twoFactorU2fResponse';
|
||||
|
||||
import { TwoFactorBaseComponent } from './two-factor-base.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-two-factor-u2f',
|
||||
templateUrl: 'two-factor-u2f.component.html',
|
||||
})
|
||||
export class TwoFactorU2fComponent implements OnInit, OnDestroy {
|
||||
@Output() onUpdated = new EventEmitter<boolean>();
|
||||
|
||||
enabled = false;
|
||||
authed = false;
|
||||
export class TwoFactorU2fComponent extends TwoFactorBaseComponent implements OnInit, OnDestroy {
|
||||
u2fChallenge: any;
|
||||
u2fError: boolean;
|
||||
u2fListening: boolean;
|
||||
u2fResponse: string;
|
||||
masterPassword: string;
|
||||
|
||||
authPromise: Promise<any>;
|
||||
formPromise: Promise<any>;
|
||||
|
||||
private masterPasswordHash: string;
|
||||
private closed = false;
|
||||
private u2fScript: HTMLScriptElement;
|
||||
|
||||
constructor(private apiService: ApiService, private i18nService: I18nService,
|
||||
private analytics: Angulartics2, private toasterService: ToasterService,
|
||||
private cryptoService: CryptoService, private platformUtilsService: PlatformUtilsService) {
|
||||
constructor(apiService: ApiService, i18nService: I18nService,
|
||||
analytics: Angulartics2, toasterService: ToasterService,
|
||||
platformUtilsService: PlatformUtilsService) {
|
||||
super(apiService, i18nService, analytics, toasterService, platformUtilsService,
|
||||
TwoFactorProviderType.U2f);
|
||||
this.u2fScript = window.document.createElement('script');
|
||||
this.u2fScript.src = 'scripts/u2f.js';
|
||||
this.u2fScript.async = true;
|
||||
@@ -60,33 +50,32 @@ export class TwoFactorU2fComponent implements OnInit, OnDestroy {
|
||||
window.document.body.removeChild(this.u2fScript);
|
||||
}
|
||||
|
||||
async auth() {
|
||||
if (this.masterPassword == null || this.masterPassword === '') {
|
||||
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
||||
this.i18nService.t('masterPassRequired'));
|
||||
return;
|
||||
}
|
||||
|
||||
const request = new PasswordVerificationRequest();
|
||||
request.masterPasswordHash = this.masterPasswordHash =
|
||||
await this.cryptoService.hashPassword(this.masterPassword, null);
|
||||
try {
|
||||
this.authPromise = this.apiService.getTwoFactorU2f(request);
|
||||
const response = await this.authPromise;
|
||||
this.authed = true;
|
||||
await this.processResponse(response);
|
||||
this.readDevice();
|
||||
} catch { }
|
||||
auth(authResponse: any) {
|
||||
super.auth(authResponse);
|
||||
this.processResponse(authResponse.response);
|
||||
this.readDevice();
|
||||
}
|
||||
|
||||
async submit() {
|
||||
submit() {
|
||||
if (this.enabled) {
|
||||
this.disable();
|
||||
return super.disable(this.formPromise);
|
||||
} else {
|
||||
this.enable();
|
||||
return this.enable();
|
||||
}
|
||||
}
|
||||
|
||||
protected enable() {
|
||||
const request = new UpdateTwoFactorU2fRequest();
|
||||
request.masterPasswordHash = this.masterPasswordHash;
|
||||
request.deviceResponse = this.u2fResponse;
|
||||
|
||||
return super.enable(async () => {
|
||||
this.formPromise = this.apiService.putTwoFactorU2f(request);
|
||||
const response = await this.formPromise;
|
||||
await this.processResponse(response);
|
||||
});
|
||||
}
|
||||
|
||||
private readDevice() {
|
||||
if (this.closed || this.enabled) {
|
||||
return;
|
||||
@@ -117,40 +106,7 @@ export class TwoFactorU2fComponent implements OnInit, OnDestroy {
|
||||
}, 10);
|
||||
}
|
||||
|
||||
private async enable() {
|
||||
const request = new UpdateTwoFactorU2fRequest();
|
||||
request.masterPasswordHash = this.masterPasswordHash;
|
||||
request.deviceResponse = this.u2fResponse;
|
||||
try {
|
||||
this.formPromise = this.apiService.putTwoFactorU2f(request);
|
||||
const response = await this.formPromise;
|
||||
await this.processResponse(response);
|
||||
this.analytics.eventTrack.next({ action: 'Enabled Two-step U2F' });
|
||||
this.onUpdated.emit(true);
|
||||
} catch { }
|
||||
}
|
||||
|
||||
private async disable() {
|
||||
const confirmed = await this.platformUtilsService.showDialog(this.i18nService.t('twoStepDisableDesc'),
|
||||
this.i18nService.t('disable'), this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
|
||||
if (!confirmed) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const request = new TwoFactorProviderRequest();
|
||||
request.masterPasswordHash = this.masterPasswordHash;
|
||||
request.type = TwoFactorProviderType.U2f;
|
||||
this.formPromise = this.apiService.putTwoFactorDisable(request);
|
||||
await this.formPromise;
|
||||
this.enabled = false;
|
||||
this.analytics.eventTrack.next({ action: 'Disabled Two-step U2F' });
|
||||
this.toasterService.popAsync('success', null, this.i18nService.t('twoStepDisabled'));
|
||||
this.onUpdated.emit(false);
|
||||
} catch { }
|
||||
}
|
||||
|
||||
private async processResponse(response: TwoFactorU2fResponse) {
|
||||
private processResponse(response: TwoFactorU2fResponse) {
|
||||
this.u2fChallenge = response.challenge;
|
||||
this.enabled = response.enabled;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user