2021-09-17 15:44:27 +02:00
|
|
|
import { CipherType } from 'jslib-common/enums/cipherType';
|
2021-06-07 19:25:37 +02:00
|
|
|
|
|
|
|
import { CipherView } from 'jslib-common/models/view/cipherView';
|
|
|
|
import { LoginUriView } from 'jslib-common/models/view/loginUriView';
|
|
|
|
import { LoginView } from 'jslib-common/models/view/loginView';
|
|
|
|
|
|
|
|
import { CipherService } from 'jslib-common/abstractions/cipher.service';
|
|
|
|
import { EnvironmentService } from 'jslib-common/abstractions/environment.service';
|
2021-09-03 03:54:52 +02:00
|
|
|
import { FolderService } from 'jslib-common/abstractions/folder.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
|
|
|
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
|
|
|
|
import { NotificationsService } from 'jslib-common/abstractions/notifications.service';
|
|
|
|
import { PolicyService } from 'jslib-common/abstractions/policy.service';
|
|
|
|
import { StorageService } from 'jslib-common/abstractions/storage.service';
|
|
|
|
import { SystemService } from 'jslib-common/abstractions/system.service';
|
|
|
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
|
|
|
import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service';
|
|
|
|
import { ConstantsService } from 'jslib-common/services/constants.service';
|
2021-09-30 16:33:33 +02:00
|
|
|
import { PopupUtilsService } from '../popup/services/popup-utils.service';
|
2021-02-10 16:40:15 +01:00
|
|
|
import { AutofillService } from '../services/abstractions/autofill.service';
|
|
|
|
import BrowserPlatformUtilsService from '../services/browserPlatformUtils.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';
|
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { Utils } from 'jslib-common/misc/utils';
|
2018-04-23 19:04:11 +02:00
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { PolicyType } from 'jslib-common/enums/policyType';
|
2020-12-09 21:11:25 +01:00
|
|
|
|
2021-10-13 22:08:02 +02:00
|
|
|
import AddChangePasswordQueueMessage from './models/addChangePasswordQueueMessage';
|
|
|
|
import AddLoginQueueMessage from './models/addLoginQueueMessage';
|
2021-09-30 16:03:14 +02:00
|
|
|
|
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-18 05:21:17 +01:00
|
|
|
private onInstalledReason: string = null;
|
2017-12-07 21:06:37 +01:00
|
|
|
|
2021-10-08 13:23:05 +02:00
|
|
|
private lockedVaultPendingNotifications: any[] = [];
|
|
|
|
|
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,
|
2021-04-14 23:43:09 +02:00
|
|
|
private notificationsService: NotificationsService,
|
2020-08-11 22:25:07 +02:00
|
|
|
private systemService: SystemService, private vaultTimeoutService: VaultTimeoutService,
|
2020-12-09 21:11:25 +01:00
|
|
|
private environmentService: EnvironmentService, private policyService: PolicyService,
|
2021-09-03 03:54:52 +02:00
|
|
|
private userService: UserService, private messagingService: MessagingService,
|
2021-09-30 16:33:33 +02:00
|
|
|
private folderService: FolderService, private popupUtilsService: PopupUtilsService) {
|
2018-01-18 05:21:17 +01:00
|
|
|
|
|
|
|
// onInstalled listener must be wired up before anything else, so we do it in the ctor
|
2021-02-03 20:36:05 +01:00
|
|
|
chrome.runtime.onInstalled.addListener((details: any) => {
|
|
|
|
this.onInstalledReason = details.reason;
|
|
|
|
});
|
2020-08-11 22:25:07 +02:00
|
|
|
}
|
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
async init() {
|
2021-02-03 20:36:05 +01:00
|
|
|
if (!chrome.runtime) {
|
2017-12-07 21:36:24 +01:00
|
|
|
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();
|
2021-09-30 16:43:10 +02:00
|
|
|
|
2021-10-08 13:23:05 +02:00
|
|
|
if (this.lockedVaultPendingNotifications.length > 0) {
|
|
|
|
const retryItem = this.lockedVaultPendingNotifications.pop();
|
2021-09-30 16:43:10 +02:00
|
|
|
await this.processMessage(retryItem.msg, retryItem.sender, null);
|
2021-10-08 15:27:20 +02:00
|
|
|
|
|
|
|
await BrowserApi.closeLoginTab();
|
2021-10-11 17:46:43 +02:00
|
|
|
|
|
|
|
if (retryItem?.sender?.tab?.id) {
|
|
|
|
await BrowserApi.focusSpecifiedTab(retryItem.sender.tab.id);
|
|
|
|
}
|
2021-09-30 16:43:10 +02:00
|
|
|
}
|
|
|
|
break;
|
2021-10-08 15:22:21 +02:00
|
|
|
case 'addToLockedVaultPendingNotifications':
|
2021-09-30 16:43:10 +02:00
|
|
|
const retryMessage = {
|
|
|
|
msg: msg.retryItem,
|
|
|
|
sender: sender,
|
|
|
|
};
|
2021-10-08 13:23:05 +02:00
|
|
|
this.lockedVaultPendingNotifications.push(retryMessage);
|
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;
|
2021-10-08 15:23:37 +02:00
|
|
|
case 'promptForLogin':
|
2021-10-08 15:26:39 +02:00
|
|
|
await BrowserApi.createNewTab('popup/index.html?uilocation=popout', true, true);
|
2021-09-30 16:33:33 +02:00
|
|
|
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 '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':
|
2018-08-01 05:24:11 +02:00
|
|
|
case 'bgChangeSave':
|
2021-09-30 16:48:06 +02:00
|
|
|
await this.saveOrUpdateCredentials(sender.tab, msg.folder);
|
2018-08-01 05:24:11 +02:00
|
|
|
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':
|
|
|
|
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':
|
2020-08-12 22:05:12 +02:00
|
|
|
const totpCode = await this.autofillService.doAutoFillActiveTab([{
|
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;
|
2020-08-14 22:20:16 +02:00
|
|
|
case 'authResult':
|
2021-07-23 22:32:42 +02:00
|
|
|
const vaultUrl = this.environmentService.getWebVaultUrl();
|
2020-08-14 22:20:16 +02:00
|
|
|
|
2020-08-24 16:17:15 +02:00
|
|
|
if (msg.referrer == null || Utils.getHostname(vaultUrl) !== msg.referrer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-09-18 22:03:08 +02:00
|
|
|
BrowserApi.createNewTab('popup/index.html?uilocation=popout#/sso?code=' +
|
|
|
|
msg.code + '&state=' + msg.state);
|
2020-08-24 16:17:15 +02:00
|
|
|
}
|
|
|
|
catch { }
|
2020-08-14 22:20:16 +02:00
|
|
|
break;
|
2021-03-17 22:14:26 +01:00
|
|
|
case 'webAuthnResult':
|
2021-07-23 22:32:42 +02:00
|
|
|
const vaultUrl2 = this.environmentService.getWebVaultUrl();
|
2021-03-17 22:14:26 +01:00
|
|
|
|
|
|
|
if (msg.referrer == null || Utils.getHostname(vaultUrl2) !== msg.referrer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const params = `webAuthnResponse=${encodeURIComponent(msg.data)};remember=${msg.remember}`;
|
|
|
|
BrowserApi.createNewTab(`popup/index.html?uilocation=popout#/2fa;${params}`, undefined, false);
|
|
|
|
break;
|
|
|
|
case 'reloadPopup':
|
|
|
|
this.messagingService.send('reloadPopup');
|
|
|
|
break;
|
2021-05-12 05:35:18 +02:00
|
|
|
case 'emailVerificationRequired':
|
|
|
|
this.messagingService.send('showDialog', {
|
|
|
|
dialogId: 'emailVerificationRequired',
|
|
|
|
title: this.i18nService.t('emailVerificationRequired'),
|
|
|
|
text: this.i18nService.t('emailVerificationRequiredDesc'),
|
|
|
|
confirmText: this.i18nService.t('ok'),
|
|
|
|
type: 'info',
|
|
|
|
});
|
|
|
|
break;
|
2021-09-01 23:51:43 +02:00
|
|
|
case 'getClickedElementResponse':
|
|
|
|
this.platformUtilsService.copyToClipboard(msg.identifier, { window: window });
|
2018-01-12 21:20:19 +01:00
|
|
|
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,
|
2021-02-10 16:40:15 +01:00
|
|
|
fillNewPassword: true,
|
2017-12-07 21:36:24 +01:00
|
|
|
});
|
|
|
|
|
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 = [];
|
|
|
|
}
|
|
|
|
|
2021-09-30 16:48:06 +02:00
|
|
|
private async saveOrUpdateCredentials(tab: any, folderId?: string) {
|
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];
|
2021-09-30 16:48:06 +02:00
|
|
|
if (queueMessage.tabId !== tab.id ||
|
|
|
|
(queueMessage.type !== 'addLogin' && queueMessage.type !== 'changePassword')) {
|
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
|
|
|
|
2021-09-30 16:48:06 +02:00
|
|
|
if (queueMessage.type === 'changePassword') {
|
2021-10-13 22:08:02 +02:00
|
|
|
const message = (queueMessage as AddChangePasswordQueueMessage);
|
2021-09-30 16:48:06 +02:00
|
|
|
const cipher = await this.getDecryptedCipherById(message.cipherId);
|
|
|
|
if (cipher == null) {
|
|
|
|
return;
|
2021-09-03 03:54:52 +02:00
|
|
|
}
|
2021-09-30 16:48:06 +02:00
|
|
|
await this.updateCipher(cipher, message.newPassword);
|
|
|
|
return;
|
2021-09-03 03:54:52 +02:00
|
|
|
}
|
|
|
|
|
2021-09-30 16:48:06 +02:00
|
|
|
if (!queueMessage.wasVaultLocked) {
|
|
|
|
await this.createNewCipher(queueMessage, folderId);
|
|
|
|
}
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2021-09-30 16:48:06 +02:00
|
|
|
// If the vault was locked, check if a cipher needs updating instead of creating a new one
|
|
|
|
if (queueMessage.type === 'addLogin' && queueMessage.wasVaultLocked === true) {
|
2021-10-13 22:08:02 +02:00
|
|
|
const message = (queueMessage as AddLoginQueueMessage);
|
2021-09-30 16:48:06 +02:00
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(message.uri);
|
|
|
|
const usernameMatches = ciphers.filter(c => c.login.username != null &&
|
|
|
|
c.login.username.toLowerCase() === message.username);
|
2019-03-06 22:50:04 +01:00
|
|
|
|
2021-09-30 16:56:04 +02:00
|
|
|
if (usernameMatches.length >= 1) {
|
2021-09-30 16:48:06 +02:00
|
|
|
await this.updateCipher(usernameMatches[0], message.password);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.createNewCipher(message, folderId);
|
2018-08-01 05:24:11 +02:00
|
|
|
}
|
2021-09-30 16:48:06 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-01 05:24:11 +02:00
|
|
|
|
2021-10-13 22:08:02 +02:00
|
|
|
private async createNewCipher(queueMessage: AddLoginQueueMessage, folderId: string) {
|
2021-09-30 16:48:06 +02:00
|
|
|
const loginModel = new LoginView();
|
|
|
|
const loginUri = new LoginUriView();
|
|
|
|
loginUri.uri = queueMessage.uri;
|
|
|
|
loginModel.uris = [loginUri];
|
|
|
|
loginModel.username = queueMessage.username;
|
|
|
|
loginModel.password = queueMessage.password;
|
|
|
|
const model = new CipherView();
|
|
|
|
model.name = Utils.getHostname(queueMessage.uri) || queueMessage.domain;
|
|
|
|
model.name = model.name.replace(/^www\./, '');
|
|
|
|
model.type = CipherType.Login;
|
|
|
|
model.login = loginModel;
|
|
|
|
|
|
|
|
if (!Utils.isNullOrWhitespace(folderId)) {
|
|
|
|
const folders = await this.folderService.getAllDecrypted();
|
|
|
|
if (folders.some(x => x.id === folderId)) {
|
|
|
|
model.folderId = folderId;
|
2018-08-01 05:24:11 +02:00
|
|
|
}
|
2021-09-30 16:48:06 +02:00
|
|
|
}
|
2018-08-01 05:24:11 +02:00
|
|
|
|
2021-09-30 16:48:06 +02:00
|
|
|
const cipher = await this.cipherService.encrypt(model);
|
|
|
|
await this.cipherService.saveWithServer(cipher);
|
|
|
|
}
|
2018-08-01 05:24:11 +02:00
|
|
|
|
2021-09-30 16:48:06 +02:00
|
|
|
private async getDecryptedCipherById(cipherId: string) {
|
|
|
|
const cipher = await this.cipherService.get(cipherId);
|
|
|
|
if (cipher != null && cipher.type === CipherType.Login) {
|
|
|
|
return await cipher.decrypt();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async updateCipher(cipher: CipherView, newPassword: string) {
|
|
|
|
if (cipher != null && cipher.type === CipherType.Login) {
|
|
|
|
cipher.login.password = newPassword;
|
|
|
|
const newCipher = await this.cipherService.encrypt(cipher);
|
|
|
|
await this.cipherService.saveWithServer(newCipher);
|
2018-08-01 05:24:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
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
|
|
|
}
|
|
|
|
|
2021-09-30 16:03:14 +02:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
|
|
|
this.pushAddLoginToQueue(loginDomain, loginInfo, tab, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-02 18:04:21 +01:00
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(loginInfo.url);
|
2021-02-10 16:40:15 +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;
|
|
|
|
}
|
2020-12-09 21:11:25 +01:00
|
|
|
|
|
|
|
if (!(await this.allowPersonalOwnership())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-30 16:03:14 +02:00
|
|
|
this.pushAddLoginToQueue(loginDomain, loginInfo, tab);
|
|
|
|
|
|
|
|
} else if (usernameMatches.length === 1 && usernameMatches[0].login.password !== loginInfo.password) {
|
|
|
|
const disabledChangePassword = await this.storageService.get<boolean>(
|
|
|
|
ConstantsService.disableChangedPasswordNotificationKey);
|
|
|
|
if (disabledChangePassword) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.pushChangePasswordToQueue(usernameMatches[0].id, loginDomain, loginInfo.password, tab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async pushAddLoginToQueue(loginDomain: string, loginInfo: any, tab: any, isVaultLocked: boolean = false) {
|
2021-09-30 16:43:10 +02:00
|
|
|
// remove any old messages for this tab
|
|
|
|
this.removeTabFromNotificationQueue(tab);
|
2021-10-13 22:08:02 +02:00
|
|
|
const message: AddLoginQueueMessage = {
|
2021-09-30 16:43:10 +02:00
|
|
|
type: 'addLogin',
|
|
|
|
username: loginInfo.username,
|
|
|
|
password: loginInfo.password,
|
|
|
|
domain: loginDomain,
|
|
|
|
uri: loginInfo.url,
|
|
|
|
tabId: tab.id,
|
2021-10-08 15:43:54 +02:00
|
|
|
expires: new Date((new Date()).getTime() + 5 * 60000), // 5 minutes
|
2021-09-30 16:03:14 +02:00
|
|
|
wasVaultLocked: isVaultLocked,
|
|
|
|
};
|
|
|
|
this.main.notificationQueue.push(message);
|
2021-09-30 16:43:10 +02:00
|
|
|
await this.main.checkNotificationQueue(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) {
|
2021-09-30 16:03:14 +02:00
|
|
|
const loginDomain = Utils.getDomain(changeData.url);
|
|
|
|
if (loginDomain == null) {
|
2019-03-06 22:50:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-30 16:03:14 +02:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
|
|
|
this.pushChangePasswordToQueue(null, loginDomain, changeData.newPassword, tab, true);
|
2018-08-01 05:24:11 +02:00
|
|
|
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) {
|
2021-02-10 16:40:15 +01:00
|
|
|
const passwordMatches = ciphers.filter(c => c.login.password === changeData.currentPassword);
|
2018-09-07 14:01:50 +02:00
|
|
|
if (passwordMatches.length === 1) {
|
|
|
|
id = passwordMatches[0].id;
|
|
|
|
}
|
|
|
|
} else if (ciphers.length === 1) {
|
|
|
|
id = ciphers[0].id;
|
|
|
|
}
|
|
|
|
if (id != null) {
|
2021-09-30 16:03:14 +02:00
|
|
|
this.pushChangePasswordToQueue(id, loginDomain, changeData.newPassword, tab);
|
2017-12-07 21:36:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 16:03:14 +02:00
|
|
|
private async pushChangePasswordToQueue(cipherId: string, loginDomain: string, newPassword: string, tab: any, isVaultLocked: boolean = false) {
|
2018-08-06 19:37:29 +02:00
|
|
|
// remove any old messages for this tab
|
|
|
|
this.removeTabFromNotificationQueue(tab);
|
2021-10-13 22:08:02 +02:00
|
|
|
const message: AddChangePasswordQueueMessage = {
|
2018-08-06 19:37:29 +02:00
|
|
|
type: 'changePassword',
|
|
|
|
cipherId: cipherId,
|
|
|
|
newPassword: newPassword,
|
|
|
|
domain: loginDomain,
|
|
|
|
tabId: tab.id,
|
2021-10-08 15:43:54 +02:00
|
|
|
expires: new Date((new Date()).getTime() + 5 * 60000), // 5 minutes
|
2021-09-30 16:03:14 +02:00
|
|
|
wasVaultLocked: isVaultLocked,
|
|
|
|
};
|
|
|
|
this.main.notificationQueue.push(message);
|
2018-08-06 19:37:29 +02:00
|
|
|
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() {
|
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
|
|
|
|
|
|
|
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 = {};
|
2021-09-30 16:51:53 +02:00
|
|
|
if (responseCommand === 'notificationBarGetFoldersList') {
|
2021-09-03 03:54:52 +02:00
|
|
|
responseData.folders = await this.folderService.getAllDecrypted();
|
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
|
|
|
}
|
2020-12-09 21:11:25 +01:00
|
|
|
|
|
|
|
private async allowPersonalOwnership(): Promise<boolean> {
|
2021-08-31 23:11:28 +02:00
|
|
|
return !await this.policyService.policyAppliesToUser(PolicyType.PersonalOwnership);
|
2020-12-09 21:11:25 +01:00
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|