bitwarden-estensione-browser/src/popup/accounts/two-factor.component.ts

124 lines
4.9 KiB
TypeScript
Raw Normal View History

2018-04-06 21:33:20 +02:00
import {
ChangeDetectorRef,
Component,
NgZone,
} from '@angular/core';
import {
ActivatedRoute,
Router,
} from '@angular/router';
import { TwoFactorProviderType } from 'jslib-common/enums/twoFactorProviderType';
2018-04-04 22:53:41 +02:00
import { ApiService } from 'jslib-common/abstractions/api.service';
import { AuthService } from 'jslib-common/abstractions/auth.service';
import { EnvironmentService } from 'jslib-common/abstractions/environment.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { StateService } from 'jslib-common/abstractions/state.service';
import { StorageService } from 'jslib-common/abstractions/storage.service';
import { SyncService } from 'jslib-common/abstractions/sync.service';
import { BroadcasterService } from 'jslib-angular/services/broadcaster.service';
2018-04-06 21:33:20 +02:00
import { TwoFactorComponent as BaseTwoFactorComponent } from 'jslib-angular/components/two-factor.component';
2018-12-03 19:47:40 +01:00
import { PopupUtilsService } from '../services/popup-utils.service';
2021-03-02 19:31:52 +01:00
import { BrowserApi } from '../../browser/browserApi';
2018-12-03 19:47:40 +01:00
2018-04-06 21:33:20 +02:00
const BroadcasterSubscriptionId = 'TwoFactorComponent';
@Component({
selector: 'app-two-factor',
2018-04-06 17:48:45 +02:00
templateUrl: 'two-factor.component.html',
})
export class TwoFactorComponent extends BaseTwoFactorComponent {
2018-04-04 22:53:41 +02:00
showNewWindowMessage = false;
constructor(authService: AuthService, router: Router,
i18nService: I18nService, apiService: ApiService,
platformUtilsService: PlatformUtilsService, private syncService: SyncService,
2018-04-06 21:33:20 +02:00
environmentService: EnvironmentService, private ngZone: NgZone,
2018-12-03 19:47:40 +01:00
private broadcasterService: BroadcasterService, private changeDetectorRef: ChangeDetectorRef,
2019-07-02 14:26:33 +02:00
private popupUtilsService: PopupUtilsService, stateService: StateService,
storageService: StorageService, route: ActivatedRoute, private messagingService: MessagingService) {
2019-07-02 14:26:33 +02:00
super(authService, router, i18nService, apiService, platformUtilsService, window, environmentService,
stateService, storageService, route);
2018-07-13 16:50:32 +02:00
super.onSuccessfulLogin = () => {
2018-04-25 18:08:23 +02:00
return syncService.fullSync(true);
};
super.successRoute = '/tabs/vault';
this.webAuthnNewTab = this.platformUtilsService.isFirefox() || this.platformUtilsService.isSafari();
}
2018-04-04 22:53:41 +02:00
async ngOnInit() {
if (this.route.snapshot.paramMap.has('webAuthnResponse')) {
// WebAuthn fallback response
this.selectedProviderType = TwoFactorProviderType.WebAuthn;
this.token = this.route.snapshot.paramMap.get('webAuthnResponse');
super.onSuccessfulLogin = async () => {
this.syncService.fullSync(true);
this.messagingService.send('reloadPopup');
window.close();
};
this.remember = this.route.snapshot.paramMap.get('remember') === 'true';
await this.doSubmit();
return;
}
2018-04-04 22:53:41 +02:00
await super.ngOnInit();
if (this.selectedProviderType == null) {
return;
}
// WebAuthn prompt appears inside the popup on linux, and requires a larger popup width
// than usual to avoid cutting off the dialog.
if (this.selectedProviderType === TwoFactorProviderType.WebAuthn && await this.isLinux()) {
document.body.classList.add('linux-webauthn');
}
if (this.selectedProviderType === TwoFactorProviderType.Email &&
2018-12-03 19:47:40 +01:00
this.popupUtilsService.inPopup(window)) {
const confirmed = await this.platformUtilsService.showDialog(this.i18nService.t('popup2faCloseMessage'),
null, this.i18nService.t('yes'), this.i18nService.t('no'));
if (confirmed) {
this.popupUtilsService.popOut(window);
}
}
2021-03-02 19:31:52 +01:00
const queryParamsSub = this.route.queryParams.subscribe(async qParams => {
if (qParams.sso === 'true') {
super.onSuccessfulLogin = () => {
BrowserApi.reloadOpenWindows();
const thisWindow = window.open('', '_self');
thisWindow.close();
return this.syncService.fullSync(true);
};
if (queryParamsSub != null) {
queryParamsSub.unsubscribe();
}
}
});
2018-04-06 21:33:20 +02:00
}
2018-04-04 22:53:41 +02:00
async ngOnDestroy() {
2018-04-06 21:33:20 +02:00
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
if (this.selectedProviderType === TwoFactorProviderType.WebAuthn && await this.isLinux()) {
document.body.classList.remove('linux-webauthn');
}
2018-04-06 21:33:20 +02:00
super.ngOnDestroy();
2018-04-04 22:53:41 +02:00
}
anotherMethod() {
this.router.navigate(['2fa-options']);
}
async isLinux() {
return (await BrowserApi.getPlatformInfo()).os === 'linux';
}
}