mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 03:03:43 +00:00
consolidate 2fa component functionality
This commit is contained in:
73
src/app/settings/two-factor-verify.component.ts
Normal file
73
src/app/settings/two-factor-verify.component.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import {
|
||||
Component,
|
||||
EventEmitter,
|
||||
Input,
|
||||
Output,
|
||||
} from '@angular/core';
|
||||
|
||||
import { ToasterService } from 'angular2-toaster';
|
||||
|
||||
import { TwoFactorProviderType } from 'jslib/enums/twoFactorProviderType';
|
||||
|
||||
import { ApiService } from 'jslib/abstractions/api.service';
|
||||
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
||||
import { I18nService } from 'jslib/abstractions/i18n.service';
|
||||
|
||||
import { PasswordVerificationRequest } from 'jslib/models/request/passwordVerificationRequest';
|
||||
|
||||
@Component({
|
||||
selector: 'app-two-factor-verify',
|
||||
templateUrl: 'two-factor-verify.component.html',
|
||||
})
|
||||
export class TwoFactorVerifyComponent {
|
||||
@Input()
|
||||
type: TwoFactorProviderType;
|
||||
@Output()
|
||||
onAuthed = new EventEmitter<any>();
|
||||
|
||||
masterPassword: string;
|
||||
formPromise: Promise<any>;
|
||||
|
||||
private masterPasswordHash: string;
|
||||
|
||||
constructor(private apiService: ApiService, private i18nService: I18nService,
|
||||
private toasterService: ToasterService, private cryptoService: CryptoService) { }
|
||||
|
||||
async submit() {
|
||||
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 {
|
||||
switch (this.type) {
|
||||
case TwoFactorProviderType.Duo:
|
||||
this.formPromise = this.apiService.getTwoFactorDuo(request);
|
||||
break;
|
||||
case TwoFactorProviderType.Email:
|
||||
this.formPromise = this.apiService.getTwoFactorEmail(request);
|
||||
break;
|
||||
case TwoFactorProviderType.U2f:
|
||||
this.formPromise = this.apiService.getTwoFactorU2f(request);
|
||||
break;
|
||||
case TwoFactorProviderType.Authenticator:
|
||||
this.formPromise = this.apiService.getTwoFactorAuthenticator(request);
|
||||
break;
|
||||
case TwoFactorProviderType.Yubikey:
|
||||
this.formPromise = this.apiService.getTwoFactorYubiKey(request);
|
||||
break;
|
||||
}
|
||||
|
||||
const response = await this.formPromise;
|
||||
this.onAuthed.emit({
|
||||
response: response,
|
||||
masterPasswordHash: this.masterPasswordHash,
|
||||
});
|
||||
} catch { }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user