1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-27 17:18:04 +01:00

Merge pull request #2132 from bitwarden/unlock-on-autofill

Unlock on autofill (shortcut and contextMenu)
This commit is contained in:
Daniel James Smith 2021-10-22 13:49:13 +02:00 committed by GitHub
commit 439b1f52c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 31 deletions

View File

@ -5,6 +5,7 @@ import MainBackground from './main.background';
import { PasswordGenerationService } from 'jslib-common/abstractions/passwordGeneration.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service';
import LockedVaultPendingNotificationsItem from './models/lockedVaultPendingNotificationsItem';
export default class CommandsBackground {
private isSafari: boolean;
@ -17,20 +18,24 @@ export default class CommandsBackground {
}
async init() {
if (this.isVivaldi) {
BrowserApi.messageListener('commands.background', async (msg: any, sender: any, sendResponse: any) => {
if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) {
await this.processCommand(msg.shortcut, sender);
}
});
} else if (chrome && chrome.commands) {
chrome.commands.onCommand.addListener(async (command: any) => {
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);
}
if (this.isVivaldi && msg.command === 'keyboardShortcutTriggered' && msg.shortcut) {
await this.processCommand(msg.shortcut, sender);
}
});
if (!this.isVivaldi && chrome && chrome.commands) {
chrome.commands.onCommand.addListener(async (command: string) => {
await this.processCommand(command);
});
}
}
private async processCommand(command: string, sender?: any) {
private async processCommand(command: string, sender?: chrome.runtime.MessageSender) {
switch (command) {
case 'generate_password':
await this.generatePasswordToClipboard();
@ -56,11 +61,7 @@ export default class CommandsBackground {
this.passwordGenerationService.addHistory(password);
}
private async autoFillLogin(tab?: any) {
if (await this.vaultTimeoutService.isLocked()) {
return;
}
private async autoFillLogin(tab?: chrome.tabs.Tab) {
if (!tab) {
tab = await BrowserApi.getTabFromCurrentWindowId();
}
@ -69,6 +70,20 @@ export default class CommandsBackground {
return;
}
if (await this.vaultTimeoutService.isLocked()) {
const retryMessage: LockedVaultPendingNotificationsItem = {
commandToRetry: {
msg: { command: 'autofill_login' },
sender: { tab: tab },
},
target: 'commands.background',
};
await BrowserApi.tabSendMessageData(tab, 'addToLockedVaultPendingNotifications', retryMessage);
BrowserApi.tabSendMessageData(tab, 'promptForLogin');
return;
}
await this.main.collectPageDetailsForContentScript(tab, 'autofill_cmd');
}

View File

@ -11,8 +11,10 @@ import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.serv
import { EventType } from 'jslib-common/enums/eventType';
import { CipherView } from 'jslib-common/models/view/cipherView';
import LockedVaultPendingNotificationsItem from './models/lockedVaultPendingNotificationsItem';
export default class ContextMenusBackground {
private readonly noopCommandSuffix = 'noop';
private contextMenus: any;
constructor(private main: MainBackground, private cipherService: CipherService,
@ -27,16 +29,22 @@ export default class ContextMenusBackground {
return;
}
this.contextMenus.onClicked.addListener(async (info: any, tab: any) => {
this.contextMenus.onClicked.addListener(async (info: chrome.contextMenus.OnClickData, tab: chrome.tabs.Tab) => {
if (info.menuItemId === 'generate-password') {
await this.generatePasswordToClipboard();
} else if (info.menuItemId === 'copy-identifier') {
await this.getClickedElement(info.frameId);
await this.getClickedElement(tab, info.frameId);
} else if (info.parentMenuItemId === 'autofill' ||
info.parentMenuItemId === 'copy-username' ||
info.parentMenuItemId === 'copy-password' ||
info.parentMenuItemId === 'copy-totp') {
await this.cipherAction(info);
await this.cipherAction(tab, info);
}
});
BrowserApi.messageListener('contextmenus.background', 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);
}
});
}
@ -48,8 +56,7 @@ export default class ContextMenusBackground {
this.passwordGenerationService.addHistory(password);
}
private async getClickedElement(frameId: number) {
const tab = await BrowserApi.getTabFromCurrentWindow();
private async getClickedElement(tab: chrome.tabs.Tab, frameId: number) {
if (tab == null) {
return;
}
@ -57,27 +64,38 @@ export default class ContextMenusBackground {
BrowserApi.tabSendMessage(tab, { command: 'getClickedElement' }, { frameId: frameId });
}
private async cipherAction(info: any) {
private async cipherAction(tab: chrome.tabs.Tab, info: chrome.contextMenus.OnClickData) {
const id = info.menuItemId.split('_')[1];
if (id === 'noop') {
if (chrome.browserAction && (chrome.browserAction as any).openPopup) {
(chrome.browserAction as any).openPopup();
}
return;
}
if (await this.vaultTimeoutService.isLocked()) {
const retryMessage: LockedVaultPendingNotificationsItem = {
commandToRetry: {
msg: { command: this.noopCommandSuffix, data: info },
sender: { tab: tab },
},
target: 'contextmenus.background',
};
await BrowserApi.tabSendMessageData(tab, 'addToLockedVaultPendingNotifications', retryMessage);
BrowserApi.tabSendMessageData(tab, 'promptForLogin');
return;
}
const ciphers = await this.cipherService.getAllDecrypted();
const cipher = ciphers.find(c => c.id === id);
let cipher: CipherView;
if (id === this.noopCommandSuffix) {
const ciphers = await this.cipherService.getAllDecryptedForUrl(tab.url);
cipher = ciphers.length > 0 ? ciphers[0] : null;
} else {
const ciphers = await this.cipherService.getAllDecrypted();
cipher = ciphers.find(c => c.id === id);
}
if (cipher == null) {
return;
}
if (info.parentMenuItemId === 'autofill') {
await this.startAutofillPage(cipher);
await this.startAutofillPage(tab, cipher);
} else if (info.parentMenuItemId === 'copy-username') {
this.platformUtilsService.copyToClipboard(cipher.login.username, { window: window });
} else if (info.parentMenuItemId === 'copy-password') {
@ -89,9 +107,8 @@ export default class ContextMenusBackground {
}
}
private async startAutofillPage(cipher: CipherView) {
private async startAutofillPage(tab: chrome.tabs.Tab, cipher: CipherView) {
this.main.loginToAutoFill = cipher;
const tab = await BrowserApi.getTabFromCurrentWindow();
if (tab == null) {
return;
}