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