2018-05-14 19:37:52 +02:00
|
|
|
import * as program from 'commander';
|
|
|
|
|
|
|
|
import { CipherType } from 'jslib/enums/cipherType';
|
|
|
|
|
|
|
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
|
|
|
import { CollectionService } from 'jslib/abstractions/collection.service';
|
|
|
|
import { FolderService } from 'jslib/abstractions/folder.service';
|
|
|
|
import { TotpService } from 'jslib/abstractions/totp.service';
|
|
|
|
|
2018-05-16 17:54:59 +02:00
|
|
|
import { CipherView } from 'jslib/models/view/cipherView';
|
|
|
|
import { CollectionView } from 'jslib/models/view/collectionView';
|
|
|
|
import { FolderView } from 'jslib/models/view/folderView';
|
|
|
|
|
2018-05-14 20:54:19 +02:00
|
|
|
import { Response } from '../models/response';
|
|
|
|
import { CipherResponse } from '../models/response/cipherResponse';
|
|
|
|
import { CollectionResponse } from '../models/response/collectionResponse';
|
|
|
|
import { FolderResponse } from '../models/response/folderResponse';
|
|
|
|
import { StringResponse } from '../models/response/stringResponse';
|
2018-05-14 22:25:14 +02:00
|
|
|
import { TemplateResponse } from '../models/response/templateResponse';
|
|
|
|
|
2018-05-15 18:18:47 +02:00
|
|
|
import { Card } from '../models/card';
|
|
|
|
import { Cipher } from '../models/cipher';
|
2018-05-15 18:53:08 +02:00
|
|
|
import { Collection } from '../models/collection';
|
2018-05-15 18:18:47 +02:00
|
|
|
import { Field } from '../models/field';
|
2018-05-15 18:53:08 +02:00
|
|
|
import { Folder } from '../models/folder';
|
2018-05-15 18:18:47 +02:00
|
|
|
import { Identity } from '../models/identity';
|
|
|
|
import { Login } from '../models/login';
|
|
|
|
import { LoginUri } from '../models/loginUri';
|
|
|
|
import { SecureNote } from '../models/secureNote';
|
2018-05-14 20:54:19 +02:00
|
|
|
|
2018-05-16 17:54:59 +02:00
|
|
|
import { CliUtils } from '../utils';
|
|
|
|
|
2018-05-14 19:37:52 +02:00
|
|
|
export class GetCommand {
|
|
|
|
constructor(private cipherService: CipherService, private folderService: FolderService,
|
2018-05-16 19:53:12 +02:00
|
|
|
private collectionService: CollectionService, private totpService: TotpService) { }
|
2018-05-14 19:37:52 +02:00
|
|
|
|
2018-05-14 20:54:19 +02:00
|
|
|
async run(object: string, id: string, cmd: program.Command): Promise<Response> {
|
2018-05-16 17:54:59 +02:00
|
|
|
if (id != null) {
|
|
|
|
id = id.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:25:14 +02:00
|
|
|
switch (object.toLowerCase()) {
|
2018-05-14 19:37:52 +02:00
|
|
|
case 'item':
|
2018-05-14 20:54:19 +02:00
|
|
|
return await this.getCipher(id);
|
2018-05-14 19:37:52 +02:00
|
|
|
case 'totp':
|
2018-05-14 20:54:19 +02:00
|
|
|
return await this.getTotp(id);
|
2018-05-14 19:37:52 +02:00
|
|
|
case 'folder':
|
2018-05-14 20:54:19 +02:00
|
|
|
return await this.getFolder(id);
|
2018-05-14 19:37:52 +02:00
|
|
|
case 'collection':
|
2018-05-14 20:54:19 +02:00
|
|
|
return await this.getCollection(id);
|
2018-05-14 22:25:14 +02:00
|
|
|
case 'template':
|
|
|
|
return await this.getTemplate(id);
|
2018-05-14 19:37:52 +02:00
|
|
|
default:
|
2018-05-14 20:54:19 +02:00
|
|
|
return Response.badRequest('Unknown object.');
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getCipher(id: string) {
|
2018-05-16 17:54:59 +02:00
|
|
|
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) {
|
2018-05-16 18:00:40 +02:00
|
|
|
return Response.multipleResults(ciphers.map((c) => c.id));
|
2018-05-16 17:54:59 +02:00
|
|
|
}
|
|
|
|
if (ciphers.length > 0) {
|
|
|
|
decCipher = ciphers[0];
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 17:54:59 +02:00
|
|
|
if (decCipher == null) {
|
|
|
|
return Response.notFound();
|
|
|
|
}
|
2018-05-14 20:54:19 +02:00
|
|
|
const res = new CipherResponse(decCipher);
|
|
|
|
return Response.success(res);
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async getTotp(id: string) {
|
2018-05-16 17:54:59 +02:00
|
|
|
const cipherResponse = await this.getCipher(id);
|
|
|
|
if (!cipherResponse.success) {
|
|
|
|
return cipherResponse;
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 17:54:59 +02:00
|
|
|
const cipher = cipherResponse.data as CipherResponse;
|
2018-05-14 19:37:52 +02:00
|
|
|
if (cipher.type !== CipherType.Login) {
|
2018-05-14 20:54:19 +02:00
|
|
|
return Response.badRequest('Not a login.');
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 17:54:59 +02:00
|
|
|
if (cipher.login.totp == null || cipher.login.totp === '') {
|
2018-05-14 20:54:19 +02:00
|
|
|
return Response.error('No TOTP available for this login.');
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 17:54:59 +02:00
|
|
|
const totp = await this.totpService.getCode(cipher.login.totp);
|
2018-05-14 19:37:52 +02:00
|
|
|
if (totp == null) {
|
2018-05-14 20:54:19 +02:00
|
|
|
return Response.error('Couldn\'t generate TOTP code.');
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:54:19 +02:00
|
|
|
const res = new StringResponse(totp);
|
|
|
|
return Response.success(res);
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async getFolder(id: string) {
|
2018-05-16 17:54:59 +02:00
|
|
|
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) {
|
2018-05-16 18:00:40 +02:00
|
|
|
return Response.multipleResults(folders.map((f) => f.id));
|
2018-05-16 17:54:59 +02:00
|
|
|
}
|
|
|
|
if (folders.length > 0) {
|
|
|
|
decFolder = folders[0];
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 17:54:59 +02:00
|
|
|
if (decFolder == null) {
|
|
|
|
return Response.notFound();
|
|
|
|
}
|
2018-05-14 20:54:19 +02:00
|
|
|
const res = new FolderResponse(decFolder);
|
|
|
|
return Response.success(res);
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async getCollection(id: string) {
|
2018-05-16 17:54:59 +02:00
|
|
|
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) {
|
2018-05-16 18:00:40 +02:00
|
|
|
return Response.multipleResults(collections.map((c) => c.id));
|
2018-05-16 17:54:59 +02:00
|
|
|
}
|
|
|
|
if (collections.length > 0) {
|
|
|
|
decCollection = collections[0];
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 17:54:59 +02:00
|
|
|
if (decCollection == null) {
|
|
|
|
return Response.notFound();
|
|
|
|
}
|
2018-05-14 20:54:19 +02:00
|
|
|
const res = new CollectionResponse(decCollection);
|
|
|
|
return Response.success(res);
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|
2018-05-14 22:25:14 +02:00
|
|
|
|
2018-05-16 17:54:59 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:25:14 +02:00
|
|
|
private async getTemplate(id: string) {
|
|
|
|
let template: any = null;
|
|
|
|
switch (id.toLowerCase()) {
|
|
|
|
case 'item':
|
2018-05-15 18:18:47 +02:00
|
|
|
template = Cipher.template();
|
2018-05-14 22:25:14 +02:00
|
|
|
break;
|
|
|
|
case 'field':
|
2018-05-15 18:18:47 +02:00
|
|
|
template = Field.template();
|
2018-05-14 22:25:14 +02:00
|
|
|
break;
|
|
|
|
case 'login':
|
2018-05-15 18:18:47 +02:00
|
|
|
template = Login.template();
|
2018-05-14 22:25:14 +02:00
|
|
|
break;
|
|
|
|
case 'loginuri':
|
2018-05-15 18:18:47 +02:00
|
|
|
template = LoginUri.template();
|
2018-05-14 22:25:14 +02:00
|
|
|
break;
|
|
|
|
case 'card':
|
2018-05-15 18:18:47 +02:00
|
|
|
template = Card.template();
|
2018-05-14 22:25:14 +02:00
|
|
|
break;
|
|
|
|
case 'identity':
|
2018-05-15 18:18:47 +02:00
|
|
|
template = Identity.template();
|
2018-05-14 22:25:14 +02:00
|
|
|
break;
|
|
|
|
case 'securenote':
|
2018-05-15 18:18:47 +02:00
|
|
|
template = SecureNote.template();
|
2018-05-14 22:25:14 +02:00
|
|
|
break;
|
2018-05-15 18:53:08 +02:00
|
|
|
case 'folder':
|
|
|
|
template = Folder.template();
|
|
|
|
break;
|
|
|
|
case 'collection':
|
|
|
|
template = Collection.template();
|
|
|
|
break;
|
2018-05-14 22:25:14 +02:00
|
|
|
default:
|
2018-05-14 23:13:57 +02:00
|
|
|
return Response.badRequest('Unknown template object.');
|
2018-05-14 22:25:14 +02:00
|
|
|
}
|
2018-05-15 05:40:11 +02:00
|
|
|
|
2018-05-14 22:25:14 +02:00
|
|
|
const res = new TemplateResponse(template);
|
|
|
|
return Response.success(res);
|
|
|
|
}
|
2018-05-14 19:37:52 +02:00
|
|
|
}
|