1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-02-21 02:11:54 +01:00

Add login launch data (#174)

* added launch time data to CipherView for autofill

* removed unused code

* fixed linter errors
This commit is contained in:
Addison Beck 2020-09-23 12:41:25 -04:00 committed by GitHub
parent 5cb3e9c965
commit 26d40d4c43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 4 deletions

View File

@ -26,8 +26,10 @@ export abstract class CipherService {
defaultMatch?: UriMatchType) => Promise<CipherView[]>;
getAllFromApiForOrganization: (organizationId: string) => Promise<CipherView[]>;
getLastUsedForUrl: (url: string) => Promise<CipherView>;
getLastLaunchedForUrl: (url: string) => Promise<CipherView>;
getNextCipherForUrl: (url: string) => Promise<CipherView>;
updateLastUsedDate: (id: string) => Promise<void>;
updateLastLaunchedDate: (id: string) => Promise<void>;
saveNeverDomain: (domain: string) => Promise<void>;
saveWithServer: (cipher: Cipher) => Promise<any>;
shareWithServer: (cipher: CipherView, organizationId: string, collectionIds: string[]) => Promise<any>;

View File

@ -23,6 +23,10 @@ export class SortedCiphersCache {
return this.isCached(url) ? this.sortedCiphersByUrl.get(url).getLastUsed() : null;
}
getLastLaunched(url: string) {
return this.isCached(url) ? this.sortedCiphersByUrl.get(url).getLastLaunched() : null;
}
getNext(url: string) {
this.resetTimer(url);
return this.isCached(url) ? this.sortedCiphersByUrl.get(url).getNext() : null;
@ -52,6 +56,11 @@ class Ciphers {
return this.ciphers[this.lastUsedIndex];
}
getLastLaunched() {
const sortedCiphers = this.ciphers.sort((x, y) => y.localData?.lastLaunched?.valueOf() - x.localData?.lastLaunched?.valueOf());
return sortedCiphers[0];
}
getNext() {
const nextIndex = (this.lastUsedIndex + 1) % this.ciphers.length;
this.lastUsedIndex = nextIndex;

View File

@ -445,11 +445,15 @@ export class CipherService implements CipherServiceAbstraction {
}
async getLastUsedForUrl(url: string): Promise<CipherView> {
return this.getCipherForUrl(url, true);
return this.getCipherForUrl(url, true, false);
}
async getLastLaunchedForUrl(url: string): Promise<CipherView> {
return this.getCipherForUrl(url, false, true);
}
async getNextCipherForUrl(url: string): Promise<CipherView> {
return this.getCipherForUrl(url, false);
return this.getCipherForUrl(url, false, false);
}
async updateLastUsedDate(id: string): Promise<void> {
@ -481,6 +485,35 @@ export class CipherService implements CipherServiceAbstraction {
}
}
async updateLastLaunchedDate(id: string): Promise<void> {
let ciphersLocalData = await this.storageService.get<any>(Keys.localData);
if (!ciphersLocalData) {
ciphersLocalData = {};
}
if (ciphersLocalData[id]) {
ciphersLocalData[id].lastLaunched = new Date().getTime();
} else {
ciphersLocalData[id] = {
lastUsedDate: new Date().getTime(),
};
}
await this.storageService.save(Keys.localData, ciphersLocalData);
if (this.decryptedCipherCache == null) {
return;
}
for (let i = 0; i < this.decryptedCipherCache.length; i++) {
const cached = this.decryptedCipherCache[i];
if (cached.id === id) {
cached.localData = ciphersLocalData[id];
break;
}
}
}
async saveNeverDomain(domain: string): Promise<void> {
if (domain == null) {
return;
@ -1009,7 +1042,7 @@ export class CipherService implements CipherServiceAbstraction {
}
}
private async getCipherForUrl(url: string, lastUsed: boolean): Promise<CipherView> {
private async getCipherForUrl(url: string, lastUsed: boolean, lastLaunched: boolean): Promise<CipherView> {
if (!this.sortedCiphersCache.isCached(url)) {
const ciphers = await this.getAllDecryptedForUrl(url);
if (!ciphers) {
@ -1018,6 +1051,13 @@ export class CipherService implements CipherServiceAbstraction {
this.sortedCiphersCache.addCiphers(url, ciphers);
}
return lastUsed ? this.sortedCiphersCache.getLastUsed(url) : this.sortedCiphersCache.getNext(url);
if (lastLaunched) {
return this.sortedCiphersCache.getLastLaunched(url);
} else if (lastUsed) {
return this.sortedCiphersCache.getLastUsed(url);
}
else {
return this.sortedCiphersCache.getNext(url);
}
}
}