2018-01-09 20:26:20 +01:00
|
|
|
import { CipherType } from 'jslib/enums';
|
|
|
|
|
2018-01-24 19:27:58 +01:00
|
|
|
import { CipherView } from 'jslib/models/view/cipherView';
|
2018-03-02 18:04:21 +01:00
|
|
|
import { LoginUriView } from 'jslib/models/view/loginUriView';
|
2018-01-24 19:27:58 +01:00
|
|
|
import { LoginView } from 'jslib/models/view/loginView';
|
|
|
|
|
2020-08-11 22:25:07 +02:00
|
|
|
import { AuthResult } from 'jslib/models/domain/authResult';
|
|
|
|
import { AuthService } from 'jslib/abstractions/auth.service';
|
|
|
|
import { AutofillService } from '../services/abstractions/autofill.service';
|
|
|
|
import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service';
|
|
|
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
2018-01-13 04:31:03 +01:00
|
|
|
import { ConstantsService } from 'jslib/services/constants.service';
|
2020-08-11 22:25:07 +02:00
|
|
|
import { EnvironmentService } from 'jslib/abstractions/environment.service';
|
2018-04-11 20:52:49 +02:00
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
2020-08-11 22:25:07 +02:00
|
|
|
import { NotificationsService } from 'jslib/abstractions/notifications.service';
|
2020-08-14 19:48:50 +02:00
|
|
|
import { PopupUtilsService } from '../popup/services/popup-utils.service';
|
2020-08-11 22:25:07 +02:00
|
|
|
import { StateService } from 'jslib/abstractions/state.service';
|
2019-03-06 22:50:04 +01:00
|
|
|
import { StorageService } from 'jslib/abstractions/storage.service';
|
2020-08-11 22:25:07 +02:00
|
|
|
import { SyncService } from 'jslib/abstractions/sync.service';
|
2019-02-27 15:28:16 +01:00
|
|
|
import { SystemService } from 'jslib/abstractions/system.service';
|
2020-04-06 17:40:16 +02:00
|
|
|
import { VaultTimeoutService } from 'jslib/abstractions/vaultTimeout.service';
|
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';
|
|
|
|
|
2020-08-11 22:25:07 +02:00
|
|
|
import { Analytics } from 'jslib/misc';
|
2018-04-23 19:04:11 +02:00
|
|
|
import { Utils } from 'jslib/misc/utils';
|
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
export default class RuntimeBackground {
|
|
|
|
private runtime: any;
|
2018-04-06 17:48:45 +02:00
|
|
|
private autofillTimeout: any;
|
2017-12-07 21:36:24 +01:00
|
|
|
private pageDetailsToAutoFill: any[] = [];
|
2018-01-12 18:22:55 +01:00
|
|
|
private isSafari: boolean;
|
2018-01-18 05:21:17 +01:00
|
|
|
private onInstalledReason: string = null;
|
2017-12-07 21:06:37 +01:00
|
|
|
|
2017-12-07 21:36:24 +01:00
|
|
|
constructor(private main: MainBackground, private autofillService: AutofillService,
|
2018-04-10 20:20:03 +02:00
|
|
|
private cipherService: CipherService, private platformUtilsService: BrowserPlatformUtilsService,
|
2018-08-20 23:40:39 +02:00
|
|
|
private storageService: StorageService, private i18nService: I18nService,
|
2019-02-25 22:19:19 +01:00
|
|
|
private analytics: Analytics, private notificationsService: NotificationsService,
|
2020-08-11 22:25:07 +02:00
|
|
|
private systemService: SystemService, private vaultTimeoutService: VaultTimeoutService,
|
|
|
|
private syncService: SyncService, private authService: AuthService, private stateService: StateService,
|
2020-08-14 19:48:50 +02:00
|
|
|
private environmentService: EnvironmentService, private popupUtilsService : PopupUtilsService) {
|
2018-01-12 18:22:55 +01:00
|
|
|
this.isSafari = this.platformUtilsService.isSafari();
|
2019-08-13 21:47:03 +02:00
|
|
|
this.runtime = this.isSafari ? {} : chrome.runtime;
|
2018-01-18 05:21:17 +01:00
|
|
|
|
|
|
|
// onInstalled listener must be wired up before anything else, so we do it in the ctor
|
|
|
|
if (!this.isSafari) {
|
|
|
|
this.runtime.onInstalled.addListener((details: any) => {
|
|
|
|
this.onInstalledReason = details.reason;
|
|
|
|
});
|
|
|
|
}
|
2020-08-11 22:25:07 +02:00
|
|
|
|
|
|
|
chrome.runtime.onMessage.addListener(
|
|
|
|
(request: any) => {
|
|
|
|
|
|
|
|
var vaultUrl = environmentService.webVaultUrl;
|
|
|
|
if(!vaultUrl) {
|
|
|
|
vaultUrl = 'https://vault.bitwarden.com';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!request.referrer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!vaultUrl.includes(request.referrer)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.type == "AUTH_RESULT") {
|
|
|
|
try {
|
2020-08-14 19:48:50 +02:00
|
|
|
popupUtilsService.ProcessSso(request.code, request.state);
|
2020-08-11 22:25:07 +02:00
|
|
|
}
|
2020-08-14 19:48:50 +02:00
|
|
|
catch (error) { }
|
2020-08-11 22:25:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
async init() {
|
2017-12-07 21:36:24 +01:00
|
|
|
if (!this.runtime) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-16 06:03:17 +01:00
|
|
|
await this.checkOnInstalled();
|
2019-08-15 22:36:49 +02:00
|
|
|
BrowserApi.messageListener('runtime.background', 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-14 00:16:19 +01:00
|
|
|
async processMessage(msg: any, sender: any, sendResponse: any) {
|
2018-01-12 21:20:19 +01:00
|
|
|
switch (msg.command) {
|
|
|
|
case 'loggedIn':
|
|
|
|
case 'unlocked':
|
|
|
|
await this.main.setIcon();
|
2019-02-25 22:19:19 +01:00
|
|
|
await this.main.refreshBadgeAndMenu(false);
|
2018-08-23 15:26:07 +02:00
|
|
|
this.notificationsService.updateConnection(msg.command === 'unlocked');
|
2019-02-27 15:28:16 +01:00
|
|
|
this.systemService.cancelProcessReload();
|
2018-01-12 21:20:19 +01:00
|
|
|
break;
|
|
|
|
case 'logout':
|
|
|
|
await this.main.logout(msg.expired);
|
|
|
|
break;
|
|
|
|
case 'syncCompleted':
|
|
|
|
if (msg.successfully) {
|
|
|
|
setTimeout(async () => await this.main.refreshBadgeAndMenu(), 2000);
|
|
|
|
}
|
|
|
|
break;
|
2018-01-18 22:17:58 +01:00
|
|
|
case 'openPopup':
|
|
|
|
await this.main.openPopup();
|
|
|
|
break;
|
2018-04-10 20:20:03 +02:00
|
|
|
case 'showDialogResolve':
|
|
|
|
this.platformUtilsService.resolveDialogPromise(msg.dialogId, msg.confirmed);
|
|
|
|
break;
|
2018-01-13 16:13:31 +01:00
|
|
|
case 'bgGetDataForTab':
|
|
|
|
await this.getDataForTab(sender.tab, msg.responseCommand);
|
2018-01-13 04:31:03 +01:00
|
|
|
break;
|
2018-01-12 21:20:19 +01:00
|
|
|
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':
|
2019-03-06 22:50:04 +01:00
|
|
|
await this.main.collectPageDetailsForContentScript(sender.tab, msg.sender, sender.frameId);
|
2018-01-12 21:20:19 +01:00
|
|
|
break;
|
|
|
|
case 'bgAddLogin':
|
|
|
|
await this.addLogin(msg.login, sender.tab);
|
|
|
|
break;
|
2018-08-01 05:24:11 +02:00
|
|
|
case 'bgChangedPassword':
|
|
|
|
await this.changedPassword(msg.data, sender.tab);
|
|
|
|
break;
|
2018-01-12 21:20:19 +01:00
|
|
|
case 'bgAddClose':
|
2018-08-01 05:24:11 +02:00
|
|
|
case 'bgChangeClose':
|
|
|
|
this.removeTabFromNotificationQueue(sender.tab);
|
2018-01-12 21:20:19 +01:00
|
|
|
break;
|
|
|
|
case 'bgAddSave':
|
|
|
|
await this.saveAddLogin(sender.tab);
|
|
|
|
break;
|
2018-08-01 05:24:11 +02:00
|
|
|
case 'bgChangeSave':
|
|
|
|
await this.saveChangePassword(sender.tab);
|
|
|
|
break;
|
2018-01-12 21:20:19 +01:00
|
|
|
case 'bgNeverSave':
|
|
|
|
await this.saveNever(sender.tab);
|
|
|
|
break;
|
|
|
|
case 'bgUpdateContextMenu':
|
2019-01-03 16:22:55 +01:00
|
|
|
case 'editedCipher':
|
|
|
|
case 'addedCipher':
|
2019-02-22 21:37:05 +01:00
|
|
|
case 'deletedCipher':
|
2018-01-12 21:20:19 +01:00
|
|
|
await this.main.refreshBadgeAndMenu();
|
|
|
|
break;
|
2018-10-04 04:46:11 +02:00
|
|
|
case 'bgReseedStorage':
|
2019-02-13 17:34:42 +01:00
|
|
|
await this.main.reseedStorage();
|
2018-10-04 04:46:11 +02:00
|
|
|
break;
|
2018-01-12 21:20:19 +01:00
|
|
|
case 'collectPageDetailsResponse':
|
2020-04-06 17:40:16 +02:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
2019-03-06 22:50:04 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-01-12 21:20:19 +01:00
|
|
|
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':
|
2018-04-23 16:02:30 +02:00
|
|
|
const totpCode = await this.autofillService.doAutoFillForLastUsedLogin([{
|
2018-01-12 21:20:19 +01:00
|
|
|
frameId: sender.frameId,
|
|
|
|
tab: msg.tab,
|
|
|
|
details: msg.details,
|
|
|
|
}], msg.sender === 'autofill_cmd');
|
2018-08-31 03:47:49 +02:00
|
|
|
if (totpCode != null) {
|
2018-08-13 15:44:59 +02:00
|
|
|
this.platformUtilsService.copyToClipboard(totpCode, { window: window });
|
2018-04-23 16:02:30 +02:00
|
|
|
}
|
2018-01-12 21:20:19 +01:00
|
|
|
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() {
|
2018-04-23 16:02:30 +02:00
|
|
|
const totpCode = await this.autofillService.doAutoFill({
|
2017-12-07 21:36:24 +01:00
|
|
|
cipher: this.main.loginToAutoFill,
|
|
|
|
pageDetails: this.pageDetailsToAutoFill,
|
|
|
|
});
|
|
|
|
|
2018-08-31 03:47:49 +02:00
|
|
|
if (totpCode != null) {
|
2018-08-13 15:44:59 +02:00
|
|
|
this.platformUtilsService.copyToClipboard(totpCode, { window: window });
|
2018-04-23 16:02:30 +02:00
|
|
|
}
|
|
|
|
|
2017-12-07 21:36:24 +01:00
|
|
|
// reset
|
|
|
|
this.main.loginToAutoFill = null;
|
|
|
|
this.pageDetailsToAutoFill = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
private async saveAddLogin(tab: any) {
|
2020-04-06 17:40:16 +02:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
2019-03-06 22:50:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-01 05:24:11 +02:00
|
|
|
for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) {
|
|
|
|
const queueMessage = this.main.notificationQueue[i];
|
|
|
|
if (queueMessage.tabId !== tab.id || queueMessage.type !== 'addLogin') {
|
2017-12-07 21:36:24 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-14 04:52:49 +02:00
|
|
|
const tabDomain = Utils.getDomain(tab.url);
|
2018-08-01 05:24:11 +02:00
|
|
|
if (tabDomain != null && tabDomain !== queueMessage.domain) {
|
2017-12-07 21:36:24 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-01 05:24:11 +02:00
|
|
|
this.main.notificationQueue.splice(i, 1);
|
2018-08-01 05:48:11 +02:00
|
|
|
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2018-01-24 19:27:58 +01:00
|
|
|
const loginModel = new LoginView();
|
2018-03-02 18:04:21 +01:00
|
|
|
const loginUri = new LoginUriView();
|
2018-08-01 05:24:11 +02:00
|
|
|
loginUri.uri = queueMessage.uri;
|
2018-03-02 18:04:21 +01:00
|
|
|
loginModel.uris = [loginUri];
|
2018-08-01 05:24:11 +02:00
|
|
|
loginModel.username = queueMessage.username;
|
|
|
|
loginModel.password = queueMessage.password;
|
2018-01-24 19:27:58 +01:00
|
|
|
const model = new CipherView();
|
2018-11-12 12:58:53 +01:00
|
|
|
model.name = Utils.getHostname(queueMessage.uri) || queueMessage.domain;
|
2018-11-12 14:15:05 +01:00
|
|
|
model.name = model.name.replace(/^www\./, '');
|
2018-01-24 19:27:58 +01:00
|
|
|
model.type = CipherType.Login;
|
|
|
|
model.login = loginModel;
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2018-01-24 19:27:58 +01:00
|
|
|
const cipher = await this.cipherService.encrypt(model);
|
2017-12-07 21:36:24 +01:00
|
|
|
await this.cipherService.saveWithServer(cipher);
|
2018-01-19 22:19:24 +01:00
|
|
|
this.analytics.ga('send', {
|
2017-12-07 21:36:24 +01:00
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Added Login from Notification Bar',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 05:24:11 +02:00
|
|
|
private async saveChangePassword(tab: any) {
|
2020-04-06 17:40:16 +02:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
2019-03-06 22:50:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-01 05:24:11 +02:00
|
|
|
for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) {
|
|
|
|
const queueMessage = this.main.notificationQueue[i];
|
|
|
|
if (queueMessage.tabId !== tab.id || queueMessage.type !== 'changePassword') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-14 04:52:49 +02:00
|
|
|
const tabDomain = Utils.getDomain(tab.url);
|
2018-08-01 05:24:11 +02:00
|
|
|
if (tabDomain != null && tabDomain !== queueMessage.domain) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.main.notificationQueue.splice(i, 1);
|
2018-08-01 05:48:11 +02:00
|
|
|
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
|
2018-08-01 05:24:11 +02:00
|
|
|
|
|
|
|
const cipher = await this.cipherService.get(queueMessage.cipherId);
|
|
|
|
if (cipher != null && cipher.type === CipherType.Login) {
|
|
|
|
const model = await cipher.decrypt();
|
|
|
|
model.login.password = queueMessage.newPassword;
|
|
|
|
const newCipher = await this.cipherService.encrypt(model);
|
|
|
|
await this.cipherService.saveWithServer(newCipher);
|
|
|
|
this.analytics.ga('send', {
|
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'Changed Password from Notification Bar',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:36:24 +01:00
|
|
|
private async saveNever(tab: any) {
|
2018-08-01 05:24:11 +02:00
|
|
|
for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) {
|
|
|
|
const queueMessage = this.main.notificationQueue[i];
|
|
|
|
if (queueMessage.tabId !== tab.id || queueMessage.type !== 'addLogin') {
|
2017-12-07 21:36:24 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-14 04:52:49 +02:00
|
|
|
const tabDomain = Utils.getDomain(tab.url);
|
2018-08-01 05:24:11 +02:00
|
|
|
if (tabDomain != null && tabDomain !== queueMessage.domain) {
|
2017-12-07 21:36:24 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-01 05:24:11 +02:00
|
|
|
this.main.notificationQueue.splice(i, 1);
|
2018-08-01 05:48:11 +02:00
|
|
|
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
|
|
|
|
|
2018-04-23 19:04:11 +02:00
|
|
|
const hostname = Utils.getHostname(tab.url);
|
2017-12-07 21:36:24 +01:00
|
|
|
await this.cipherService.saveNeverDomain(hostname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async addLogin(loginInfo: any, tab: any) {
|
2020-04-06 17:40:16 +02:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
2019-03-06 22:50:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-14 04:52:49 +02:00
|
|
|
const loginDomain = Utils.getDomain(loginInfo.url);
|
2017-12-07 21:36:24 +01:00
|
|
|
if (loginDomain == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-18 13:34:12 +01:00
|
|
|
let normalizedUsername = loginInfo.username;
|
|
|
|
if (normalizedUsername != null) {
|
|
|
|
normalizedUsername = normalizedUsername.toLowerCase();
|
2019-12-26 13:51:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 18:04:21 +01:00
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(loginInfo.url);
|
2019-12-26 13:51:16 +01:00
|
|
|
const usernameMatches = ciphers.filter((c) =>
|
2020-01-18 13:34:12 +01:00
|
|
|
c.login.username != null && c.login.username.toLowerCase() === normalizedUsername);
|
2018-08-06 19:37:29 +02:00
|
|
|
if (usernameMatches.length === 0) {
|
2018-08-07 15:22:06 +02:00
|
|
|
const disabledAddLogin = await this.storageService.get<boolean>(
|
|
|
|
ConstantsService.disableAddLoginNotificationKey);
|
|
|
|
if (disabledAddLogin) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-01 05:24:11 +02:00
|
|
|
// remove any old messages for this tab
|
|
|
|
this.removeTabFromNotificationQueue(tab);
|
|
|
|
this.main.notificationQueue.push({
|
|
|
|
type: 'addLogin',
|
2017-12-07 21:36:24 +01:00
|
|
|
username: loginInfo.username,
|
|
|
|
password: loginInfo.password,
|
|
|
|
domain: loginDomain,
|
|
|
|
uri: loginInfo.url,
|
|
|
|
tabId: tab.id,
|
|
|
|
expires: new Date((new Date()).getTime() + 30 * 60000), // 30 minutes
|
|
|
|
});
|
2018-08-01 05:24:11 +02:00
|
|
|
await this.main.checkNotificationQueue(tab);
|
2018-08-06 19:37:29 +02:00
|
|
|
} else if (usernameMatches.length === 1 && usernameMatches[0].login.password !== loginInfo.password) {
|
2018-08-07 15:22:06 +02:00
|
|
|
const disabledChangePassword = await this.storageService.get<boolean>(
|
|
|
|
ConstantsService.disableChangedPasswordNotificationKey);
|
|
|
|
if (disabledChangePassword) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-06 19:37:29 +02:00
|
|
|
this.addChangedPasswordToQueue(usernameMatches[0].id, loginDomain, loginInfo.password, tab);
|
2018-08-01 05:24:11 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2018-08-01 05:24:11 +02:00
|
|
|
private async changedPassword(changeData: any, tab: any) {
|
2020-04-06 17:40:16 +02:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
2019-03-06 22:50:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-14 04:52:49 +02:00
|
|
|
const loginDomain = Utils.getDomain(changeData.url);
|
2018-08-01 05:24:11 +02:00
|
|
|
if (loginDomain == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-07 14:01:50 +02:00
|
|
|
let id: string = null;
|
2018-08-01 05:24:11 +02:00
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(changeData.url);
|
2018-09-07 14:01:50 +02:00
|
|
|
if (changeData.currentPassword != null) {
|
|
|
|
const passwordMatches = ciphers.filter((c) => c.login.password === changeData.currentPassword);
|
|
|
|
if (passwordMatches.length === 1) {
|
|
|
|
id = passwordMatches[0].id;
|
|
|
|
}
|
|
|
|
} else if (ciphers.length === 1) {
|
|
|
|
id = ciphers[0].id;
|
|
|
|
}
|
|
|
|
if (id != null) {
|
|
|
|
this.addChangedPasswordToQueue(id, loginDomain, changeData.newPassword, tab);
|
2017-12-07 21:36:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 19:37:29 +02:00
|
|
|
private async addChangedPasswordToQueue(cipherId: string, loginDomain: string, newPassword: string, tab: any) {
|
|
|
|
// remove any old messages for this tab
|
|
|
|
this.removeTabFromNotificationQueue(tab);
|
|
|
|
this.main.notificationQueue.push({
|
|
|
|
type: 'changePassword',
|
|
|
|
cipherId: cipherId,
|
|
|
|
newPassword: newPassword,
|
|
|
|
domain: loginDomain,
|
|
|
|
tabId: tab.id,
|
|
|
|
expires: new Date((new Date()).getTime() + 30 * 60000), // 30 minutes
|
|
|
|
});
|
|
|
|
await this.main.checkNotificationQueue(tab);
|
|
|
|
}
|
|
|
|
|
2018-08-01 05:24:11 +02:00
|
|
|
private removeTabFromNotificationQueue(tab: any) {
|
|
|
|
for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) {
|
|
|
|
if (this.main.notificationQueue[i].tabId === tab.id) {
|
|
|
|
this.main.notificationQueue.splice(i, 1);
|
2017-12-07 21:36:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 06:03:17 +01:00
|
|
|
private async checkOnInstalled() {
|
|
|
|
if (this.isSafari) {
|
2018-02-16 21:50:23 +01:00
|
|
|
const installedVersion = await this.storageService.get<string>(ConstantsService.installedVersionKey);
|
2018-01-16 06:03:17 +01:00
|
|
|
if (installedVersion == null) {
|
2018-01-18 05:21:17 +01:00
|
|
|
this.onInstalledReason = 'install';
|
2018-01-16 06:03:17 +01:00
|
|
|
} else if (BrowserApi.getApplicationVersion() !== installedVersion) {
|
2018-01-18 05:21:17 +01:00
|
|
|
this.onInstalledReason = 'update';
|
2018-01-16 06:03:17 +01:00
|
|
|
}
|
|
|
|
|
2018-01-18 05:21:17 +01:00
|
|
|
if (this.onInstalledReason != null) {
|
2018-02-16 21:50:23 +01:00
|
|
|
await this.storageService.save(ConstantsService.installedVersionKey,
|
|
|
|
BrowserApi.getApplicationVersion());
|
2018-01-16 06:03:17 +01:00
|
|
|
}
|
2018-01-18 05:21:17 +01:00
|
|
|
}
|
2018-01-16 06:03:17 +01:00
|
|
|
|
2018-01-18 05:21:17 +01:00
|
|
|
setTimeout(async () => {
|
|
|
|
if (this.onInstalledReason != null) {
|
|
|
|
if (this.onInstalledReason === 'install') {
|
|
|
|
BrowserApi.createNewTab('https://bitwarden.com/browser-start/');
|
2018-01-18 04:42:31 +01:00
|
|
|
await this.setDefaultSettings();
|
2018-01-16 06:03:17 +01:00
|
|
|
}
|
2018-01-18 05:21:17 +01:00
|
|
|
|
2018-01-19 22:19:24 +01:00
|
|
|
this.analytics.ga('send', {
|
2018-01-19 17:42:35 +01:00
|
|
|
hitType: 'event',
|
|
|
|
eventAction: 'onInstalled ' + this.onInstalledReason,
|
|
|
|
});
|
2018-01-18 05:21:17 +01:00
|
|
|
this.onInstalledReason = null;
|
|
|
|
}
|
2018-01-19 17:42:35 +01:00
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
|
2018-01-18 04:42:31 +01:00
|
|
|
private async setDefaultSettings() {
|
2020-04-06 17:40:16 +02:00
|
|
|
// Default timeout option to "on restart".
|
|
|
|
const currentVaultTimeout = await this.storageService.get<number>(ConstantsService.vaultTimeoutKey);
|
|
|
|
if (currentVaultTimeout == null) {
|
|
|
|
await this.storageService.save(ConstantsService.vaultTimeoutKey, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default action to "lock".
|
|
|
|
const currentVaultTimeoutAction = await this.storageService.get<string>(ConstantsService.vaultTimeoutActionKey);
|
|
|
|
if (currentVaultTimeoutAction == null) {
|
|
|
|
await this.storageService.save(ConstantsService.vaultTimeoutActionKey, 'lock');
|
2018-01-18 04:42:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 16:13:31 +01:00
|
|
|
private async getDataForTab(tab: any, responseCommand: string) {
|
2018-01-13 21:09:05 +01:00
|
|
|
const responseData: any = {};
|
2018-01-13 16:13:31 +01:00
|
|
|
if (responseCommand === 'notificationBarDataResponse') {
|
2018-01-13 21:09:05 +01:00
|
|
|
responseData.neverDomains = await this.storageService.get<any>(ConstantsService.neverDomainsKey);
|
2018-08-01 05:48:11 +02:00
|
|
|
responseData.disabledAddLoginNotification = await this.storageService.get<boolean>(
|
2018-01-13 16:13:31 +01:00
|
|
|
ConstantsService.disableAddLoginNotificationKey);
|
2018-08-01 05:48:11 +02:00
|
|
|
responseData.disabledChangedPasswordNotification = await this.storageService.get<boolean>(
|
|
|
|
ConstantsService.disableChangedPasswordNotificationKey);
|
2018-01-13 16:13:31 +01:00
|
|
|
} else if (responseCommand === 'autofillerAutofillOnPageLoadEnabledResponse') {
|
2018-01-13 21:09:05 +01:00
|
|
|
responseData.autofillEnabled = await this.storageService.get<boolean>(
|
2018-01-13 16:13:31 +01:00
|
|
|
ConstantsService.enableAutoFillOnPageLoadKey);
|
2018-01-13 21:09:05 +01:00
|
|
|
} else if (responseCommand === 'notificationBarFrameDataResponse') {
|
|
|
|
responseData.i18n = {
|
2018-04-11 20:52:49 +02:00
|
|
|
appName: this.i18nService.t('appName'),
|
|
|
|
close: this.i18nService.t('close'),
|
|
|
|
yes: this.i18nService.t('yes'),
|
|
|
|
never: this.i18nService.t('never'),
|
|
|
|
notificationAddSave: this.i18nService.t('notificationAddSave'),
|
|
|
|
notificationNeverSave: this.i18nService.t('notificationNeverSave'),
|
|
|
|
notificationAddDesc: this.i18nService.t('notificationAddDesc'),
|
2018-08-01 05:24:11 +02:00
|
|
|
notificationChangeSave: this.i18nService.t('notificationChangeSave'),
|
|
|
|
notificationChangeDesc: this.i18nService.t('notificationChangeDesc'),
|
2018-01-13 21:09:05 +01:00
|
|
|
};
|
2018-01-13 16:13:31 +01:00
|
|
|
}
|
|
|
|
|
2018-01-13 21:09:05 +01:00
|
|
|
await BrowserApi.tabSendMessageData(tab, responseCommand, responseData);
|
2018-01-13 16:13:31 +01:00
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|