2022-06-14 17:10:53 +02:00
|
|
|
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 { NotificationsService } from "@bitwarden/common/abstractions/notifications.service";
|
|
|
|
import { SystemService } from "@bitwarden/common/abstractions/system.service";
|
|
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
2021-10-21 11:10:46 +02:00
|
|
|
|
2022-02-24 18:14:04 +01:00
|
|
|
import { BrowserApi } from "../browser/browserApi";
|
2021-12-21 15:43:35 +01:00
|
|
|
import { AutofillService } from "../services/abstractions/autofill.service";
|
2022-09-29 16:38:50 +02:00
|
|
|
import { BrowserEnvironmentService } from "../services/browser-environment.service";
|
2021-12-21 15:43:35 +01:00
|
|
|
import BrowserPlatformUtilsService from "../services/browserPlatformUtils.service";
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
import MainBackground from "./main.background";
|
|
|
|
import LockedVaultPendingNotificationsItem from "./models/lockedVaultPendingNotificationsItem";
|
2021-10-18 16:41:42 +02:00
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
export default class RuntimeBackground {
|
2021-12-21 15:43:35 +01:00
|
|
|
private autofillTimeout: any;
|
|
|
|
private pageDetailsToAutoFill: any[] = [];
|
|
|
|
private onInstalledReason: string = null;
|
|
|
|
private lockedVaultPendingNotifications: LockedVaultPendingNotificationsItem[] = [];
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private main: MainBackground,
|
|
|
|
private autofillService: AutofillService,
|
|
|
|
private platformUtilsService: BrowserPlatformUtilsService,
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private notificationsService: NotificationsService,
|
|
|
|
private systemService: SystemService,
|
2022-08-08 13:40:09 +02:00
|
|
|
private environmentService: BrowserEnvironmentService,
|
2021-12-21 15:43:35 +01:00
|
|
|
private messagingService: MessagingService,
|
|
|
|
private logService: LogService
|
|
|
|
) {
|
|
|
|
// onInstalled listener must be wired up before anything else, so we do it in the ctor
|
|
|
|
chrome.runtime.onInstalled.addListener((details: any) => {
|
|
|
|
this.onInstalledReason = details.reason;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
if (!chrome.runtime) {
|
|
|
|
return;
|
2020-08-11 22:25:07 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
await this.checkOnInstalled();
|
2022-02-15 23:06:35 +01:00
|
|
|
const backgroundMessageListener = async (
|
|
|
|
msg: any,
|
|
|
|
sender: chrome.runtime.MessageSender,
|
|
|
|
sendResponse: any
|
|
|
|
) => {
|
|
|
|
await this.processMessage(msg, sender, sendResponse);
|
|
|
|
};
|
|
|
|
|
|
|
|
BrowserApi.messageListener("runtime.background", backgroundMessageListener);
|
2022-10-19 15:55:57 +02:00
|
|
|
if (this.main.popupOnlyContext) {
|
2022-02-15 23:06:35 +01:00
|
|
|
(window as any).bitwardenBackgroundMessageListener = backgroundMessageListener;
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async processMessage(msg: any, sender: any, sendResponse: any) {
|
|
|
|
switch (msg.command) {
|
|
|
|
case "loggedIn":
|
2022-02-24 18:14:04 +01:00
|
|
|
case "unlocked": {
|
2021-12-21 15:43:35 +01:00
|
|
|
let item: LockedVaultPendingNotificationsItem;
|
|
|
|
|
2022-02-15 23:06:35 +01:00
|
|
|
if (this.lockedVaultPendingNotifications?.length > 0) {
|
2021-12-21 15:43:35 +01:00
|
|
|
await BrowserApi.closeLoginTab();
|
|
|
|
|
|
|
|
item = this.lockedVaultPendingNotifications.pop();
|
|
|
|
if (item.commandToRetry.sender?.tab?.id) {
|
|
|
|
await BrowserApi.focusSpecifiedTab(item.commandToRetry.sender.tab.id);
|
|
|
|
}
|
2017-12-07 21:36:24 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 15:55:57 +02:00
|
|
|
await this.main.refreshBadge();
|
|
|
|
await this.main.refreshMenu(false);
|
2021-12-21 15:43:35 +01:00
|
|
|
this.notificationsService.updateConnection(msg.command === "unlocked");
|
|
|
|
this.systemService.cancelProcessReload();
|
|
|
|
|
|
|
|
if (item) {
|
|
|
|
await BrowserApi.tabSendMessageData(
|
|
|
|
item.commandToRetry.sender.tab,
|
|
|
|
"unlockCompleted",
|
|
|
|
item
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
2022-02-24 18:14:04 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
case "addToLockedVaultPendingNotifications":
|
|
|
|
this.lockedVaultPendingNotifications.push(msg.data);
|
|
|
|
break;
|
|
|
|
case "logout":
|
2022-01-27 22:22:51 +01:00
|
|
|
await this.main.logout(msg.expired, msg.userId);
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
|
|
|
case "syncCompleted":
|
|
|
|
if (msg.successfully) {
|
2022-10-19 15:55:57 +02:00
|
|
|
setTimeout(async () => {
|
|
|
|
await this.main.refreshBadge();
|
|
|
|
await this.main.refreshMenu();
|
|
|
|
}, 2000);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "openPopup":
|
|
|
|
await this.main.openPopup();
|
|
|
|
break;
|
|
|
|
case "promptForLogin":
|
|
|
|
await BrowserApi.createNewTab("popup/index.html?uilocation=popout", true, true);
|
|
|
|
break;
|
|
|
|
case "showDialogResolve":
|
|
|
|
this.platformUtilsService.resolveDialogPromise(msg.dialogId, msg.confirmed);
|
|
|
|
break;
|
|
|
|
case "bgCollectPageDetails":
|
|
|
|
await this.main.collectPageDetailsForContentScript(sender.tab, msg.sender, sender.frameId);
|
|
|
|
break;
|
|
|
|
case "bgUpdateContextMenu":
|
|
|
|
case "editedCipher":
|
|
|
|
case "addedCipher":
|
|
|
|
case "deletedCipher":
|
2022-10-19 15:55:57 +02:00
|
|
|
await this.main.refreshBadge();
|
|
|
|
await this.main.refreshMenu();
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
|
|
|
case "bgReseedStorage":
|
|
|
|
await this.main.reseedStorage();
|
|
|
|
break;
|
|
|
|
case "collectPageDetailsResponse":
|
|
|
|
switch (msg.sender) {
|
|
|
|
case "autofiller":
|
2022-02-24 18:14:04 +01:00
|
|
|
case "autofill_cmd": {
|
2021-12-21 15:43:35 +01:00
|
|
|
const totpCode = await this.autofillService.doAutoFillActiveTab(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
frameId: sender.frameId,
|
|
|
|
tab: msg.tab,
|
|
|
|
details: msg.details,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
msg.sender === "autofill_cmd"
|
|
|
|
);
|
|
|
|
if (totpCode != null) {
|
|
|
|
this.platformUtilsService.copyToClipboard(totpCode, { window: window });
|
|
|
|
}
|
|
|
|
break;
|
2022-02-24 18:14:04 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
case "contextMenu":
|
|
|
|
clearTimeout(this.autofillTimeout);
|
|
|
|
this.pageDetailsToAutoFill.push({
|
|
|
|
frameId: sender.frameId,
|
|
|
|
tab: msg.tab,
|
|
|
|
details: msg.details,
|
|
|
|
});
|
2022-07-21 19:56:09 +02:00
|
|
|
this.autofillTimeout = setTimeout(async () => await this.autofillPage(msg.tab), 300);
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2022-02-24 18:14:04 +01:00
|
|
|
case "authResult": {
|
2021-12-21 15:43:35 +01:00
|
|
|
const vaultUrl = this.environmentService.getWebVaultUrl();
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
if (msg.referrer == null || Utils.getHostname(vaultUrl) !== msg.referrer) {
|
|
|
|
return;
|
2018-01-12 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
try {
|
|
|
|
BrowserApi.createNewTab(
|
|
|
|
"popup/index.html?uilocation=popout#/sso?code=" +
|
|
|
|
encodeURIComponent(msg.code) +
|
|
|
|
"&state=" +
|
|
|
|
encodeURIComponent(msg.state)
|
|
|
|
);
|
|
|
|
} catch {
|
|
|
|
this.logService.error("Unable to open sso popout tab");
|
|
|
|
}
|
|
|
|
break;
|
2022-02-24 18:14:04 +01:00
|
|
|
}
|
|
|
|
case "webAuthnResult": {
|
|
|
|
const vaultUrl = this.environmentService.getWebVaultUrl();
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2022-02-24 18:14:04 +01:00
|
|
|
if (msg.referrer == null || Utils.getHostname(vaultUrl) !== msg.referrer) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2018-04-23 16:02:30 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
const params =
|
|
|
|
`webAuthnResponse=${encodeURIComponent(msg.data)};` +
|
|
|
|
`remember=${encodeURIComponent(msg.remember)}`;
|
|
|
|
BrowserApi.createNewTab(
|
|
|
|
`popup/index.html?uilocation=popout#/2fa;${params}`,
|
|
|
|
undefined,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
break;
|
2022-02-24 18:14:04 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
case "reloadPopup":
|
|
|
|
this.messagingService.send("reloadPopup");
|
|
|
|
break;
|
|
|
|
case "emailVerificationRequired":
|
|
|
|
this.messagingService.send("showDialog", {
|
|
|
|
dialogId: "emailVerificationRequired",
|
|
|
|
title: this.i18nService.t("emailVerificationRequired"),
|
|
|
|
text: this.i18nService.t("emailVerificationRequiredDesc"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
|
|
|
type: "info",
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "getClickedElementResponse":
|
|
|
|
this.platformUtilsService.copyToClipboard(msg.identifier, { window: window });
|
2022-02-24 18:14:04 +01:00
|
|
|
break;
|
2021-12-21 15:43:35 +01:00
|
|
|
default:
|
|
|
|
break;
|
2017-12-07 21:36:24 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2022-07-21 19:56:09 +02:00
|
|
|
private async autofillPage(tabToAutoFill: chrome.tabs.Tab) {
|
2021-12-21 15:43:35 +01:00
|
|
|
const totpCode = await this.autofillService.doAutoFill({
|
2022-07-21 19:56:09 +02:00
|
|
|
tab: tabToAutoFill,
|
2021-12-21 15:43:35 +01:00
|
|
|
cipher: this.main.loginToAutoFill,
|
|
|
|
pageDetails: this.pageDetailsToAutoFill,
|
|
|
|
fillNewPassword: true,
|
|
|
|
});
|
2018-01-18 05:21:17 +01:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
if (totpCode != null) {
|
|
|
|
this.platformUtilsService.copyToClipboard(totpCode, { window: window });
|
2018-01-19 17:42:35 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
// reset
|
|
|
|
this.main.loginToAutoFill = null;
|
|
|
|
this.pageDetailsToAutoFill = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
private async checkOnInstalled() {
|
|
|
|
setTimeout(async () => {
|
|
|
|
if (this.onInstalledReason != null) {
|
|
|
|
if (this.onInstalledReason === "install") {
|
|
|
|
BrowserApi.createNewTab("https://bitwarden.com/browser-start/");
|
2022-08-08 13:40:09 +02:00
|
|
|
|
|
|
|
if (await this.environmentService.hasManagedEnvironment()) {
|
|
|
|
await this.environmentService.setUrlsToManagedEnvironment();
|
|
|
|
}
|
2020-04-06 17:40:16 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
this.onInstalledReason = null;
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|