2022-06-14 17:10:53 +02:00
|
|
|
import { AuthService } from "@bitwarden/common/abstractions/auth.service";
|
|
|
|
import { PasswordGenerationService } from "@bitwarden/common/abstractions/passwordGeneration.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
2022-08-30 16:11:19 +02:00
|
|
|
import { VaultTimeoutService } from "@bitwarden/common/abstractions/vaultTimeout/vaultTimeout.service";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { AuthenticationStatus } from "@bitwarden/common/enums/authenticationStatus";
|
2022-02-24 18:14:04 +01:00
|
|
|
|
|
|
|
import { BrowserApi } from "../browser/browserApi";
|
|
|
|
|
|
|
|
import MainBackground from "./main.background";
|
2021-10-19 15:55:36 +02:00
|
|
|
import LockedVaultPendingNotificationsItem from "./models/lockedVaultPendingNotificationsItem";
|
2018-01-07 04:13:48 +01:00
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
export default class CommandsBackground {
|
2018-01-17 15:12:16 +01:00
|
|
|
private isSafari: boolean;
|
2021-10-22 10:01:18 +02:00
|
|
|
private isVivaldi: boolean;
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2018-01-17 15:12:16 +01:00
|
|
|
constructor(
|
|
|
|
private main: MainBackground,
|
|
|
|
private passwordGenerationService: PasswordGenerationService,
|
2021-04-14 23:43:09 +02:00
|
|
|
private platformUtilsService: PlatformUtilsService,
|
2022-05-01 23:57:40 +02:00
|
|
|
private vaultTimeoutService: VaultTimeoutService,
|
|
|
|
private authService: AuthService
|
2021-04-14 23:43:09 +02:00
|
|
|
) {
|
2018-01-17 15:12:16 +01:00
|
|
|
this.isSafari = this.platformUtilsService.isSafari();
|
2021-10-22 10:01:18 +02:00
|
|
|
this.isVivaldi = this.platformUtilsService.isVivaldi();
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
async init() {
|
2021-10-19 15:55:36 +02:00
|
|
|
BrowserApi.messageListener(
|
|
|
|
"commands.background",
|
|
|
|
async (msg: any, sender: chrome.runtime.MessageSender, sendResponse: any) => {
|
|
|
|
if (msg.command === "unlockCompleted" && msg.data.target === "commands.background") {
|
|
|
|
await this.processCommand(
|
|
|
|
msg.data.commandToRetry.msg.command,
|
|
|
|
msg.data.commandToRetry.sender
|
2018-01-17 15:12:16 +01:00
|
|
|
);
|
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
);
|
2017-12-07 21:06:37 +01:00
|
|
|
|
2022-09-08 14:48:45 +02:00
|
|
|
if (chrome && chrome.commands) {
|
2021-10-19 14:15:15 +02:00
|
|
|
chrome.commands.onCommand.addListener(async (command: string) => {
|
2018-01-17 15:12:16 +01:00
|
|
|
await this.processCommand(command);
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 14:15:15 +02:00
|
|
|
private async processCommand(command: string, sender?: chrome.runtime.MessageSender) {
|
2018-01-17 15:12:16 +01:00
|
|
|
switch (command) {
|
|
|
|
case "generate_password":
|
|
|
|
await this.generatePasswordToClipboard();
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
2018-01-17 15:12:16 +01:00
|
|
|
case "autofill_login":
|
2018-01-17 16:11:09 +01:00
|
|
|
await this.autoFillLogin(sender ? sender.tab : null);
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
2018-01-17 15:12:16 +01:00
|
|
|
case "open_popup":
|
|
|
|
await this.openPopup();
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
2020-11-06 16:43:06 +01:00
|
|
|
case "lock_vault":
|
2022-08-25 19:09:27 +02:00
|
|
|
await this.vaultTimeoutService.lock();
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
private async generatePasswordToClipboard() {
|
2022-10-11 14:08:48 +02:00
|
|
|
const options = (await this.passwordGenerationService.getOptions())?.[0] ?? {};
|
2018-01-09 23:55:28 +01:00
|
|
|
const password = await this.passwordGenerationService.generatePassword(options);
|
2018-08-13 15:44:59 +02:00
|
|
|
this.platformUtilsService.copyToClipboard(password, { window: window });
|
2017-12-07 21:06:37 +01:00
|
|
|
this.passwordGenerationService.addHistory(password);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 14:15:15 +02:00
|
|
|
private async autoFillLogin(tab?: chrome.tabs.Tab) {
|
2018-01-17 16:11:09 +01:00
|
|
|
if (!tab) {
|
|
|
|
tab = await BrowserApi.getTabFromCurrentWindowId();
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tab == null) {
|
|
|
|
return;
|
2021-10-19 15:55:36 +02:00
|
|
|
}
|
|
|
|
|
2022-05-01 23:57:40 +02:00
|
|
|
if ((await this.authService.getAuthStatus()) < AuthenticationStatus.Unlocked) {
|
2019-03-06 22:50:04 +01:00
|
|
|
const retryMessage: LockedVaultPendingNotificationsItem = {
|
2021-10-19 15:55:36 +02:00
|
|
|
commandToRetry: {
|
2019-03-06 22:50:04 +01:00
|
|
|
msg: { command: "autofill_login" },
|
|
|
|
sender: { tab: tab },
|
2021-12-21 15:43:35 +01:00
|
|
|
},
|
2021-10-19 15:55:36 +02:00
|
|
|
target: "commands.background",
|
2021-12-21 15:43:35 +01:00
|
|
|
};
|
2019-03-06 22:50:04 +01:00
|
|
|
await BrowserApi.tabSendMessageData(
|
2021-12-21 15:43:35 +01:00
|
|
|
tab,
|
2019-03-06 22:50:04 +01:00
|
|
|
"addToLockedVaultPendingNotifications",
|
2021-10-19 15:55:36 +02:00
|
|
|
retryMessage
|
2021-12-21 15:43:35 +01:00
|
|
|
);
|
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
BrowserApi.tabSendMessageData(tab, "promptForLogin");
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
2018-01-17 15:12:16 +01:00
|
|
|
|
2018-01-18 22:17:58 +01:00
|
|
|
await this.main.collectPageDetailsForContentScript(tab, "autofill_cmd");
|
2018-01-17 15:12:16 +01:00
|
|
|
}
|
|
|
|
|
2018-01-18 22:17:58 +01:00
|
|
|
private async openPopup() {
|
2018-01-17 19:38:32 +01:00
|
|
|
// Chrome APIs cannot open popup
|
2018-01-18 22:17:58 +01:00
|
|
|
if (!this.isSafari) {
|
|
|
|
return;
|
2018-01-17 15:12:16 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2018-01-17 15:12:16 +01:00
|
|
|
this.main.openPopup();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|