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';
|
|
|
|
|
2018-07-21 21:35:17 +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 { 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';
|
2019-07-02 14:13:33 +02:00
|
|
|
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';
|
2019-07-02 14:13:33 +02:00
|
|
|
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';
|
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;
|
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;
|
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-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,
|
2019-07-02 14:13:33 +02:00
|
|
|
protected environmentService: EnvironmentService, protected stateService: StateService,
|
|
|
|
protected storageService: StorageService) {
|
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 ||
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-03 16:37:26 +02:00
|
|
|
if (this.initU2f && this.win != null && this.u2fSupported) {
|
2018-04-04 22:27:30 +02:00
|
|
|
let customWebVaultUrl: string = null;
|
2018-07-23 23:15:23 +02:00
|
|
|
if (this.environmentService.baseUrl != null) {
|
2018-04-04 22:27:30 +02:00
|
|
|
customWebVaultUrl = this.environmentService.baseUrl;
|
2018-07-23 23:15:23 +02:00
|
|
|
} else if (this.environmentService.webVaultUrl != null) {
|
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) => {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), error);
|
2018-04-04 22:27:30 +02:00
|
|
|
}, (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;
|
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:
|
2018-04-04 22:27:30 +02:00
|
|
|
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);
|
2018-10-10 23:52:08 +02:00
|
|
|
} else {
|
|
|
|
// TODO: Deprecated. Remove in future version.
|
2019-05-27 16:29:09 +02:00
|
|
|
const challenges = JSON.parse(providerData.Challenges);
|
2018-10-10 23:52:08 +02:00
|
|
|
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 22:27:30 +02:00
|
|
|
}
|
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) {
|
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;
|
2019-07-02 14:13:33 +02:00
|
|
|
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
|
|
|
}
|
2018-10-03 06:03:49 +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 {
|
|
|
|
this.router.navigate([this.successRoute]);
|
|
|
|
}
|
2018-08-25 14:47:38 +02:00
|
|
|
} catch {
|
2018-04-04 22:27:30 +02:00
|
|
|
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;
|
|
|
|
}
|
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
|
|
|
}
|