2018-04-06 21:33:20 +02:00
|
|
|
import {
|
|
|
|
ChangeDetectorRef,
|
|
|
|
Component,
|
|
|
|
NgZone,
|
|
|
|
OnDestroy,
|
|
|
|
OnInit,
|
|
|
|
} from '@angular/core';
|
2018-04-04 16:08:30 +02:00
|
|
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
import { Angulartics2 } from 'angulartics2';
|
|
|
|
|
2018-04-04 22:53:41 +02:00
|
|
|
import { BrowserApi } from '../../browser/browserApi';
|
|
|
|
|
|
|
|
import { TwoFactorProviderType } from 'jslib/enums/twoFactorProviderType';
|
|
|
|
|
2018-04-04 16:08:30 +02:00
|
|
|
import { ApiService } from 'jslib/abstractions/api.service';
|
|
|
|
import { AuthService } from 'jslib/abstractions/auth.service';
|
2018-04-04 22:29:43 +02:00
|
|
|
import { EnvironmentService } from 'jslib/abstractions/environment.service';
|
2018-04-04 16:08:30 +02:00
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
|
|
|
import { SyncService } from 'jslib/abstractions/sync.service';
|
|
|
|
|
2018-04-06 21:33:20 +02:00
|
|
|
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
|
|
|
|
|
2018-04-04 16:08:30 +02:00
|
|
|
import { TwoFactorComponent as BaseTwoFactorComponent } from 'jslib/angular/components/two-factor.component';
|
|
|
|
|
2018-04-06 21:33:20 +02:00
|
|
|
const BroadcasterSubscriptionId = 'TwoFactorComponent';
|
|
|
|
|
2018-04-04 16:08:30 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-two-factor',
|
2018-04-06 17:48:45 +02:00
|
|
|
templateUrl: 'two-factor.component.html',
|
2018-04-04 16:08:30 +02:00
|
|
|
})
|
|
|
|
export class TwoFactorComponent extends BaseTwoFactorComponent {
|
2018-04-04 22:53:41 +02:00
|
|
|
showNewWindowMessage = false;
|
|
|
|
|
2018-04-04 16:08:30 +02:00
|
|
|
constructor(authService: AuthService, router: Router,
|
|
|
|
analytics: Angulartics2, toasterService: ToasterService,
|
|
|
|
i18nService: I18nService, apiService: ApiService,
|
2018-04-04 22:29:43 +02:00
|
|
|
platformUtilsService: PlatformUtilsService, syncService: SyncService,
|
2018-04-06 21:33:20 +02:00
|
|
|
environmentService: EnvironmentService, private ngZone: NgZone,
|
|
|
|
private broadcasterService: BroadcasterService, private changeDetectorRef: ChangeDetectorRef) {
|
2018-04-04 16:08:30 +02:00
|
|
|
super(authService, router, analytics, toasterService, i18nService, apiService,
|
2018-04-04 22:29:43 +02:00
|
|
|
platformUtilsService, syncService, window, environmentService);
|
2018-04-05 04:59:42 +02:00
|
|
|
this.successRoute = '/tabs/vault';
|
2018-04-04 16:08:30 +02:00
|
|
|
}
|
|
|
|
|
2018-04-04 22:53:41 +02:00
|
|
|
async ngOnInit() {
|
2018-04-06 21:33:20 +02:00
|
|
|
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
|
|
|
|
this.ngZone.run(async () => {
|
|
|
|
switch (message.command) {
|
|
|
|
case '2faPageResponse':
|
|
|
|
if (message.type === 'duo') {
|
|
|
|
this.token = message.data.sigValue;
|
|
|
|
this.submitWithTab(message.webExtSender.tab);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.changeDetectorRef.detectChanges();
|
2018-04-11 05:49:46 +02:00
|
|
|
});
|
2018-04-06 21:33:20 +02:00
|
|
|
});
|
|
|
|
|
2018-04-04 22:53:41 +02:00
|
|
|
this.showNewWindowMessage = this.platformUtilsService.isSafari();
|
|
|
|
await super.ngOnInit();
|
|
|
|
|
|
|
|
if (this.selectedProviderType == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-11 05:49:46 +02:00
|
|
|
const isDuo = this.selectedProviderType === TwoFactorProviderType.Duo ||
|
|
|
|
this.selectedProviderType === TwoFactorProviderType.OrganizationDuo;
|
2018-04-04 22:53:41 +02:00
|
|
|
if (!this.platformUtilsService.isSafari() || !isDuo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const params = this.authService.twoFactorProviders.get(this.selectedProviderType);
|
|
|
|
const tab = BrowserApi.createNewTab(BrowserApi.getAssetUrl('2fa/index.html'));
|
|
|
|
const tabToSend = BrowserApi.makeTabObject(tab);
|
|
|
|
window.setTimeout(() => {
|
|
|
|
BrowserApi.tabSendMessage(tabToSend, {
|
|
|
|
command: '2faPageData',
|
|
|
|
data: {
|
|
|
|
type: 'duo',
|
|
|
|
host: params.Host,
|
|
|
|
signature: params.Signature,
|
2018-04-11 05:49:46 +02:00
|
|
|
},
|
2018-04-04 22:53:41 +02:00
|
|
|
});
|
|
|
|
}, 500);
|
2018-04-06 21:33:20 +02:00
|
|
|
}
|
2018-04-04 22:53:41 +02:00
|
|
|
|
2018-04-06 21:33:20 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
|
|
|
|
super.ngOnDestroy();
|
2018-04-04 22:53:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-04 16:08:30 +02:00
|
|
|
anotherMethod() {
|
|
|
|
this.router.navigate(['2fa-options']);
|
|
|
|
}
|
2018-04-06 21:33:20 +02:00
|
|
|
|
|
|
|
async submitWithTab(sendSuccessToTab: any) {
|
|
|
|
await super.submit();
|
|
|
|
if (sendSuccessToTab != null) {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
BrowserApi.tabSendMessage(sendSuccessToTab, {
|
|
|
|
command: '2faPageData',
|
2018-04-11 05:49:46 +02:00
|
|
|
data: { type: 'success' },
|
2018-04-06 21:33:20 +02:00
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
2018-04-04 16:08:30 +02:00
|
|
|
}
|