1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-03 13:33:32 +01:00

try to filter some get requests

This commit is contained in:
Kyle Spearrin 2019-10-21 14:16:16 -04:00
parent ba7ea4ec85
commit 268852035a

View File

@ -94,7 +94,7 @@ export class GetCommand {
} }
} }
private async getCipherView(id: string): Promise<CipherView | string[]> { private async getCipherView(id: string): Promise<CipherView | CipherView[]> {
let decCipher: CipherView = null; let decCipher: CipherView = null;
if (Utils.isGuid(id)) { if (Utils.isGuid(id)) {
const cipher = await this.cipherService.get(id); const cipher = await this.cipherService.get(id);
@ -105,7 +105,7 @@ export class GetCommand {
let ciphers = await this.cipherService.getAllDecrypted(); let ciphers = await this.cipherService.getAllDecrypted();
ciphers = this.searchService.searchCiphersBasic(ciphers, id); ciphers = this.searchService.searchCiphersBasic(ciphers, id);
if (ciphers.length > 1) { if (ciphers.length > 1) {
return ciphers.map((c) => c.id); return ciphers;
} }
if (ciphers.length > 0) { if (ciphers.length > 0) {
decCipher = ciphers[0]; decCipher = ciphers[0];
@ -115,20 +115,29 @@ export class GetCommand {
return decCipher; return decCipher;
} }
private async getCipher(id: string) { private async getCipher(id: string, filter?: (c: CipherView) => boolean) {
const decCipher = await this.getCipherView(id); let decCipher = await this.getCipherView(id);
if (decCipher == null) { if (decCipher == null) {
return Response.notFound(); return Response.notFound();
} }
if (Array.isArray(decCipher)) { if (Array.isArray(decCipher)) {
return Response.multipleResults(decCipher); if (filter != null) {
const filteredCiphers = decCipher.filter(filter);
if (filteredCiphers.length === 1) {
decCipher = filteredCiphers[0];
}
}
if (Array.isArray(decCipher)) {
return Response.multipleResults(decCipher.map((c) => c.id));
}
} }
const res = new CipherResponse(decCipher); const res = new CipherResponse(decCipher);
return Response.success(res); return Response.success(res);
} }
private async getUsername(id: string) { private async getUsername(id: string) {
const cipherResponse = await this.getCipher(id); const cipherResponse = await this.getCipher(id,
(c) => c.type === CipherType.Login && !Utils.isNullOrWhitespace(c.login.username));
if (!cipherResponse.success) { if (!cipherResponse.success) {
return cipherResponse; return cipherResponse;
} }
@ -138,7 +147,7 @@ export class GetCommand {
return Response.badRequest('Not a login.'); return Response.badRequest('Not a login.');
} }
if (cipher.login.username == null || cipher.login.username === '') { if (Utils.isNullOrWhitespace(cipher.login.username)) {
return Response.error('No username available for this login.'); return Response.error('No username available for this login.');
} }
@ -147,7 +156,8 @@ export class GetCommand {
} }
private async getPassword(id: string) { private async getPassword(id: string) {
const cipherResponse = await this.getCipher(id); const cipherResponse = await this.getCipher(id,
(c) => c.type === CipherType.Login && !Utils.isNullOrWhitespace(c.login.password));
if (!cipherResponse.success) { if (!cipherResponse.success) {
return cipherResponse; return cipherResponse;
} }
@ -157,7 +167,7 @@ export class GetCommand {
return Response.badRequest('Not a login.'); return Response.badRequest('Not a login.');
} }
if (cipher.login.password == null || cipher.login.password === '') { if (Utils.isNullOrWhitespace(cipher.login.password)) {
return Response.error('No password available for this login.'); return Response.error('No password available for this login.');
} }
@ -166,7 +176,9 @@ export class GetCommand {
} }
private async getUri(id: string) { private async getUri(id: string) {
const cipherResponse = await this.getCipher(id); const cipherResponse = await this.getCipher(id,
(c) => c.type === CipherType.Login && c.login.uris != null && c.login.uris.length > 0 &&
c.login.uris[0].uri !== '');
if (!cipherResponse.success) { if (!cipherResponse.success) {
return cipherResponse; return cipherResponse;
} }
@ -185,7 +197,8 @@ export class GetCommand {
} }
private async getTotp(id: string) { private async getTotp(id: string) {
const cipherResponse = await this.getCipher(id); const cipherResponse = await this.getCipher(id,
(c) => c.type === CipherType.Login && !Utils.isNullOrWhitespace(c.login.totp));
if (!cipherResponse.success) { if (!cipherResponse.success) {
return cipherResponse; return cipherResponse;
} }
@ -195,7 +208,7 @@ export class GetCommand {
return Response.badRequest('Not a login.'); return Response.badRequest('Not a login.');
} }
if (cipher.login.totp == null || cipher.login.totp === '') { if (Utils.isNullOrWhitespace(cipher.login.totp)) {
return Response.error('No TOTP available for this login.'); return Response.error('No TOTP available for this login.');
} }