1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 00:33:44 +00:00

various fixes for generator history

This commit is contained in:
Kyle Spearrin
2017-12-05 11:33:12 -05:00
parent 4b820bb7bd
commit 9822f95938
2 changed files with 23 additions and 35 deletions

View File

@@ -10,7 +10,6 @@ export default class ConstantsService {
static readonly enableAutoFillOnPageLoadKey: string = 'enableAutoFillOnPageLoad'; static readonly enableAutoFillOnPageLoadKey: string = 'enableAutoFillOnPageLoad';
static readonly lockOptionKey: string = 'lockOption'; static readonly lockOptionKey: string = 'lockOption';
static readonly lastActiveKey: string = 'lastActive'; static readonly lastActiveKey: string = 'lastActive';
static readonly generatedPasswordHistoryKey: string = 'generatedPasswordHistory';
// TODO: remove these instance properties once all references are reading from the static properties // TODO: remove these instance properties once all references are reading from the static properties
readonly environmentUrlsKey: string = 'environmentUrls'; readonly environmentUrlsKey: string = 'environmentUrls';
@@ -22,7 +21,6 @@ export default class ConstantsService {
readonly enableAutoFillOnPageLoadKey: string = 'enableAutoFillOnPageLoad'; readonly enableAutoFillOnPageLoadKey: string = 'enableAutoFillOnPageLoad';
readonly lockOptionKey: string = 'lockOption'; readonly lockOptionKey: string = 'lockOption';
readonly lastActiveKey: string = 'lastActive'; readonly lastActiveKey: string = 'lastActive';
readonly generatedPasswordHistoryKey: string = 'generatedPasswordHistory';
// TODO: Convert these objects to enums // TODO: Convert these objects to enums
readonly encType: any = { readonly encType: any = {

View File

@@ -1,7 +1,6 @@
import { CipherString } from '../models/domain/cipherString'; import { CipherString } from '../models/domain/cipherString';
import PasswordHistory from '../models/domain/passwordHistory'; import PasswordHistory from '../models/domain/passwordHistory';
import ConstantsService from './constants.service';
import CryptoService from './crypto.service'; import CryptoService from './crypto.service';
import UtilsService from './utils.service'; import UtilsService from './utils.service';
@@ -20,6 +19,7 @@ const DefaultOptions = {
const Keys = { const Keys = {
options: 'passwordGenerationOptions', options: 'passwordGenerationOptions',
history: 'generatedPasswordHistory',
}; };
const MaxPasswordsInHistory = 100; const MaxPasswordsInHistory = 100;
@@ -143,16 +143,13 @@ export default class PasswordGenerationService {
} }
optionsCache: any; optionsCache: any;
history: PasswordHistory[]; history: PasswordHistory[] = [];
constructor(private cryptoService: CryptoService) { constructor(private cryptoService: CryptoService) {
const self = this; UtilsService.getObjFromStorage<PasswordHistory[]>(Keys.history).then((encrypted) => {
return this.decryptHistory(encrypted);
const historyKey = ConstantsService.generatedPasswordHistoryKey;
UtilsService.getObjFromStorage<PasswordHistory[]>(historyKey).then((encrypted) => {
return self.decryptHistory(encrypted);
}).then((history) => { }).then((history) => {
self.history = history; this.history = history;
}); });
} }
@@ -162,7 +159,7 @@ export default class PasswordGenerationService {
async getOptions() { async getOptions() {
if (this.optionsCache == null) { if (this.optionsCache == null) {
let options = await UtilsService.getObjFromStorage(Keys.options); const options = await UtilsService.getObjFromStorage(Keys.options);
if (options == null) { if (options == null) {
this.optionsCache = DefaultOptions; this.optionsCache = DefaultOptions;
} else { } else {
@@ -179,7 +176,7 @@ export default class PasswordGenerationService {
} }
getHistory() { getHistory() {
return this.history; return this.history || new Array<PasswordHistory>();
} }
async addHistory(password: string) { async addHistory(password: string) {
@@ -195,53 +192,46 @@ export default class PasswordGenerationService {
this.history.shift(); this.history.shift();
} }
await this.saveHistory(); const newHistory = await this.encryptHistory();
await UtilsService.saveObjToStorage(Keys.history, newHistory);
} }
clear(): Promise<any> { async clear(): Promise<any> {
this.history = []; this.history = [];
return UtilsService.removeFromStorage(ConstantsService.generatedPasswordHistoryKey); await UtilsService.removeFromStorage(Keys.history);
} }
private async saveHistory() { private async encryptHistory(): Promise<PasswordHistory[]> {
const history = await this.encryptHistory(); if (this.history == null || this.history.length === 0) {
return UtilsService.saveObjToStorage(ConstantsService.generatedPasswordHistoryKey, history);
}
private encryptHistory(): Promise<PasswordHistory[]> {
if (this.history == null) {
return Promise.resolve([]); return Promise.resolve([]);
} }
const self = this; const promises = this.history.map(async (item) => {
const promises = self.history.map(async (item) => { const encrypted = await this.cryptoService.encrypt(item.password);
const encrypted = await self.cryptoService.encrypt(item.password);
return new PasswordHistory(encrypted.encryptedString, item.date); return new PasswordHistory(encrypted.encryptedString, item.date);
}); });
return Promise.all(promises); await Promise.all(promises);
} }
private decryptHistory(history: PasswordHistory[]): Promise<PasswordHistory[]> { private async decryptHistory(history: PasswordHistory[]): Promise<PasswordHistory[]> {
if (history == null) { if (history == null || this.history.length === 0) {
return Promise.resolve([]); return Promise.resolve([]);
} }
const self = this;
const promises = history.map(async (item) => { const promises = history.map(async (item) => {
const decrypted = await self.cryptoService.decrypt(new CipherString(item.password)); const decrypted = await this.cryptoService.decrypt(new CipherString(item.password));
return new PasswordHistory(decrypted, item.date); return new PasswordHistory(decrypted, item.date);
}); });
return Promise.all(promises); await Promise.all(promises);
} }
private matchesPrevious(password: string): boolean { private matchesPrevious(password: string): boolean {
if (this.history == null) { if (this.history == null || this.history.length === 0) {
return false; return false;
} }
const len = this.history.length; return this.history[this.history.length - 1].password === password;
return len !== 0 && this.history[len - 1].password === password;
} }
} }