2018-01-09 20:26:20 +01:00
|
|
|
import { CipherType } from 'jslib/enums';
|
|
|
|
|
|
|
|
import { UtilsService } from 'jslib/services/utils.service';
|
|
|
|
|
|
|
|
import {
|
2018-01-10 05:05:46 +01:00
|
|
|
CipherService,
|
2018-01-09 20:26:20 +01:00
|
|
|
PlatformUtilsService,
|
|
|
|
} from 'jslib/abstractions';
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2018-01-12 17:09:30 +01:00
|
|
|
import { BrowserApi } from '../browser/browserApi';
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
import MainBackground from './main.background';
|
|
|
|
|
2018-01-11 20:45:27 +01:00
|
|
|
import { AutofillService } from '../services/abstractions/autofill.service';
|
2018-01-05 22:30:15 +01:00
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
export default class RuntimeBackground {
|
|
|
|
private runtime: any;
|
2017-12-07 21:36:24 +01:00
|
|
|
private autofillTimeout: number;
|
|
|
|
private pageDetailsToAutoFill: any[] = [];
|
2018-01-12 18:22:55 +01:00
|
|
|
private isSafari: boolean;
|
2017-12-07 21:06:37 +01:00
|
|
|
|
2017-12-07 21:36:24 +01:00
|
|
|
constructor(private main: MainBackground, private autofillService: AutofillService,
|
2018-01-09 20:26:20 +01:00
|
|
|
private cipherService: CipherService, private platformUtilsService: PlatformUtilsService) {
|
2018-01-12 18:22:55 +01:00
|
|
|
this.isSafari = this.platformUtilsService.isSafari();
|
|
|
|
this.runtime = this.isSafari ? safari.application : chrome.runtime;
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2017-12-07 21:36:24 +01:00
|
|
|
if (!this.runtime) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-12 18:22:55 +01:00
|
|
|
if (this.isSafari) {
|
|
|
|
// Reload the popup when it's opened
|
|
|
|
this.runtime.addEventListener('popover', (event: any) => {
|
|
|
|
const win: Window = event.target.contentWindow;
|
2018-01-12 18:41:51 +01:00
|
|
|
const href = win.location.origin + win.location.pathname;
|
2018-01-12 19:27:40 +01:00
|
|
|
if (win.location.toString() === href) {
|
2018-01-12 18:41:51 +01:00
|
|
|
win.location.reload();
|
|
|
|
} else {
|
|
|
|
win.location.href = href;
|
2018-01-12 18:22:55 +01:00
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
|
2018-01-12 21:20:19 +01:00
|
|
|
this.runtime.addEventListener('message', async (msgEvent: any) => {
|
|
|
|
await this.processMessage(msgEvent.message, {
|
|
|
|
tab: {
|
|
|
|
id: null, // TODO
|
|
|
|
},
|
|
|
|
frameId: null,
|
|
|
|
}, () => { /* No responses in Safari */ });
|
|
|
|
}, false);
|
|
|
|
|
2018-01-12 18:22:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:46:56 +01:00
|
|
|
if (this.runtime.onInstalled) {
|
|
|
|
this.runtime.onInstalled.addListener((details: any) => {
|
|
|
|
(window as any).ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'onInstalled ' + details.reason,
|
|
|
|
});
|
2017-12-07 21:06:37 +01:00
|
|
|
|
2017-12-07 21:46:56 +01:00
|
|
|
if (details.reason === 'install') {
|
|
|
|
chrome.tabs.create({ url: 'https://bitwarden.com/browser-start/' });
|
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
});
|
2017-12-07 21:46:56 +01:00
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
|
2017-12-07 21:46:56 +01:00
|
|
|
this.runtime.onMessage.addListener(async (msg: any, sender: any, sendResponse: any) => {
|
2018-01-12 21:20:19 +01:00
|
|
|
await this.processMessage(msg, sender, sendResponse);
|
2017-12-07 21:06:37 +01:00
|
|
|
});
|
|
|
|
}
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2018-01-12 21:20:19 +01:00
|
|
|
private async processMessage(msg: any, sender: any, sendResponse: any) {
|
|
|
|
switch (msg.command) {
|
|
|
|
case 'loggedIn':
|
|
|
|
case 'unlocked':
|
|
|
|
case 'locked':
|
|
|
|
await this.main.setIcon();
|
|
|
|
await this.main.refreshBadgeAndMenu();
|
|
|
|
break;
|
|
|
|
case 'logout':
|
|
|
|
await this.main.logout(msg.expired);
|
|
|
|
break;
|
|
|
|
case 'syncCompleted':
|
|
|
|
if (msg.successfully) {
|
|
|
|
setTimeout(async () => await this.main.refreshBadgeAndMenu(), 2000);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'bgOpenNotificationBar':
|
|
|
|
await BrowserApi.tabSendMessageData(sender.tab, 'openNotificationBar', msg.data);
|
|
|
|
break;
|
|
|
|
case 'bgCloseNotificationBar':
|
|
|
|
await BrowserApi.tabSendMessageData(sender.tab, 'closeNotificationBar');
|
|
|
|
break;
|
|
|
|
case 'bgAdjustNotificationBar':
|
|
|
|
await BrowserApi.tabSendMessageData(sender.tab, 'adjustNotificationBar', msg.data);
|
|
|
|
break;
|
|
|
|
case 'bgCollectPageDetails':
|
|
|
|
this.main.collectPageDetailsForContentScript(sender.tab, msg.sender, sender.frameId);
|
|
|
|
break;
|
|
|
|
case 'bgAddLogin':
|
|
|
|
await this.addLogin(msg.login, sender.tab);
|
|
|
|
break;
|
|
|
|
case 'bgAddClose':
|
|
|
|
this.removeAddLogin(sender.tab);
|
|
|
|
break;
|
|
|
|
case 'bgAddSave':
|
|
|
|
await this.saveAddLogin(sender.tab);
|
|
|
|
break;
|
|
|
|
case 'bgNeverSave':
|
|
|
|
await this.saveNever(sender.tab);
|
|
|
|
break;
|
|
|
|
case 'bgUpdateContextMenu':
|
|
|
|
await this.main.refreshBadgeAndMenu();
|
|
|
|
break;
|
|
|
|
case 'collectPageDetailsResponse':
|
|
|
|
switch (msg.sender) {
|
|
|
|
case 'notificationBar':
|
|
|
|
const forms = this.autofillService.getFormsWithPasswordFields(msg.details);
|
|
|
|
await BrowserApi.tabSendMessageData(msg.tab, 'notificationBarPageDetails', {
|
|
|
|
details: msg.details,
|
|
|
|
forms: forms,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'autofiller':
|
|
|
|
case 'autofill_cmd':
|
|
|
|
await this.autofillService.doAutoFillForLastUsedLogin([{
|
|
|
|
frameId: sender.frameId,
|
|
|
|
tab: msg.tab,
|
|
|
|
details: msg.details,
|
|
|
|
}], msg.sender === 'autofill_cmd');
|
|
|
|
break;
|
|
|
|
case 'contextMenu':
|
|
|
|
clearTimeout(this.autofillTimeout);
|
|
|
|
this.pageDetailsToAutoFill.push({
|
|
|
|
frameId: sender.frameId,
|
|
|
|
tab: msg.tab,
|
|
|
|
details: msg.details,
|
|
|
|
});
|
|
|
|
this.autofillTimeout = setTimeout(async () => await this.autofillPage(), 300);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:36:24 +01:00
|
|
|
private async autofillPage() {
|
|
|
|
await this.autofillService.doAutoFill({
|
|
|
|
cipher: this.main.loginToAutoFill,
|
|
|
|
pageDetails: this.pageDetailsToAutoFill,
|
|
|
|
fromBackground: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// reset
|
|
|
|
this.main.loginToAutoFill = null;
|
|
|
|
this.pageDetailsToAutoFill = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
private async saveAddLogin(tab: any) {
|
|
|
|
for (let i = this.main.loginsToAdd.length - 1; i >= 0; i--) {
|
|
|
|
if (this.main.loginsToAdd[i].tabId !== tab.id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const loginInfo = this.main.loginsToAdd[i];
|
2018-01-05 22:30:15 +01:00
|
|
|
const tabDomain = this.platformUtilsService.getDomain(tab.url);
|
2017-12-07 21:36:24 +01:00
|
|
|
if (tabDomain != null && tabDomain !== loginInfo.domain) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.main.loginsToAdd.splice(i, 1);
|
|
|
|
|
|
|
|
const cipher = await this.cipherService.encrypt({
|
|
|
|
id: null,
|
|
|
|
folderId: null,
|
|
|
|
favorite: false,
|
|
|
|
name: loginInfo.name,
|
|
|
|
notes: null,
|
2018-01-09 20:26:20 +01:00
|
|
|
type: CipherType.Login,
|
2017-12-07 21:36:24 +01:00
|
|
|
login: {
|
|
|
|
uri: loginInfo.uri,
|
|
|
|
username: loginInfo.username,
|
|
|
|
password: loginInfo.password,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.cipherService.saveWithServer(cipher);
|
|
|
|
(window as any).ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Added Login from Notification Bar',
|
|
|
|
});
|
|
|
|
|
2018-01-12 17:32:42 +01:00
|
|
|
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
|
2017-12-07 21:36:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async saveNever(tab: any) {
|
|
|
|
for (let i = this.main.loginsToAdd.length - 1; i >= 0; i--) {
|
|
|
|
if (this.main.loginsToAdd[i].tabId !== tab.id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const loginInfo = this.main.loginsToAdd[i];
|
2018-01-05 22:30:15 +01:00
|
|
|
const tabDomain = this.platformUtilsService.getDomain(tab.url);
|
2017-12-07 21:36:24 +01:00
|
|
|
if (tabDomain != null && tabDomain !== loginInfo.domain) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.main.loginsToAdd.splice(i, 1);
|
2018-01-09 20:26:20 +01:00
|
|
|
const hostname = UtilsService.getHostname(tab.url);
|
2017-12-07 21:36:24 +01:00
|
|
|
await this.cipherService.saveNeverDomain(hostname);
|
2018-01-12 17:32:42 +01:00
|
|
|
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
|
2017-12-07 21:36:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async addLogin(loginInfo: any, tab: any) {
|
2018-01-05 22:30:15 +01:00
|
|
|
const loginDomain = this.platformUtilsService.getDomain(loginInfo.url);
|
2017-12-07 21:36:24 +01:00
|
|
|
if (loginDomain == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForDomain(loginDomain);
|
|
|
|
|
|
|
|
let match = false;
|
|
|
|
for (let i = 0; i < ciphers.length; i++) {
|
|
|
|
if (ciphers[i].login.username === loginInfo.username) {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
// remove any old logins for this tab
|
|
|
|
this.removeAddLogin(tab);
|
|
|
|
|
|
|
|
this.main.loginsToAdd.push({
|
|
|
|
username: loginInfo.username,
|
|
|
|
password: loginInfo.password,
|
|
|
|
name: loginDomain,
|
|
|
|
domain: loginDomain,
|
|
|
|
uri: loginInfo.url,
|
|
|
|
tabId: tab.id,
|
|
|
|
expires: new Date((new Date()).getTime() + 30 * 60000), // 30 minutes
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.main.checkLoginsToAdd(tab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private removeAddLogin(tab: any) {
|
|
|
|
for (let i = this.main.loginsToAdd.length - 1; i >= 0; i--) {
|
|
|
|
if (this.main.loginsToAdd[i].tabId === tab.id) {
|
|
|
|
this.main.loginsToAdd.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:32:42 +01:00
|
|
|
private async currenttabSendMessageData(command: string, data: any = null) {
|
2017-12-07 21:36:24 +01:00
|
|
|
const tab = await BrowserApi.getTabFromCurrentWindow();
|
|
|
|
if (tab == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:32:42 +01:00
|
|
|
await BrowserApi.tabSendMessageData(tab, command, data);
|
2017-12-07 21:36:24 +01:00
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|