Skip to content

Commit

Permalink
Add new method for cycling through every login
Browse files Browse the repository at this point in the history
To be used from browser extension when autofilling.
Related PR: bitwarden/clients#956
  • Loading branch information
xusoo committed Aug 10, 2020
1 parent 7c3a9d6 commit c7d2ce4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/abstractions/cipher.service.ts
Expand Up @@ -24,6 +24,7 @@ export abstract class CipherService {
getAllDecryptedForUrl: (url: string, includeOtherTypes?: CipherType[]) => Promise<CipherView[]>;
getAllFromApiForOrganization: (organizationId: string) => Promise<CipherView[]>;
getLastUsedForUrl: (url: string) => Promise<CipherView>;
getNextCipherForUrl: (url: string) => Promise<CipherView>;
updateLastUsedDate: (id: string) => Promise<void>;
saveNeverDomain: (domain: string) => Promise<void>;
saveWithServer: (cipher: Cipher) => Promise<any>;
Expand Down
38 changes: 32 additions & 6 deletions src/services/cipher.service.ts
Expand Up @@ -63,6 +63,8 @@ export class CipherService implements CipherServiceAbstraction {
// tslint:disable-next-line
_decryptedCipherCache: CipherView[];

private lastSortedCiphers: CipherView[] = [];

constructor(private cryptoService: CryptoService, private userService: UserService,
private settingsService: SettingsService, private apiService: ApiService,
private storageService: StorageService, private i18nService: I18nService,
Expand Down Expand Up @@ -437,13 +439,11 @@ export class CipherService implements CipherServiceAbstraction {
}

async getLastUsedForUrl(url: string): Promise<CipherView> {
const ciphers = await this.getAllDecryptedForUrl(url);
if (ciphers.length === 0) {
return null;
}
return this.getCipherForUrl(url, true);
}

const sortedCiphers = ciphers.sort(this.sortCiphersByLastUsed);
return sortedCiphers[0];
async getNextCipherForUrl(url: string): Promise<CipherView> {
return this.getCipherForUrl(url, false);
}

async updateLastUsedDate(id: string): Promise<void> {
Expand Down Expand Up @@ -1002,4 +1002,30 @@ export class CipherService implements CipherServiceAbstraction {
throw new Error('Unknown cipher type.');
}
}

private async getCipherForUrl(url: string, lastUsed: boolean): Promise<CipherView> {
const ciphers = await this.getAllDecryptedForUrl(url);
if (ciphers.length === 0) {
return null;
}

ciphers.sort(this.sortCiphersByLastUsed);

const lastUsedCipher = ciphers[0];

if (lastUsed) {
return lastUsedCipher;
}

if (!this.arraysContainSameElements(this.lastSortedCiphers, ciphers)) {
this.lastSortedCiphers = ciphers;
}

const lastUsedIndex = this.lastSortedCiphers.indexOf(lastUsedCipher);
return this.lastSortedCiphers[(lastUsedIndex + 1) % this.lastSortedCiphers.length];
}

private arraysContainSameElements(arrayA: any[], arrayB: any[]) {
return arrayA.length === arrayB.length && arrayA.every((cipher) => arrayB.includes(cipher));
}
}

0 comments on commit c7d2ce4

Please sign in to comment.