From 0490c6ba3f35ad489f3699aa31e3a6759918f1a3 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 16 May 2018 11:54:59 -0400 Subject: [PATCH] allow gets by search term --- src/commands/delete.command.ts | 4 ++ src/commands/edit.command.ts | 4 ++ src/commands/get.command.ts | 92 +++++++++++++++++++++++++++------- src/program.ts | 2 +- 4 files changed, 83 insertions(+), 19 deletions(-) diff --git a/src/commands/delete.command.ts b/src/commands/delete.command.ts index e96123e2ff..de88f7de3a 100644 --- a/src/commands/delete.command.ts +++ b/src/commands/delete.command.ts @@ -9,6 +9,10 @@ export class DeleteCommand { constructor(private cipherService: CipherService, private folderService: FolderService) { } async run(object: string, id: string, cmd: program.Command): Promise { + if (id != null) { + id = id.toLowerCase(); + } + switch (object.toLowerCase()) { case 'item': return await this.deleteCipher(id); diff --git a/src/commands/edit.command.ts b/src/commands/edit.command.ts index 796761fc27..557b8ff153 100644 --- a/src/commands/edit.command.ts +++ b/src/commands/edit.command.ts @@ -20,6 +20,10 @@ export class EditCommand { return Response.badRequest('Error parsing the encoded request data.'); } + if (id != null) { + id = id.toLowerCase(); + } + switch (object.toLowerCase()) { case 'item': return await this.editCipher(id, req); diff --git a/src/commands/get.command.ts b/src/commands/get.command.ts index 8b70c39dfb..7a7eb16abc 100644 --- a/src/commands/get.command.ts +++ b/src/commands/get.command.ts @@ -9,6 +9,10 @@ import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration import { SyncService } from 'jslib/abstractions/sync.service'; import { TotpService } from 'jslib/abstractions/totp.service'; +import { CipherView } from 'jslib/models/view/cipherView'; +import { CollectionView } from 'jslib/models/view/collectionView'; +import { FolderView } from 'jslib/models/view/folderView'; + import { Response } from '../models/response'; import { CipherResponse } from '../models/response/cipherResponse'; import { CollectionResponse } from '../models/response/collectionResponse'; @@ -26,6 +30,8 @@ import { Login } from '../models/login'; import { LoginUri } from '../models/loginUri'; import { SecureNote } from '../models/secureNote'; +import { CliUtils } from '../utils'; + export class GetCommand { constructor(private cipherService: CipherService, private folderService: FolderService, private collectionService: CollectionService, private totpService: TotpService, @@ -36,6 +42,10 @@ export class GetCommand { return Response.badRequest('`id` argument is required.'); } + if (id != null) { + id = id.toLowerCase(); + } + switch (object.toLowerCase()) { case 'item': return await this.getCipher(id); @@ -57,32 +67,46 @@ export class GetCommand { } private async getCipher(id: string) { - const cipher = await this.cipherService.get(id); - if (cipher == null) { - return Response.notFound(); + let decCipher: CipherView = null; + if (this.isGuid(id)) { + const cipher = await this.cipherService.get(id); + if (cipher != null) { + decCipher = await cipher.decrypt(); + } + } else if (id.trim() !== '') { + let ciphers = await this.cipherService.getAllDecrypted(); + ciphers = CliUtils.searchCiphers(ciphers, id); + if (ciphers.length > 1) { + return Response.error('More than one result was found.'); + } + if (ciphers.length > 0) { + decCipher = ciphers[0]; + } } - const decCipher = await cipher.decrypt(); + if (decCipher == null) { + return Response.notFound(); + } const res = new CipherResponse(decCipher); return Response.success(res); } private async getTotp(id: string) { - const cipher = await this.cipherService.get(id); - if (cipher == null) { - return Response.notFound(); + const cipherResponse = await this.getCipher(id); + if (!cipherResponse.success) { + return cipherResponse; } + const cipher = cipherResponse.data as CipherResponse; if (cipher.type !== CipherType.Login) { return Response.badRequest('Not a login.'); } - const decCipher = await cipher.decrypt(); - if (decCipher.login.totp == null || decCipher.login.totp === '') { + if (cipher.login.totp == null || cipher.login.totp === '') { return Response.error('No TOTP available for this login.'); } - const totp = await this.totpService.getCode(decCipher.login.totp); + const totp = await this.totpService.getCode(cipher.login.totp); if (totp == null) { return Response.error('Couldn\'t generate TOTP code.'); } @@ -92,27 +116,59 @@ export class GetCommand { } private async getFolder(id: string) { - const folder = await this.folderService.get(id); - if (folder == null) { - return Response.notFound(); + let decFolder: FolderView = null; + if (this.isGuid(id)) { + const folder = await this.folderService.get(id); + if (folder != null) { + decFolder = await folder.decrypt(); + } + } else if (id.trim() !== '') { + let folders = await this.folderService.getAllDecrypted(); + folders = CliUtils.searchFolders(folders, id); + if (folders.length > 1) { + return Response.error('More than one result was found.'); + } + if (folders.length > 0) { + decFolder = folders[0]; + } } - const decFolder = await folder.decrypt(); + if (decFolder == null) { + return Response.notFound(); + } const res = new FolderResponse(decFolder); return Response.success(res); } private async getCollection(id: string) { - const collection = await this.collectionService.get(id); - if (collection == null) { - return Response.notFound(); + let decCollection: CollectionView = null; + if (this.isGuid(id)) { + const collection = await this.collectionService.get(id); + if (collection != null) { + decCollection = await collection.decrypt(); + } + } else if (id.trim() !== '') { + let collections = await this.collectionService.getAllDecrypted(); + collections = CliUtils.searchCollections(collections, id); + if (collections.length > 1) { + return Response.error('More than one result was found.'); + } + if (collections.length > 0) { + decCollection = collections[0]; + } } - const decCollection = await collection.decrypt(); + if (decCollection == null) { + return Response.notFound(); + } const res = new CollectionResponse(decCollection); return Response.success(res); } + private isGuid(id: string) { + return RegExp(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, 'i').test(id); + } + private async getTemplate(id: string) { let template: any = null; switch (id.toLowerCase()) { diff --git a/src/program.ts b/src/program.ts index 8093c36271..15dfef365b 100644 --- a/src/program.ts +++ b/src/program.ts @@ -180,7 +180,7 @@ export class Program { .command('update') .description('Check for updates.') .action(async (object, id, cmd) => { - console.log('Checking...'); + // TODO }); program