1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-28 17:27:50 +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 { PasswordGenerationService } from 'jslib-common/abstractions/passwordGeneration.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service'; import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service'; import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service';
import LockedVaultPendingNotificationsItem from './models/lockedVaultPendingNotificationsItem';
export default class CommandsBackground { export default class CommandsBackground {
private isSafari: boolean; private isSafari: boolean;
@ -17,20 +18,24 @@ export default class CommandsBackground {
} }
async init() { async init() {
if (this.isVivaldi) { BrowserApi.messageListener('commands.background', async (msg: any, sender: chrome.runtime.MessageSender, sendResponse: any) => {
BrowserApi.messageListener('commands.background', async (msg: any, sender: any, sendResponse: any) => { if (msg.command === 'unlockCompleted' && msg.data.target === 'commands.background') {
if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) { await this.processCommand(msg.data.commandToRetry.msg.command, msg.data.commandToRetry.sender);
await this.processCommand(msg.shortcut, sender); }
}
}); if (this.isVivaldi && msg.command === 'keyboardShortcutTriggered' && msg.shortcut) {
} else if (chrome && chrome.commands) { await this.processCommand(msg.shortcut, sender);
chrome.commands.onCommand.addListener(async (command: any) => { }
});
if (!this.isVivaldi && chrome && chrome.commands) {
chrome.commands.onCommand.addListener(async (command: string) => {
await this.processCommand(command); await this.processCommand(command);
}); });
} }
} }
private async processCommand(command: string, sender?: any) { private async processCommand(command: string, sender?: chrome.runtime.MessageSender) {
switch (command) { switch (command) {
case 'generate_password': case 'generate_password':
await this.generatePasswordToClipboard(); await this.generatePasswordToClipboard();
@ -56,11 +61,7 @@ export default class CommandsBackground {
this.passwordGenerationService.addHistory(password); this.passwordGenerationService.addHistory(password);
} }
private async autoFillLogin(tab?: any) { private async autoFillLogin(tab?: chrome.tabs.Tab) {
if (await this.vaultTimeoutService.isLocked()) {
return;
}
if (!tab) { if (!tab) {
tab = await BrowserApi.getTabFromCurrentWindowId(); tab = await BrowserApi.getTabFromCurrentWindowId();
} }
@ -69,6 +70,20 @@ export default class CommandsBackground {
return; 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'); 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 { EventType } from 'jslib-common/enums/eventType';
import { CipherView } from 'jslib-common/models/view/cipherView'; import { CipherView } from 'jslib-common/models/view/cipherView';
import LockedVaultPendingNotificationsItem from './models/lockedVaultPendingNotificationsItem';
export default class ContextMenusBackground { export default class ContextMenusBackground {
private readonly noopCommandSuffix = 'noop';
private contextMenus: any; private contextMenus: any;
constructor(private main: MainBackground, private cipherService: CipherService, constructor(private main: MainBackground, private cipherService: CipherService,
@ -27,16 +29,22 @@ export default class ContextMenusBackground {
return; 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') { if (info.menuItemId === 'generate-password') {
await this.generatePasswordToClipboard(); await this.generatePasswordToClipboard();
} else if (info.menuItemId === 'copy-identifier') { } else if (info.menuItemId === 'copy-identifier') {
await this.getClickedElement(info.frameId); await this.getClickedElement(tab, info.frameId);
} else if (info.parentMenuItemId === 'autofill' || } else if (info.parentMenuItemId === 'autofill' ||
info.parentMenuItemId === 'copy-username' || info.parentMenuItemId === 'copy-username' ||
info.parentMenuItemId === 'copy-password' || info.parentMenuItemId === 'copy-password' ||
info.parentMenuItemId === 'copy-totp') { 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); this.passwordGenerationService.addHistory(password);
} }
private async getClickedElement(frameId: number) { private async getClickedElement(tab: chrome.tabs.Tab, frameId: number) {
const tab = await BrowserApi.getTabFromCurrentWindow();
if (tab == null) { if (tab == null) {
return; return;
} }
@ -57,27 +64,38 @@ export default class ContextMenusBackground {
BrowserApi.tabSendMessage(tab, { command: 'getClickedElement' }, { frameId: frameId }); 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]; 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()) { 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; return;
} }
const ciphers = await this.cipherService.getAllDecrypted(); let cipher: CipherView;
const cipher = ciphers.find(c => c.id === id); 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) { if (cipher == null) {
return; return;
} }
if (info.parentMenuItemId === 'autofill') { if (info.parentMenuItemId === 'autofill') {
await this.startAutofillPage(cipher); await this.startAutofillPage(tab, cipher);
} else if (info.parentMenuItemId === 'copy-username') { } else if (info.parentMenuItemId === 'copy-username') {
this.platformUtilsService.copyToClipboard(cipher.login.username, { window: window }); this.platformUtilsService.copyToClipboard(cipher.login.username, { window: window });
} else if (info.parentMenuItemId === 'copy-password') { } 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; this.main.loginToAutoFill = cipher;
const tab = await BrowserApi.getTabFromCurrentWindow();
if (tab == null) { if (tab == null) {
return; return;
} }