2022-06-14 17:10:53 +02:00
|
|
|
import { AuthService } from "@bitwarden/common/abstractions/auth.service";
|
|
|
|
import { CipherService } from "@bitwarden/common/abstractions/cipher.service";
|
|
|
|
import { EventService } from "@bitwarden/common/abstractions/event.service";
|
|
|
|
import { PasswordGenerationService } from "@bitwarden/common/abstractions/passwordGeneration.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
|
|
import { TotpService } from "@bitwarden/common/abstractions/totp.service";
|
|
|
|
import { AuthenticationStatus } from "@bitwarden/common/enums/authenticationStatus";
|
|
|
|
import { CipherRepromptType } from "@bitwarden/common/enums/cipherRepromptType";
|
|
|
|
import { EventType } from "@bitwarden/common/enums/eventType";
|
|
|
|
import { CipherView } from "@bitwarden/common/models/view/cipherView";
|
2022-02-24 18:14:04 +01:00
|
|
|
|
|
|
|
import { BrowserApi } from "../browser/browserApi";
|
|
|
|
|
|
|
|
import MainBackground from "./main.background";
|
2021-10-19 16:08:40 +02:00
|
|
|
import LockedVaultPendingNotificationsItem from "./models/lockedVaultPendingNotificationsItem";
|
2018-01-07 04:13:48 +01:00
|
|
|
|
2017-12-07 22:02:15 +01:00
|
|
|
export default class ContextMenusBackground {
|
2021-10-20 17:45:19 +02:00
|
|
|
private readonly noopCommandSuffix = "noop";
|
2017-12-07 22:02:15 +01:00
|
|
|
private contextMenus: any;
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2017-12-07 22:02:15 +01:00
|
|
|
constructor(
|
|
|
|
private main: MainBackground,
|
|
|
|
private cipherService: CipherService,
|
2021-04-14 23:43:09 +02:00
|
|
|
private passwordGenerationService: PasswordGenerationService,
|
2020-04-06 17:40:16 +02:00
|
|
|
private platformUtilsService: PlatformUtilsService,
|
2022-05-01 23:57:40 +02:00
|
|
|
private authService: AuthService,
|
2020-03-03 16:40:06 +01:00
|
|
|
private eventService: EventService,
|
|
|
|
private totpService: TotpService
|
|
|
|
) {
|
2017-12-07 22:02:15 +01:00
|
|
|
this.contextMenus = chrome.contextMenus;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 22:02:15 +01:00
|
|
|
async init() {
|
|
|
|
if (!this.contextMenus) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.contextMenus.onClicked.addListener(
|
2021-10-19 16:00:38 +02:00
|
|
|
async (info: chrome.contextMenus.OnClickData, tab: chrome.tabs.Tab) => {
|
2017-12-07 22:02:15 +01:00
|
|
|
if (info.menuItemId === "generate-password") {
|
|
|
|
await this.generatePasswordToClipboard();
|
|
|
|
} else if (info.menuItemId === "copy-identifier") {
|
|
|
|
await this.getClickedElement(tab, info.frameId);
|
2020-03-03 16:40:06 +01:00
|
|
|
} else if (
|
|
|
|
info.parentMenuItemId === "autofill" ||
|
2017-12-07 22:02:15 +01:00
|
|
|
info.parentMenuItemId === "copy-username" ||
|
2020-03-03 16:42:49 +01:00
|
|
|
info.parentMenuItemId === "copy-password" ||
|
|
|
|
info.parentMenuItemId === "copy-totp"
|
2021-12-21 15:43:35 +01:00
|
|
|
) {
|
2021-10-19 16:03:39 +02:00
|
|
|
await this.cipherAction(tab, info);
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-10-19 16:00:38 +02:00
|
|
|
BrowserApi.messageListener(
|
|
|
|
"contextmenus.background",
|
2021-10-19 16:10:43 +02:00
|
|
|
async (msg: any, sender: chrome.runtime.MessageSender, sendResponse: any) => {
|
|
|
|
if (msg.command === "unlockCompleted" && msg.data.target === "contextmenus.background") {
|
|
|
|
await this.cipherAction(
|
|
|
|
msg.data.commandToRetry.sender.tab,
|
|
|
|
msg.data.commandToRetry.msg.data
|
2021-12-21 15:43:35 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-19 16:10:43 +02:00
|
|
|
private async generatePasswordToClipboard() {
|
2020-02-28 18:43:27 +01:00
|
|
|
const options = (await this.passwordGenerationService.getOptions())[0];
|
2021-10-19 16:10:43 +02:00
|
|
|
const password = await this.passwordGenerationService.generatePassword(options);
|
|
|
|
this.platformUtilsService.copyToClipboard(password, { window: window });
|
2017-12-07 22:02:15 +01:00
|
|
|
this.passwordGenerationService.addHistory(password);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 16:04:32 +02:00
|
|
|
private async getClickedElement(tab: chrome.tabs.Tab, frameId: number) {
|
2021-09-01 23:51:43 +02:00
|
|
|
if (tab == null) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BrowserApi.tabSendMessage(tab, { command: "getClickedElement" }, { frameId: frameId });
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 22:02:15 +01:00
|
|
|
private async cipherAction(tab: chrome.tabs.Tab, info: chrome.contextMenus.OnClickData) {
|
2020-02-28 18:43:27 +01:00
|
|
|
const id = info.menuItemId.split("_")[1];
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2022-05-01 23:57:40 +02:00
|
|
|
if ((await this.authService.getAuthStatus()) < AuthenticationStatus.Unlocked) {
|
2018-04-23 19:04:11 +02:00
|
|
|
const retryMessage: LockedVaultPendingNotificationsItem = {
|
2018-08-13 15:44:59 +02:00
|
|
|
commandToRetry: {
|
|
|
|
msg: { command: this.noopCommandSuffix, data: info },
|
|
|
|
sender: { tab: tab },
|
2021-12-21 15:43:35 +01:00
|
|
|
},
|
2017-12-07 22:02:15 +01:00
|
|
|
target: "contextmenus.background",
|
|
|
|
};
|
|
|
|
await BrowserApi.tabSendMessageData(
|
2021-12-21 15:43:35 +01:00
|
|
|
tab,
|
2017-12-07 22:02:15 +01:00
|
|
|
"addToLockedVaultPendingNotifications",
|
|
|
|
retryMessage
|
2021-12-21 15:43:35 +01:00
|
|
|
);
|
|
|
|
|
2017-12-07 22:02:15 +01:00
|
|
|
BrowserApi.tabSendMessageData(tab, "promptForLogin");
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 16:04:32 +02:00
|
|
|
let cipher: CipherView;
|
|
|
|
if (id === this.noopCommandSuffix) {
|
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(tab.url);
|
2021-09-01 23:51:43 +02:00
|
|
|
cipher = ciphers.find((c) => c.reprompt === CipherRepromptType.None);
|
2021-10-07 01:52:33 +02:00
|
|
|
} else {
|
|
|
|
const ciphers = await this.cipherService.getAllDecrypted();
|
|
|
|
cipher = ciphers.find((c) => c.id === id);
|
2021-09-01 23:51:43 +02:00
|
|
|
}
|
|
|
|
|
2021-10-20 17:45:19 +02:00
|
|
|
if (cipher == null) {
|
2019-03-06 22:50:04 +01:00
|
|
|
return;
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 16:03:39 +02:00
|
|
|
if (info.parentMenuItemId === "autofill") {
|
|
|
|
await this.startAutofillPage(tab, cipher);
|
2017-12-07 22:02:15 +01:00
|
|
|
} else if (info.parentMenuItemId === "copy-username") {
|
|
|
|
this.platformUtilsService.copyToClipboard(cipher.login.username, { window: window });
|
|
|
|
} else if (info.parentMenuItemId === "copy-password") {
|
2019-03-06 22:50:04 +01:00
|
|
|
this.platformUtilsService.copyToClipboard(cipher.login.password, { window: window });
|
2017-12-07 22:02:15 +01:00
|
|
|
this.eventService.collect(EventType.Cipher_ClientCopiedPassword, cipher.id);
|
|
|
|
} else if (info.parentMenuItemId === "copy-totp") {
|
|
|
|
const totpValue = await this.totpService.getCode(cipher.login.totp);
|
2020-03-03 16:40:06 +01:00
|
|
|
this.platformUtilsService.copyToClipboard(totpValue, { window: window });
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2017-12-07 22:02:15 +01:00
|
|
|
|
2018-01-12 20:44:44 +01:00
|
|
|
private async startAutofillPage(tab: chrome.tabs.Tab, cipher: CipherView) {
|
2017-12-07 22:02:15 +01:00
|
|
|
this.main.loginToAutoFill = cipher;
|
|
|
|
if (tab == null) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-10-19 16:08:40 +02:00
|
|
|
BrowserApi.tabSendMessage(tab, {
|
2017-12-07 22:02:15 +01:00
|
|
|
command: "collectPageDetails",
|
2021-12-21 15:43:35 +01:00
|
|
|
tab: tab,
|
2017-12-07 22:02:15 +01:00
|
|
|
sender: "contextMenu",
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
}
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|