Combined saving credentials and added fallback to update only password if only that changed

This commit is contained in:
Daniel James Smith 2021-09-30 16:48:06 +02:00
parent 838bfe9454
commit de0e9a4d94
No known key found for this signature in database
GPG Key ID: 03E4BD365FF06726
1 changed files with 61 additions and 42 deletions

View File

@ -127,10 +127,8 @@ export default class RuntimeBackground {
this.removeTabFromNotificationQueue(sender.tab); this.removeTabFromNotificationQueue(sender.tab);
break; break;
case 'bgAddSave': case 'bgAddSave':
await this.saveAddLogin(sender.tab, msg.folder);
break;
case 'bgChangeSave': case 'bgChangeSave':
await this.saveChangePassword(sender.tab); await this.saveOrUpdateCredentials(sender.tab, msg.folder);
break; break;
case 'bgNeverSave': case 'bgNeverSave':
await this.saveNever(sender.tab); await this.saveNever(sender.tab);
@ -235,11 +233,11 @@ export default class RuntimeBackground {
this.pageDetailsToAutoFill = []; this.pageDetailsToAutoFill = [];
} }
private async saveAddLogin(tab: any, folderId: string) { private async saveOrUpdateCredentials(tab: any, folderId?: string) {
for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) { for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) {
const queueMessage = this.main.notificationQueue[i]; const queueMessage = this.main.notificationQueue[i];
if (queueMessage.tabId !== tab.id || queueMessage.type !== 'addLogin') { if (queueMessage.tabId !== tab.id ||
(queueMessage.type !== 'addLogin' && queueMessage.type !== 'changePassword')) {
continue; continue;
} }
@ -251,6 +249,38 @@ export default class RuntimeBackground {
this.main.notificationQueue.splice(i, 1); this.main.notificationQueue.splice(i, 1);
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar'); BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
if (queueMessage.type === 'changePassword') {
const message = (queueMessage as addChangePasswordQueueMessage);
const cipher = await this.getDecryptedCipherById(message.cipherId);
if (cipher == null) {
return;
}
await this.updateCipher(cipher, message.newPassword);
return;
}
if (!queueMessage.wasVaultLocked) {
await this.createNewCipher(queueMessage, folderId);
}
// If the vault was locked, check if a cipher needs updating instead of creating a new one
if (queueMessage.type === 'addLogin' && queueMessage.wasVaultLocked === true) {
const message = (queueMessage as addLoginQueueMessage);
const ciphers = await this.cipherService.getAllDecryptedForUrl(message.uri);
const usernameMatches = ciphers.filter(c => c.login.username != null &&
c.login.username.toLowerCase() === message.username);
if (usernameMatches.length === 1) {
await this.updateCipher(usernameMatches[0], message.password);
return;
}
await this.createNewCipher(message, folderId);
}
}
}
private async createNewCipher(queueMessage: addLoginQueueMessage, folderId: string) {
const loginModel = new LoginView(); const loginModel = new LoginView();
const loginUri = new LoginUriView(); const loginUri = new LoginUriView();
loginUri.uri = queueMessage.uri; loginUri.uri = queueMessage.uri;
@ -273,31 +303,20 @@ export default class RuntimeBackground {
const cipher = await this.cipherService.encrypt(model); const cipher = await this.cipherService.encrypt(model);
await this.cipherService.saveWithServer(cipher); await this.cipherService.saveWithServer(cipher);
} }
}
private async saveChangePassword(tab: any) { private async getDecryptedCipherById(cipherId: string) {
const cipher = await this.cipherService.get(cipherId);
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;
}
const tabDomain = Utils.getDomain(tab.url);
if (tabDomain != null && tabDomain !== queueMessage.domain) {
continue;
}
this.main.notificationQueue.splice(i, 1);
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
const cipher = await this.cipherService.get(queueMessage.cipherId);
if (cipher != null && cipher.type === CipherType.Login) { if (cipher != null && cipher.type === CipherType.Login) {
const model = await cipher.decrypt(); return await cipher.decrypt();
model.login.password = queueMessage.newPassword;
const newCipher = await this.cipherService.encrypt(model);
await this.cipherService.saveWithServer(newCipher);
} }
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);
} }
} }