2018-04-04 22:27:30 +02:00
|
|
|
import {
|
|
|
|
OnDestroy,
|
|
|
|
OnInit,
|
|
|
|
} from '@angular/core';
|
2018-04-04 15:47:43 +02:00
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
import { Angulartics2 } from 'angulartics2';
|
|
|
|
|
|
|
|
import { TwoFactorOptionsComponent } from './two-factor-options.component';
|
|
|
|
|
|
|
|
import { TwoFactorProviderType } from '../../enums/twoFactorProviderType';
|
|
|
|
|
|
|
|
import { TwoFactorEmailRequest } from '../../models/request/twoFactorEmailRequest';
|
|
|
|
|
|
|
|
import { ApiService } from '../../abstractions/api.service';
|
|
|
|
import { AuthService } from '../../abstractions/auth.service';
|
2018-04-04 22:27:30 +02:00
|
|
|
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 { SyncService } from '../../abstractions/sync.service';
|
|
|
|
|
|
|
|
import { TwoFactorProviders } from '../../services/auth.service';
|
|
|
|
|
2018-04-04 22:27:30 +02:00
|
|
|
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;
|
|
|
|
providers = TwoFactorProviders;
|
|
|
|
providerType = TwoFactorProviderType;
|
|
|
|
selectedProviderType: TwoFactorProviderType = TwoFactorProviderType.Authenticator;
|
|
|
|
u2fSupported: boolean = false;
|
2018-04-04 22:27:30 +02:00
|
|
|
u2f: U2f = null;
|
2018-04-04 15:47:43 +02:00
|
|
|
title: string = '';
|
|
|
|
twoFactorEmail: string = null;
|
|
|
|
formPromise: Promise<any>;
|
|
|
|
emailPromise: Promise<any>;
|
|
|
|
|
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 analytics: Angulartics2, protected toasterService: ToasterService,
|
|
|
|
protected i18nService: I18nService, protected apiService: ApiService,
|
2018-04-04 22:27:30 +02:00
|
|
|
protected platformUtilsService: PlatformUtilsService, protected syncService: SyncService,
|
|
|
|
protected win: Window, protected environmentService: EnvironmentService) {
|
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.email == null || this.authService.masterPasswordHash == null ||
|
|
|
|
this.authService.twoFactorProviders == null) {
|
2018-04-05 04:59:14 +02:00
|
|
|
this.router.navigate([this.loginRoute]);
|
2018-04-04 15:47:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-04 22:27:30 +02:00
|
|
|
if (this.win != null && this.u2fSupported) {
|
|
|
|
let customWebVaultUrl: string = null;
|
|
|
|
if (this.environmentService.baseUrl) {
|
|
|
|
customWebVaultUrl = this.environmentService.baseUrl;
|
2018-04-04 23:04:31 +02:00
|
|
|
} else if (this.environmentService.webVaultUrl) {
|
2018-04-04 22:27:30 +02:00
|
|
|
customWebVaultUrl = this.environmentService.webVaultUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.u2f = new U2f(this.win, customWebVaultUrl, (token: string) => {
|
|
|
|
this.token = token;
|
|
|
|
this.submit();
|
|
|
|
}, (error: string) => {
|
|
|
|
this.toasterService.popAsync('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();
|
|
|
|
}
|
|
|
|
|
2018-04-04 22:27:30 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-04 22:27:30 +02:00
|
|
|
this.cleanupU2f();
|
2018-04-04 15:47:43 +02:00
|
|
|
this.title = (TwoFactorProviders as any)[this.selectedProviderType].name;
|
|
|
|
const params = this.authService.twoFactorProviders.get(this.selectedProviderType);
|
|
|
|
switch (this.selectedProviderType) {
|
|
|
|
case TwoFactorProviderType.U2f:
|
2018-04-04 22:27:30 +02:00
|
|
|
if (!this.u2fSupported || this.u2f == null) {
|
2018-04-04 15:47:43 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const challenges = JSON.parse(params.Challenges);
|
2018-04-04 22:27:30 +02:00
|
|
|
if (challenges.length > 0) {
|
|
|
|
this.u2f.init({
|
|
|
|
appId: challenges[0].appId,
|
|
|
|
challenge: challenges[0].challenge,
|
|
|
|
keys: [{
|
|
|
|
version: challenges[0].version,
|
2018-04-11 05:49:09 +02:00
|
|
|
keyHandle: challenges[0].keyHandle,
|
2018-04-04 22:27:30 +02:00
|
|
|
}],
|
|
|
|
});
|
|
|
|
}
|
2018-04-04 15:47:43 +02:00
|
|
|
break;
|
|
|
|
case TwoFactorProviderType.Duo:
|
|
|
|
case TwoFactorProviderType.OrganizationDuo:
|
2018-04-04 22:51:11 +02:00
|
|
|
if (this.platformUtilsService.isSafari()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-04-04 15:47:43 +02:00
|
|
|
setTimeout(() => {
|
2018-04-07 06:18:31 +02:00
|
|
|
(this.win as any).Duo.init({
|
2018-04-04 15:47:43 +02:00
|
|
|
host: params.Host,
|
|
|
|
sig_request: params.Signature,
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case TwoFactorProviderType.Email:
|
|
|
|
this.twoFactorEmail = params.Email;
|
|
|
|
if (this.authService.twoFactorProviders.size > 1) {
|
|
|
|
await this.sendEmail(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async submit() {
|
|
|
|
if (this.token == null || this.token === '') {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('verificationCodeRequired'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.selectedProviderType === TwoFactorProviderType.U2f) {
|
2018-04-04 22:27:30 +02:00
|
|
|
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);
|
|
|
|
await this.formPromise;
|
|
|
|
this.syncService.fullSync(true);
|
|
|
|
this.analytics.eventTrack.next({ action: 'Logged In From Two-step' });
|
|
|
|
this.router.navigate([this.successRoute]);
|
2018-04-04 22:27:30 +02:00
|
|
|
} catch (e) {
|
|
|
|
if (this.selectedProviderType === TwoFactorProviderType.U2f && this.u2f != null) {
|
|
|
|
this.u2f.start();
|
|
|
|
} else {
|
|
|
|
throw e;
|
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) {
|
|
|
|
this.toasterService.popAsync('success', null,
|
|
|
|
this.i18nService.t('verificationCodeEmailSent', this.twoFactorEmail));
|
|
|
|
}
|
|
|
|
} catch { }
|
|
|
|
|
|
|
|
this.emailPromise = null;
|
|
|
|
}
|
2018-04-04 22:27:30 +02:00
|
|
|
|
|
|
|
private cleanupU2f() {
|
|
|
|
if (this.u2f != null) {
|
|
|
|
this.u2f.stop();
|
|
|
|
this.u2f.cleanup();
|
|
|
|
}
|
|
|
|
}
|
2018-04-04 15:47:43 +02:00
|
|
|
}
|