2021-10-15 15:03:25 +02:00
|
|
|
import { CipherService } from "jslib-common/abstractions/cipher.service";
|
|
|
|
import { FolderService } from "jslib-common/abstractions/folder.service";
|
|
|
|
import { PolicyService } from "jslib-common/abstractions/policy.service";
|
|
|
|
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
2022-02-24 18:14:04 +01:00
|
|
|
import { CipherType } from "jslib-common/enums/cipherType";
|
|
|
|
import { PolicyType } from "jslib-common/enums/policyType";
|
|
|
|
import { Utils } from "jslib-common/misc/utils";
|
|
|
|
import { CipherView } from "jslib-common/models/view/cipherView";
|
|
|
|
import { LoginUriView } from "jslib-common/models/view/loginUriView";
|
|
|
|
import { LoginView } from "jslib-common/models/view/loginView";
|
2021-10-15 15:03:25 +02:00
|
|
|
|
|
|
|
import { BrowserApi } from "../browser/browserApi";
|
2022-02-24 18:14:04 +01:00
|
|
|
import { AutofillService } from "../services/abstractions/autofill.service";
|
|
|
|
import { StateService } from "../services/abstractions/state.service";
|
2021-10-15 15:03:25 +02:00
|
|
|
|
|
|
|
import MainBackground from "./main.background";
|
|
|
|
import AddChangePasswordQueueMessage from "./models/addChangePasswordQueueMessage";
|
|
|
|
import AddLoginQueueMessage from "./models/addLoginQueueMessage";
|
2021-10-19 12:10:31 +02:00
|
|
|
import AddLoginRuntimeMessage from "./models/addLoginRuntimeMessage";
|
|
|
|
import ChangePasswordRuntimeMessage from "./models/changePasswordRuntimeMessage";
|
2021-10-19 11:20:55 +02:00
|
|
|
import LockedVaultPendingNotificationsItem from "./models/lockedVaultPendingNotificationsItem";
|
2021-10-20 15:11:46 +02:00
|
|
|
import { NotificationQueueMessageType } from "./models/notificationQueueMessageType";
|
2021-10-15 15:03:25 +02:00
|
|
|
|
|
|
|
export default class NotificationBackground {
|
2021-10-15 19:47:48 +02:00
|
|
|
private notificationQueue: (AddLoginQueueMessage | AddChangePasswordQueueMessage)[] = [];
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-10-15 15:03:25 +02:00
|
|
|
constructor(
|
|
|
|
private main: MainBackground,
|
|
|
|
private autofillService: AutofillService,
|
|
|
|
private cipherService: CipherService,
|
|
|
|
private vaultTimeoutService: VaultTimeoutService,
|
|
|
|
private policyService: PolicyService,
|
2021-11-16 05:14:48 +01:00
|
|
|
private folderService: FolderService,
|
2022-01-27 22:22:51 +01:00
|
|
|
private stateService: StateService
|
2021-12-21 15:43:35 +01:00
|
|
|
) {}
|
|
|
|
|
2021-10-15 15:03:25 +02:00
|
|
|
async init() {
|
2021-11-16 05:14:48 +01:00
|
|
|
if (chrome.runtime == null) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2021-10-15 15:03:25 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 16:34:14 +02:00
|
|
|
BrowserApi.messageListener(
|
2021-10-18 16:31:59 +02:00
|
|
|
"notification.background",
|
|
|
|
async (msg: any, sender: chrome.runtime.MessageSender) => {
|
2021-10-18 16:34:14 +02:00
|
|
|
await this.processMessage(msg, sender);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-10-18 16:31:59 +02:00
|
|
|
this.cleanupNotificationQueue();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 16:34:14 +02:00
|
|
|
async processMessage(msg: any, sender: chrome.runtime.MessageSender) {
|
2021-10-15 15:32:56 +02:00
|
|
|
switch (msg.command) {
|
2021-10-18 16:31:59 +02:00
|
|
|
case "unlockCompleted":
|
|
|
|
if (msg.data.target !== "notification.background") {
|
2021-10-15 15:03:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-10-18 16:34:14 +02:00
|
|
|
await this.processMessage(msg.data.commandToRetry.msg, msg.data.commandToRetry.sender);
|
2021-10-15 15:03:25 +02:00
|
|
|
break;
|
2021-10-18 16:41:42 +02:00
|
|
|
case "bgGetDataForTab":
|
|
|
|
await this.getDataForTab(sender.tab, msg.responseCommand);
|
|
|
|
break;
|
2021-10-15 15:32:56 +02:00
|
|
|
case "bgCloseNotificationBar":
|
|
|
|
await BrowserApi.tabSendMessageData(sender.tab, "closeNotificationBar");
|
|
|
|
break;
|
|
|
|
case "bgAdjustNotificationBar":
|
|
|
|
await BrowserApi.tabSendMessageData(sender.tab, "adjustNotificationBar", msg.data);
|
|
|
|
break;
|
|
|
|
case "bgAddLogin":
|
|
|
|
await this.addLogin(msg.login, sender.tab);
|
|
|
|
break;
|
|
|
|
case "bgChangedPassword":
|
|
|
|
await this.changedPassword(msg.data, sender.tab);
|
|
|
|
break;
|
|
|
|
case "bgAddClose":
|
|
|
|
case "bgChangeClose":
|
|
|
|
this.removeTabFromNotificationQueue(sender.tab);
|
|
|
|
break;
|
|
|
|
case "bgAddSave":
|
|
|
|
case "bgChangeSave":
|
2021-10-18 16:42:32 +02:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
2021-10-19 11:20:55 +02:00
|
|
|
const retryMessage: LockedVaultPendingNotificationsItem = {
|
2021-10-18 16:42:32 +02:00
|
|
|
commandToRetry: {
|
|
|
|
msg: msg,
|
|
|
|
sender: sender,
|
|
|
|
},
|
|
|
|
target: "notification.background",
|
2021-10-15 15:32:56 +02:00
|
|
|
};
|
|
|
|
await BrowserApi.tabSendMessageData(
|
|
|
|
sender.tab,
|
|
|
|
"addToLockedVaultPendingNotifications",
|
|
|
|
retryMessage
|
2021-12-21 15:43:35 +01:00
|
|
|
);
|
2021-10-15 15:32:56 +02:00
|
|
|
await BrowserApi.tabSendMessageData(sender.tab, "promptForLogin");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await this.saveOrUpdateCredentials(sender.tab, msg.folder);
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
2021-10-15 19:42:05 +02:00
|
|
|
case "bgNeverSave":
|
|
|
|
await this.saveNever(sender.tab);
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
2021-10-15 19:42:05 +02:00
|
|
|
case "collectPageDetailsResponse":
|
|
|
|
switch (msg.sender) {
|
2022-02-24 18:14:04 +01:00
|
|
|
case "notificationBar": {
|
2021-10-15 19:42:05 +02:00
|
|
|
const forms = this.autofillService.getFormsWithPasswordFields(msg.details);
|
|
|
|
await BrowserApi.tabSendMessageData(msg.tab, "notificationBarPageDetails", {
|
2021-10-15 15:03:25 +02:00
|
|
|
details: msg.details,
|
2021-10-15 15:32:56 +02:00
|
|
|
forms: forms,
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
2021-10-15 15:03:25 +02:00
|
|
|
break;
|
2022-02-24 18:14:04 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
default:
|
|
|
|
break;
|
2021-10-15 15:03:25 +02:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-10-15 15:03:25 +02:00
|
|
|
|
|
|
|
async checkNotificationQueue(tab: chrome.tabs.Tab = null): Promise<void> {
|
|
|
|
if (this.notificationQueue.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tab != null) {
|
|
|
|
this.doNotificationQueueCheck(tab);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:09:13 +02:00
|
|
|
const currentTab = await BrowserApi.getTabFromCurrentWindow();
|
|
|
|
if (currentTab != null) {
|
2021-10-15 15:03:25 +02:00
|
|
|
this.doNotificationQueueCheck(currentTab);
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2021-10-15 15:03:25 +02:00
|
|
|
|
2021-10-15 19:42:05 +02:00
|
|
|
private cleanupNotificationQueue() {
|
|
|
|
for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
|
2021-10-15 15:03:25 +02:00
|
|
|
if (this.notificationQueue[i].expires < new Date()) {
|
2021-10-15 15:09:13 +02:00
|
|
|
this.notificationQueue.splice(i, 1);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-15 15:03:25 +02:00
|
|
|
setTimeout(() => this.cleanupNotificationQueue(), 2 * 60 * 1000); // check every 2 minutes
|
|
|
|
}
|
|
|
|
|
|
|
|
private doNotificationQueueCheck(tab: chrome.tabs.Tab): void {
|
|
|
|
if (tab == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:09:13 +02:00
|
|
|
const tabDomain = Utils.getDomain(tab.url);
|
2021-10-15 19:47:48 +02:00
|
|
|
if (tabDomain == null) {
|
2021-10-15 15:03:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:09:13 +02:00
|
|
|
for (let i = 0; i < this.notificationQueue.length; i++) {
|
|
|
|
if (
|
|
|
|
this.notificationQueue[i].tabId !== tab.id ||
|
|
|
|
this.notificationQueue[i].domain !== tabDomain
|
2021-12-21 15:43:35 +01:00
|
|
|
) {
|
2021-10-15 15:03:25 +02:00
|
|
|
continue;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 08:16:36 +01:00
|
|
|
if (this.notificationQueue[i].type === NotificationQueueMessageType.AddLogin) {
|
2021-10-15 15:09:13 +02:00
|
|
|
BrowserApi.tabSendMessageData(tab, "openNotificationBar", {
|
2021-12-21 15:43:35 +01:00
|
|
|
type: "add",
|
2021-10-15 15:03:25 +02:00
|
|
|
typeData: {
|
2021-10-15 15:09:13 +02:00
|
|
|
isVaultLocked: this.notificationQueue[i].wasVaultLocked,
|
2021-12-21 15:43:35 +01:00
|
|
|
},
|
2021-10-15 15:09:13 +02:00
|
|
|
});
|
2022-01-25 08:16:36 +01:00
|
|
|
} else if (this.notificationQueue[i].type === NotificationQueueMessageType.ChangePassword) {
|
2021-10-15 15:03:25 +02:00
|
|
|
BrowserApi.tabSendMessageData(tab, "openNotificationBar", {
|
|
|
|
type: "change",
|
|
|
|
typeData: {
|
2021-10-15 15:09:13 +02:00
|
|
|
isVaultLocked: this.notificationQueue[i].wasVaultLocked,
|
2021-12-21 15:43:35 +01:00
|
|
|
},
|
|
|
|
});
|
2021-10-15 15:03:25 +02:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
2021-10-15 15:03:25 +02:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2021-10-15 15:03:25 +02:00
|
|
|
|
2021-10-19 12:10:31 +02:00
|
|
|
private removeTabFromNotificationQueue(tab: chrome.tabs.Tab) {
|
2021-11-16 13:34:06 +01:00
|
|
|
for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
|
|
|
|
if (this.notificationQueue[i].tabId === tab.id) {
|
|
|
|
this.notificationQueue.splice(i, 1);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2021-11-16 13:34:06 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2021-11-16 13:34:06 +01:00
|
|
|
|
2021-10-15 15:03:25 +02:00
|
|
|
private async addLogin(loginInfo: AddLoginRuntimeMessage, tab: chrome.tabs.Tab) {
|
2022-01-27 22:22:51 +01:00
|
|
|
if (!(await this.stateService.getIsAuthenticated())) {
|
2021-10-15 15:03:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const loginDomain = Utils.getDomain(loginInfo.url);
|
|
|
|
if (loginDomain == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:04:47 +01:00
|
|
|
let normalizedUsername = loginInfo.username;
|
2021-11-16 13:34:06 +01:00
|
|
|
if (normalizedUsername != null) {
|
|
|
|
normalizedUsername = normalizedUsername.toLowerCase();
|
2021-12-15 19:04:47 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
const disabledAddLogin = await this.stateService.getDisableAddLoginNotification();
|
2021-11-02 10:09:44 +01:00
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
2021-10-15 15:03:25 +02:00
|
|
|
if (disabledAddLogin) {
|
2021-10-29 18:50:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:03:25 +02:00
|
|
|
if (!(await this.allowPersonalOwnership())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pushAddLoginToQueue(loginDomain, loginInfo, tab, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-19 12:10:31 +02:00
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(loginInfo.url);
|
|
|
|
const usernameMatches = ciphers.filter(
|
|
|
|
(c) => c.login.username != null && c.login.username.toLowerCase() === normalizedUsername
|
2021-12-21 15:43:35 +01:00
|
|
|
);
|
2021-10-19 12:10:31 +02:00
|
|
|
if (usernameMatches.length === 0) {
|
|
|
|
if (disabledAddLogin) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-02 10:09:44 +01:00
|
|
|
if (!(await this.allowPersonalOwnership())) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-19 12:10:31 +02:00
|
|
|
this.pushAddLoginToQueue(loginDomain, loginInfo, tab);
|
2021-10-15 15:03:25 +02:00
|
|
|
} else if (
|
|
|
|
usernameMatches.length === 1 &&
|
|
|
|
usernameMatches[0].login.password !== loginInfo.password
|
2021-12-21 15:43:35 +01:00
|
|
|
) {
|
2022-01-27 22:22:51 +01:00
|
|
|
const disabledChangePassword =
|
|
|
|
await this.stateService.getDisableChangedPasswordNotification();
|
2021-10-15 15:03:25 +02:00
|
|
|
if (disabledChangePassword) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-10-19 12:10:31 +02:00
|
|
|
this.pushChangePasswordToQueue(usernameMatches[0].id, loginDomain, loginInfo.password, tab);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 12:10:31 +02:00
|
|
|
private async pushAddLoginToQueue(
|
|
|
|
loginDomain: string,
|
|
|
|
loginInfo: AddLoginRuntimeMessage,
|
|
|
|
tab: chrome.tabs.Tab,
|
2022-02-24 18:14:04 +01:00
|
|
|
isVaultLocked = false
|
2021-12-21 15:43:35 +01:00
|
|
|
) {
|
2021-10-15 15:03:25 +02:00
|
|
|
// remove any old messages for this tab
|
|
|
|
this.removeTabFromNotificationQueue(tab);
|
|
|
|
const message: AddLoginQueueMessage = {
|
2022-01-25 08:16:36 +01:00
|
|
|
type: NotificationQueueMessageType.AddLogin,
|
2021-10-15 15:03:25 +02:00
|
|
|
username: loginInfo.username,
|
|
|
|
password: loginInfo.password,
|
|
|
|
domain: loginDomain,
|
|
|
|
uri: loginInfo.url,
|
|
|
|
tabId: tab.id,
|
|
|
|
expires: new Date(new Date().getTime() + 5 * 60000), // 5 minutes
|
|
|
|
wasVaultLocked: isVaultLocked,
|
|
|
|
};
|
2021-10-15 15:09:13 +02:00
|
|
|
this.notificationQueue.push(message);
|
2021-10-15 15:03:25 +02:00
|
|
|
await this.checkNotificationQueue(tab);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 12:10:31 +02:00
|
|
|
private async changedPassword(changeData: ChangePasswordRuntimeMessage, tab: chrome.tabs.Tab) {
|
2021-10-15 15:03:25 +02:00
|
|
|
const loginDomain = Utils.getDomain(changeData.url);
|
|
|
|
if (loginDomain == null) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2021-10-15 15:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (await this.vaultTimeoutService.isLocked()) {
|
|
|
|
this.pushChangePasswordToQueue(null, loginDomain, changeData.newPassword, tab, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let id: string = null;
|
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(changeData.url);
|
|
|
|
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;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2021-10-15 15:03:25 +02:00
|
|
|
if (id != null) {
|
|
|
|
this.pushChangePasswordToQueue(id, loginDomain, changeData.newPassword, tab);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 19:42:05 +02:00
|
|
|
private async pushChangePasswordToQueue(
|
|
|
|
cipherId: string,
|
|
|
|
loginDomain: string,
|
|
|
|
newPassword: string,
|
|
|
|
tab: chrome.tabs.Tab,
|
2022-02-24 18:14:04 +01:00
|
|
|
isVaultLocked = false
|
2021-12-21 15:43:35 +01:00
|
|
|
) {
|
2021-10-15 15:03:25 +02:00
|
|
|
// remove any old messages for this tab
|
|
|
|
this.removeTabFromNotificationQueue(tab);
|
|
|
|
const message: AddChangePasswordQueueMessage = {
|
2022-01-25 08:16:36 +01:00
|
|
|
type: NotificationQueueMessageType.ChangePassword,
|
2021-10-15 15:03:25 +02:00
|
|
|
cipherId: cipherId,
|
|
|
|
newPassword: newPassword,
|
|
|
|
domain: loginDomain,
|
|
|
|
tabId: tab.id,
|
|
|
|
expires: new Date(new Date().getTime() + 5 * 60000), // 5 minutes
|
|
|
|
wasVaultLocked: isVaultLocked,
|
2021-12-21 15:43:35 +01:00
|
|
|
};
|
2021-10-15 15:09:13 +02:00
|
|
|
this.notificationQueue.push(message);
|
2021-10-15 15:03:25 +02:00
|
|
|
await this.checkNotificationQueue(tab);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 19:42:05 +02:00
|
|
|
private async saveOrUpdateCredentials(tab: chrome.tabs.Tab, folderId?: string) {
|
2021-10-15 15:09:13 +02:00
|
|
|
for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
|
|
|
|
const queueMessage = this.notificationQueue[i];
|
2021-12-21 15:43:35 +01:00
|
|
|
if (
|
2021-10-15 19:47:48 +02:00
|
|
|
queueMessage.tabId !== tab.id ||
|
2022-01-25 08:16:36 +01:00
|
|
|
(queueMessage.type !== NotificationQueueMessageType.AddLogin &&
|
|
|
|
queueMessage.type !== NotificationQueueMessageType.ChangePassword)
|
2021-12-21 15:43:35 +01:00
|
|
|
) {
|
2021-10-15 15:03:25 +02:00
|
|
|
continue;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:03:25 +02:00
|
|
|
const tabDomain = Utils.getDomain(tab.url);
|
|
|
|
if (tabDomain != null && tabDomain !== queueMessage.domain) {
|
|
|
|
continue;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:09:13 +02:00
|
|
|
this.notificationQueue.splice(i, 1);
|
2021-10-15 15:03:25 +02:00
|
|
|
BrowserApi.tabSendMessageData(tab, "closeNotificationBar");
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2022-01-25 08:16:36 +01:00
|
|
|
if (queueMessage.type === NotificationQueueMessageType.ChangePassword) {
|
|
|
|
const changePasswordMessage = queueMessage as AddChangePasswordQueueMessage;
|
|
|
|
const cipher = await this.getDecryptedCipherById(changePasswordMessage.cipherId);
|
2021-10-15 15:03:25 +02:00
|
|
|
if (cipher == null) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2021-10-15 15:03:25 +02:00
|
|
|
}
|
2022-01-25 08:16:36 +01:00
|
|
|
await this.updateCipher(cipher, changePasswordMessage.newPassword);
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-25 08:16:36 +01:00
|
|
|
if (queueMessage.type === NotificationQueueMessageType.AddLogin) {
|
|
|
|
if (!queueMessage.wasVaultLocked) {
|
|
|
|
await this.createNewCipher(queueMessage as AddLoginQueueMessage, folderId);
|
|
|
|
BrowserApi.tabSendMessageData(tab, "addedCipher");
|
|
|
|
return;
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2022-01-25 08:16:36 +01:00
|
|
|
// If the vault was locked, check if a cipher needs updating instead of creating a new one
|
|
|
|
const addLoginMessage = queueMessage as AddLoginQueueMessage;
|
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(addLoginMessage.uri);
|
2021-10-15 15:03:25 +02:00
|
|
|
const usernameMatches = ciphers.filter(
|
2022-01-25 08:16:36 +01:00
|
|
|
(c) =>
|
|
|
|
c.login.username != null && c.login.username.toLowerCase() === addLoginMessage.username
|
2021-12-21 15:43:35 +01:00
|
|
|
);
|
|
|
|
|
2021-10-15 15:03:25 +02:00
|
|
|
if (usernameMatches.length >= 1) {
|
2022-01-25 08:16:36 +01:00
|
|
|
await this.updateCipher(usernameMatches[0], addLoginMessage.password);
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2021-10-15 15:03:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 08:16:36 +01:00
|
|
|
await this.createNewCipher(addLoginMessage, folderId);
|
|
|
|
BrowserApi.tabSendMessageData(tab, "addedCipher");
|
2021-10-15 15:03:25 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 19:42:05 +02:00
|
|
|
private async createNewCipher(queueMessage: AddLoginQueueMessage, folderId: string) {
|
2021-10-15 15:09:13 +02:00
|
|
|
const loginModel = new LoginView();
|
2021-10-15 19:47:48 +02:00
|
|
|
const loginUri = new LoginUriView();
|
|
|
|
loginUri.uri = queueMessage.uri;
|
2021-10-15 15:03:25 +02:00
|
|
|
loginModel.uris = [loginUri];
|
2021-10-15 19:47:48 +02:00
|
|
|
loginModel.username = queueMessage.username;
|
2021-10-15 15:03:25 +02:00
|
|
|
loginModel.password = queueMessage.password;
|
|
|
|
const model = new CipherView();
|
2021-10-15 19:47:48 +02:00
|
|
|
model.name = Utils.getHostname(queueMessage.uri) || queueMessage.domain;
|
2021-10-15 15:03:25 +02:00
|
|
|
model.name = model.name.replace(/^www\./, "");
|
2021-10-15 19:47:48 +02:00
|
|
|
model.type = CipherType.Login;
|
|
|
|
model.login = loginModel;
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-10-15 19:47:48 +02:00
|
|
|
if (!Utils.isNullOrWhitespace(folderId)) {
|
2021-10-15 15:03:25 +02:00
|
|
|
const folders = await this.folderService.getAllDecrypted();
|
|
|
|
if (folders.some((x) => x.id === folderId)) {
|
|
|
|
model.folderId = folderId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cipher = await this.cipherService.encrypt(model);
|
|
|
|
await this.cipherService.saveWithServer(cipher);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:03:25 +02:00
|
|
|
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);
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:03:25 +02:00
|
|
|
private async saveNever(tab: chrome.tabs.Tab) {
|
|
|
|
for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
|
|
|
|
const queueMessage = this.notificationQueue[i];
|
2021-12-21 15:43:35 +01:00
|
|
|
if (
|
2021-10-15 19:47:48 +02:00
|
|
|
queueMessage.tabId !== tab.id ||
|
2022-01-25 08:16:36 +01:00
|
|
|
queueMessage.type !== NotificationQueueMessageType.AddLogin
|
2021-12-21 15:43:35 +01:00
|
|
|
) {
|
2021-10-15 15:03:25 +02:00
|
|
|
continue;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:03:25 +02:00
|
|
|
const tabDomain = Utils.getDomain(tab.url);
|
|
|
|
if (tabDomain != null && tabDomain !== queueMessage.domain) {
|
|
|
|
continue;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:03:25 +02:00
|
|
|
this.notificationQueue.splice(i, 1);
|
|
|
|
BrowserApi.tabSendMessageData(tab, "closeNotificationBar");
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-10-15 15:03:25 +02:00
|
|
|
const hostname = Utils.getHostname(tab.url);
|
|
|
|
await this.cipherService.saveNeverDomain(hostname);
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2021-10-15 15:03:25 +02:00
|
|
|
|
2021-10-15 19:42:05 +02:00
|
|
|
private async getDataForTab(tab: chrome.tabs.Tab, responseCommand: string) {
|
2021-10-15 15:09:13 +02:00
|
|
|
const responseData: any = {};
|
2021-10-15 19:47:48 +02:00
|
|
|
if (responseCommand === "notificationBarGetFoldersList") {
|
2021-10-15 15:03:25 +02:00
|
|
|
responseData.folders = await this.folderService.getAllDecrypted();
|
|
|
|
}
|
|
|
|
|
2021-10-15 19:42:05 +02:00
|
|
|
await BrowserApi.tabSendMessageData(tab, responseCommand, responseData);
|
2021-10-15 15:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async allowPersonalOwnership(): Promise<boolean> {
|
|
|
|
return !(await this.policyService.policyAppliesToUser(PolicyType.PersonalOwnership));
|
|
|
|
}
|
|
|
|
}
|