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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

141 lines
4.8 KiB
TypeScript
Raw Normal View History

import { Component } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { first } from "rxjs/operators";
2022-06-14 17:10:53 +02:00
import { TwoFactorComponent as BaseTwoFactorComponent } from "@bitwarden/angular/components/two-factor.component";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { AppIdService } from "@bitwarden/common/abstractions/appId.service";
import { AuthService } from "@bitwarden/common/abstractions/auth.service";
import { BroadcasterService } from "@bitwarden/common/abstractions/broadcaster.service";
import { EnvironmentService } from "@bitwarden/common/abstractions/environment.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/abstractions/log.service";
import { MessagingService } from "@bitwarden/common/abstractions/messaging.service";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { StateService } from "@bitwarden/common/abstractions/state.service";
import { SyncService } from "@bitwarden/common/abstractions/sync.service";
import { TwoFactorService } from "@bitwarden/common/abstractions/twoFactor.service";
import { TwoFactorProviderType } from "@bitwarden/common/enums/twoFactorProviderType";
2021-03-02 19:31:52 +01:00
import { BrowserApi } from "../../browser/browserApi";
2022-02-24 18:14:04 +01:00
import { PopupUtilsService } from "../services/popup-utils.service";
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;
2021-12-21 15:43:35 +01:00
constructor(
authService: AuthService,
router: Router,
i18nService: I18nService,
apiService: ApiService,
platformUtilsService: PlatformUtilsService,
private syncService: SyncService,
environmentService: EnvironmentService,
private broadcasterService: BroadcasterService,
2019-07-02 14:26:33 +02:00
private popupUtilsService: PopupUtilsService,
stateService: StateService,
route: ActivatedRoute,
private messagingService: MessagingService,
logService: LogService,
twoFactorService: TwoFactorService,
appIdService: AppIdService
) {
2019-07-02 14:26:33 +02:00
super(
authService,
router,
i18nService,
apiService,
platformUtilsService,
window,
environmentService,
stateService,
route,
logService,
twoFactorService,
appIdService
);
super.onSuccessfulLogin = () => {
return syncService.fullSync(true);
2018-04-25 18:08:23 +02:00
};
super.successRoute = "/tabs/vault";
this.webAuthnNewTab =
this.platformUtilsService.isFirefox() || this.platformUtilsService.isSafari();
2021-12-21 15:43:35 +01:00
}
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();
2021-12-21 15:43:35 +01:00
};
this.remember = this.route.snapshot.paramMap.get("remember") === "true";
await this.doSubmit();
2021-12-21 15:43:35 +01:00
return;
}
2018-04-04 22:53:41 +02:00
await super.ngOnInit();
if (this.selectedProviderType == null) {
return;
2018-04-06 21:33:20 +02:00
}
2018-04-04 22:53:41 +02:00
2018-04-06 21:33:20 +02:00
// 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");
2018-04-04 22:53:41 +02:00
}
2021-12-21 15:43:35 +01:00
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"),
2021-12-21 15:43:35 +01:00
null,
2018-12-03 19:47:40 +01:00
this.i18nService.t("yes"),
this.i18nService.t("no")
2021-12-21 15:43:35 +01:00
);
if (confirmed) {
this.popupUtilsService.popOut(window);
2021-12-21 15:43:35 +01:00
}
}
this.route.queryParams.pipe(first()).subscribe(async (qParams) => {
if (qParams.sso === "true") {
super.onSuccessfulLogin = () => {
BrowserApi.reloadOpenWindows();
const thisWindow = window.open("", "_self");
thisWindow.close();
return this.syncService.fullSync(true);
2021-12-21 15:43:35 +01:00
};
}
});
}
async ngOnDestroy() {
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
2021-12-21 15:43:35 +01:00
if (this.selectedProviderType === TwoFactorProviderType.WebAuthn && (await this.isLinux())) {
document.body.classList.remove("linux-webauthn");
}
2018-04-06 21:33:20 +02:00
super.ngOnDestroy();
2021-12-21 15:43:35 +01:00
}
anotherMethod() {
this.router.navigate(["2fa-options"]);
2021-12-21 15:43:35 +01:00
}
async isLinux() {
return (await BrowserApi.getPlatformInfo()).os === "linux";
2021-12-21 15:43:35 +01:00
}
}