bitwarden-estensione-browser/src/angular/components/two-factor.component.ts

250 lines
9.3 KiB
TypeScript
Raw Normal View History

import {
OnDestroy,
OnInit,
} from '@angular/core';
import {
ActivatedRoute,
Router,
} from '@angular/router';
2018-04-04 15:47:43 +02:00
import { DeviceType } from '../../enums/deviceType';
2018-04-04 15:47:43 +02:00
import { TwoFactorProviderType } from '../../enums/twoFactorProviderType';
import { TwoFactorEmailRequest } from '../../models/request/twoFactorEmailRequest';
import { AuthResult } from '../../models/domain';
2018-04-04 15:47:43 +02:00
import { ApiService } from '../../abstractions/api.service';
import { AuthService } from '../../abstractions/auth.service';
import { EnvironmentService } from '../../abstractions/environment.service';
2018-04-04 15:47:43 +02:00
import { I18nService } from '../../abstractions/i18n.service';
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
import { StateService } from '../../abstractions/state.service';
import { StorageService } from '../../abstractions/storage.service';
2018-04-04 15:47:43 +02:00
import { TwoFactorProviders } from '../../services/auth.service';
import { ConstantsService } from '../../services/constants.service';
2018-04-04 15:47:43 +02:00
2018-12-18 23:00:07 +01:00
import * as DuoWebSDK from 'duo_web_sdk';
import { U2f } from '../../misc/u2f';
export class TwoFactorComponent implements OnInit, OnDestroy {
2018-04-04 15:47:43 +02:00
token: string = '';
remember: boolean = false;
u2fReady: boolean = false;
2019-07-03 16:37:26 +02:00
initU2f: boolean = true;
2018-04-04 15:47:43 +02:00
providers = TwoFactorProviders;
providerType = TwoFactorProviderType;
selectedProviderType: TwoFactorProviderType = TwoFactorProviderType.Authenticator;
u2fSupported: boolean = false;
u2f: U2f = null;
2018-04-04 15:47:43 +02:00
title: string = '';
twoFactorEmail: string = null;
formPromise: Promise<any>;
emailPromise: Promise<any>;
identifier: string = null;
2018-07-13 16:49:37 +02:00
onSuccessfulLogin: () => Promise<any>;
onSuccessfulLoginNavigate: () => Promise<any>;
2018-04-04 15:47:43 +02:00
2018-04-05 04:59:14 +02:00
protected loginRoute = 'login';
2018-04-04 15:47:43 +02:00
protected successRoute = 'vault';
constructor(protected authService: AuthService, protected router: Router,
protected i18nService: I18nService, protected apiService: ApiService,
2018-04-25 18:08:18 +02:00
protected platformUtilsService: PlatformUtilsService, protected win: Window,
protected environmentService: EnvironmentService, protected stateService: StateService,
protected storageService: StorageService, protected route: ActivatedRoute) {
2018-04-07 06:18:31 +02:00
this.u2fSupported = this.platformUtilsService.supportsU2f(win);
2018-04-04 15:47:43 +02:00
}
async ngOnInit() {
if ((!this.authService.authingWithSso() && !this.authService.authingWithPassword()) ||
2019-05-27 16:29:09 +02:00
this.authService.twoFactorProvidersData == null) {
2018-04-05 04:59:14 +02:00
this.router.navigate([this.loginRoute]);
2018-04-04 15:47:43 +02:00
return;
}
const queryParamsSub = this.route.queryParams.subscribe(async (qParams) => {
if (qParams.identifier != null) {
this.identifier = qParams.identifier;
}
if (queryParamsSub != null) {
queryParamsSub.unsubscribe();
}
});
if (this.authService.authingWithSso()) {
this.successRoute = 'lock';
}
2019-07-03 16:37:26 +02:00
if (this.initU2f && this.win != null && this.u2fSupported) {
let customWebVaultUrl: string = null;
2018-07-23 23:15:23 +02:00
if (this.environmentService.baseUrl != null) {
customWebVaultUrl = this.environmentService.baseUrl;
2018-07-23 23:15:23 +02:00
} else if (this.environmentService.webVaultUrl != null) {
customWebVaultUrl = this.environmentService.webVaultUrl;
}
this.u2f = new U2f(this.win, customWebVaultUrl, (token: string) => {
this.token = token;
this.submit();
}, (error: string) => {
2018-10-03 05:09:19 +02:00
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), error);
}, (info: string) => {
if (info === 'ready') {
this.u2fReady = true;
}
});
}
2018-04-04 15:47:43 +02:00
this.selectedProviderType = this.authService.getDefaultTwoFactorProvider(this.u2fSupported);
await this.init();
}
ngOnDestroy(): void {
this.cleanupU2f();
this.u2f = null;
}
2018-04-04 15:47:43 +02:00
async init() {
if (this.selectedProviderType == null) {
this.title = this.i18nService.t('loginUnavailable');
return;
}
this.cleanupU2f();
2018-04-04 15:47:43 +02:00
this.title = (TwoFactorProviders as any)[this.selectedProviderType].name;
2019-05-27 16:29:09 +02:00
const providerData = this.authService.twoFactorProvidersData.get(this.selectedProviderType);
2018-04-04 15:47:43 +02:00
switch (this.selectedProviderType) {
case TwoFactorProviderType.U2f:
if (!this.u2fSupported || this.u2f == null) {
2018-04-04 15:47:43 +02:00
break;
}
2019-05-27 16:29:09 +02:00
if (providerData.Challenge != null) {
2019-07-03 16:37:26 +02:00
setTimeout(() => {
this.u2f.init(JSON.parse(providerData.Challenge));
}, 500);
} else {
// TODO: Deprecated. Remove in future version.
2019-05-27 16:29:09 +02:00
const challenges = JSON.parse(providerData.Challenges);
if (challenges != null && challenges.length > 0) {
this.u2f.init({
appId: challenges[0].appId,
challenge: challenges[0].challenge,
keys: challenges.map((c: any) => {
return {
version: c.version,
keyHandle: c.keyHandle,
};
}),
});
}
}
2018-04-04 15:47:43 +02:00
break;
case TwoFactorProviderType.Duo:
case TwoFactorProviderType.OrganizationDuo:
setTimeout(() => {
2018-06-11 19:32:53 +02:00
DuoWebSDK.init({
iframe: undefined,
2019-05-27 16:29:09 +02:00
host: providerData.Host,
sig_request: providerData.Signature,
2018-04-04 15:47:43 +02:00
submit_callback: async (f: HTMLFormElement) => {
const sig = f.querySelector('input[name="sig_response"]') as HTMLInputElement;
if (sig != null) {
this.token = sig.value;
await this.submit();
}
},
});
2018-06-11 19:32:53 +02:00
}, 0);
2018-04-04 15:47:43 +02:00
break;
case TwoFactorProviderType.Email:
2019-05-27 16:29:09 +02:00
this.twoFactorEmail = providerData.Email;
if (this.authService.twoFactorProvidersData.size > 1) {
2018-04-04 15:47:43 +02:00
await this.sendEmail(false);
}
break;
default:
break;
}
}
async submit() {
if (this.token == null || this.token === '') {
2018-10-03 05:09:19 +02:00
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
2018-04-04 15:47:43 +02:00
this.i18nService.t('verificationCodeRequired'));
return;
}
if (this.selectedProviderType === TwoFactorProviderType.U2f) {
if (this.u2f != null) {
this.u2f.stop();
} else {
return;
}
2018-04-04 15:47:43 +02:00
} else if (this.selectedProviderType === TwoFactorProviderType.Email ||
this.selectedProviderType === TwoFactorProviderType.Authenticator) {
this.token = this.token.replace(' ', '').trim();
}
try {
this.formPromise = this.authService.logInTwoFactor(this.selectedProviderType, this.token, this.remember);
const response: AuthResult = await this.formPromise;
const disableFavicon = await this.storageService.get<boolean>(ConstantsService.disableFaviconKey);
await this.stateService.save(ConstantsService.disableFaviconKey, !!disableFavicon);
2018-07-13 16:49:37 +02:00
if (this.onSuccessfulLogin != null) {
this.onSuccessfulLogin();
2018-04-25 18:08:18 +02:00
}
this.platformUtilsService.eventTrack('Logged In From Two-step');
2018-07-13 16:49:37 +02:00
if (this.onSuccessfulLoginNavigate != null) {
this.onSuccessfulLoginNavigate();
} else {
if (response.resetMasterPassword) {
this.successRoute = 'set-password';
}
this.router.navigate([this.successRoute], {
queryParams: {
identifier: this.identifier,
},
});
2018-07-13 16:49:37 +02:00
}
} catch {
if (this.selectedProviderType === TwoFactorProviderType.U2f && this.u2f != null) {
this.u2f.start();
2018-04-04 15:47:43 +02:00
}
}
}
async sendEmail(doToast: boolean) {
if (this.selectedProviderType !== TwoFactorProviderType.Email) {
return;
}
if (this.emailPromise != null) {
return;
}
try {
const request = new TwoFactorEmailRequest(this.authService.email, this.authService.masterPasswordHash);
this.emailPromise = this.apiService.postTwoFactorEmail(request);
await this.emailPromise;
if (doToast) {
2018-10-03 05:09:19 +02:00
this.platformUtilsService.showToast('success', null,
2018-04-04 15:47:43 +02:00
this.i18nService.t('verificationCodeEmailSent', this.twoFactorEmail));
}
} catch { }
this.emailPromise = null;
}
private cleanupU2f() {
if (this.u2f != null) {
this.u2f.stop();
this.u2f.cleanup();
}
}
2018-04-04 15:47:43 +02:00
}