2018-02-01 04:54:13 +01:00
|
|
|
import * as template from './two-factor.component.html';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Component,
|
2018-02-02 18:01:55 +01:00
|
|
|
ComponentFactoryResolver,
|
2018-02-02 04:59:04 +01:00
|
|
|
OnInit,
|
2018-02-02 18:01:55 +01:00
|
|
|
ViewChild,
|
|
|
|
ViewContainerRef,
|
2018-02-01 04:54:13 +01:00
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
2018-02-08 16:37:54 +01:00
|
|
|
import { Angulartics2 } from 'angulartics2';
|
2018-02-01 04:54:13 +01:00
|
|
|
|
2018-02-02 18:01:55 +01:00
|
|
|
import { ModalComponent } from '../modal.component';
|
|
|
|
|
2018-02-08 16:37:54 +01:00
|
|
|
import { TwoFactorOptionsComponent } from './two-factor-options.component';
|
|
|
|
|
2018-02-02 04:59:04 +01:00
|
|
|
import { TwoFactorProviderType } from 'jslib/enums/twoFactorProviderType';
|
|
|
|
|
|
|
|
import { TwoFactorEmailRequest } from 'jslib/models/request/twoFactorEmailRequest';
|
2018-02-01 04:54:13 +01:00
|
|
|
|
|
|
|
import { ApiService } from 'jslib/abstractions/api.service';
|
|
|
|
import { AuthService } from 'jslib/abstractions/auth.service';
|
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
2018-02-02 04:59:04 +01:00
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
2018-02-08 16:48:50 +01:00
|
|
|
import { SyncService } from 'jslib/abstractions/sync.service';
|
2018-02-02 04:59:04 +01:00
|
|
|
|
|
|
|
import { TwoFactorProviders } from 'jslib/services/auth.service';
|
2018-02-01 04:54:13 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-two-factor',
|
|
|
|
template: template,
|
|
|
|
})
|
2018-02-02 04:59:04 +01:00
|
|
|
export class TwoFactorComponent implements OnInit {
|
2018-02-02 18:01:55 +01:00
|
|
|
@ViewChild('twoFactorOptions', { read: ViewContainerRef }) twoFactorOptionsModal: ViewContainerRef;
|
|
|
|
|
2018-02-01 04:54:13 +01:00
|
|
|
token: string = '';
|
|
|
|
remember: boolean = false;
|
2018-02-02 04:59:04 +01:00
|
|
|
u2fReady: boolean = false;
|
|
|
|
providers = TwoFactorProviders;
|
|
|
|
providerType = TwoFactorProviderType;
|
|
|
|
selectedProviderType: TwoFactorProviderType = TwoFactorProviderType.Authenticator;
|
|
|
|
u2fSupported: boolean = false;
|
|
|
|
u2f: any = null;
|
|
|
|
title: string = '';
|
|
|
|
twoFactorEmail: string = null;
|
2018-02-01 04:54:13 +01:00
|
|
|
formPromise: Promise<any>;
|
2018-02-02 04:59:04 +01:00
|
|
|
emailPromise: Promise<any>;
|
2018-02-01 04:54:13 +01:00
|
|
|
|
|
|
|
constructor(private authService: AuthService, private router: Router, private analytics: Angulartics2,
|
2018-02-02 04:59:04 +01:00
|
|
|
private toasterService: ToasterService, private i18nService: I18nService, private apiService: ApiService,
|
2018-02-02 18:01:55 +01:00
|
|
|
private platformUtilsService: PlatformUtilsService,
|
2018-02-08 16:48:50 +01:00
|
|
|
private componentFactoryResolver: ComponentFactoryResolver, private syncService: SyncService) {
|
2018-02-02 04:59:04 +01:00
|
|
|
this.u2fSupported = this.platformUtilsService.supportsU2f(window);
|
|
|
|
}
|
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
if (this.authService.email == null || this.authService.masterPasswordHash == null ||
|
|
|
|
this.authService.twoFactorProviders == null) {
|
|
|
|
this.router.navigate(['login']);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.selectedProviderType = this.authService.getDefaultTwoFactorProvider(this.u2fSupported);
|
|
|
|
await this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
if (this.selectedProviderType == null) {
|
|
|
|
this.title = this.i18nService.t('loginUnavailable');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.title = (TwoFactorProviders as any)[this.selectedProviderType].name;
|
|
|
|
const params = this.authService.twoFactorProviders.get(this.selectedProviderType);
|
|
|
|
switch (this.selectedProviderType) {
|
|
|
|
case TwoFactorProviderType.U2f:
|
|
|
|
if (!this.u2fSupported) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-08 16:37:54 +01:00
|
|
|
const challenges = JSON.parse(params.Challenges);
|
2018-02-02 04:59:04 +01:00
|
|
|
// TODO: init u2f
|
|
|
|
break;
|
|
|
|
case TwoFactorProviderType.Duo:
|
2018-04-03 21:11:37 +02:00
|
|
|
case TwoFactorProviderType.OrganizationDuo:
|
2018-02-02 04:59:04 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
(window as any).Duo.init({
|
2018-02-08 16:37:54 +01:00
|
|
|
host: params.Host,
|
|
|
|
sig_request: params.Signature,
|
2018-02-02 04:59:04 +01: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-02-08 16:37:54 +01:00
|
|
|
},
|
2018-02-02 04:59:04 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case TwoFactorProviderType.Email:
|
2018-02-08 16:37:54 +01:00
|
|
|
this.twoFactorEmail = params.Email;
|
2018-02-02 04:59:04 +01:00
|
|
|
if (this.authService.twoFactorProviders.size > 1) {
|
|
|
|
await this.sendEmail(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 04:54:13 +01:00
|
|
|
|
|
|
|
async submit() {
|
|
|
|
if (this.token == null || this.token === '') {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('verificationCodeRequired'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-02 04:59:04 +01:00
|
|
|
if (this.selectedProviderType === TwoFactorProviderType.U2f) {
|
|
|
|
// TODO: stop U2f
|
|
|
|
} else if (this.selectedProviderType === TwoFactorProviderType.Email ||
|
|
|
|
this.selectedProviderType === TwoFactorProviderType.Authenticator) {
|
|
|
|
this.token = this.token.replace(' ', '').trim();
|
|
|
|
}
|
2018-02-01 04:54:13 +01:00
|
|
|
|
|
|
|
try {
|
2018-02-02 04:59:04 +01:00
|
|
|
this.formPromise = this.authService.logInTwoFactor(this.selectedProviderType, this.token, this.remember);
|
2018-02-01 04:54:13 +01:00
|
|
|
await this.formPromise;
|
2018-02-08 16:48:50 +01:00
|
|
|
this.syncService.fullSync(true);
|
2018-02-01 04:54:13 +01:00
|
|
|
this.analytics.eventTrack.next({ action: 'Logged In From Two-step' });
|
|
|
|
this.router.navigate(['vault']);
|
|
|
|
} catch {
|
2018-02-02 04:59:04 +01:00
|
|
|
if (this.selectedProviderType === TwoFactorProviderType.U2f) {
|
|
|
|
// TODO: start U2F again
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendEmail(doToast: boolean) {
|
|
|
|
if (this.selectedProviderType !== TwoFactorProviderType.Email) {
|
|
|
|
return;
|
2018-02-01 04:54:13 +01:00
|
|
|
}
|
2018-02-02 04:59:04 +01:00
|
|
|
|
2018-02-02 18:01:55 +01:00
|
|
|
if (this.emailPromise != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-02 04:59:04 +01:00
|
|
|
try {
|
|
|
|
const request = new TwoFactorEmailRequest(this.authService.email, this.authService.masterPasswordHash);
|
|
|
|
this.emailPromise = this.apiService.postTwoFactorEmail(request);
|
|
|
|
await this.emailPromise;
|
|
|
|
if (doToast) {
|
2018-02-02 18:01:55 +01:00
|
|
|
this.toasterService.popAsync('success', null,
|
|
|
|
this.i18nService.t('verificationCodeEmailSent', this.twoFactorEmail));
|
2018-02-02 04:59:04 +01:00
|
|
|
}
|
|
|
|
} catch { }
|
2018-02-02 18:01:55 +01:00
|
|
|
|
|
|
|
this.emailPromise = null;
|
2018-02-02 04:59:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
anotherMethod() {
|
2018-02-02 18:01:55 +01:00
|
|
|
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
|
|
|
|
const modal = this.twoFactorOptionsModal.createComponent(factory).instance;
|
|
|
|
const childComponent = modal.show<TwoFactorOptionsComponent>(TwoFactorOptionsComponent,
|
|
|
|
this.twoFactorOptionsModal);
|
|
|
|
|
|
|
|
childComponent.onProviderSelected.subscribe(async (provider: TwoFactorProviderType) => {
|
|
|
|
modal.close();
|
|
|
|
this.selectedProviderType = provider;
|
|
|
|
await this.init();
|
|
|
|
});
|
|
|
|
childComponent.onRecoverSelected.subscribe(() => {
|
|
|
|
modal.close();
|
|
|
|
});
|
2018-02-01 04:54:13 +01:00
|
|
|
}
|
|
|
|
}
|