1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-10 06:08:34 +02:00
bitwarden-browser/src/commands/edit.command.ts

67 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-05-15 17:30:56 +02:00
import * as program from 'commander';
import { CipherService } from 'jslib/abstractions/cipher.service';
import { FolderService } from 'jslib/services/folder.service';
import { Response } from '../models/response';
2018-05-15 18:18:47 +02:00
import { Cipher } from '../models/cipher';
import { Folder } from '../models/folder';
2018-05-15 17:30:56 +02:00
export class EditCommand {
constructor(private cipherService: CipherService, private folderService: FolderService) { }
async run(object: string, id: string, requestData: string, cmd: program.Command): Promise<Response> {
let req: any = null;
try {
const reqJson = new Buffer(requestData, 'base64').toString();
req = JSON.parse(reqJson);
} catch (e) {
return Response.badRequest('Error parsing the encoded request data.');
}
switch (object.toLowerCase()) {
case 'item':
return await this.editCipher(id, req);
case 'folder':
return await this.editFolder(id, req);
default:
return Response.badRequest('Unknown object.');
}
}
2018-05-15 18:18:47 +02:00
private async editCipher(id: string, req: Cipher) {
2018-05-15 17:30:56 +02:00
const cipher = await this.cipherService.get(id);
if (cipher == null) {
return Response.notFound();
}
let cipherView = await cipher.decrypt();
2018-05-15 18:18:47 +02:00
cipherView = Cipher.toView(req, cipherView);
2018-05-15 17:30:56 +02:00
const encCipher = await this.cipherService.encrypt(cipherView);
try {
await this.cipherService.saveWithServer(encCipher);
return Response.success();
} catch (e) {
return Response.error(e);
}
}
2018-05-15 18:18:47 +02:00
private async editFolder(id: string, req: Folder) {
2018-05-15 17:30:56 +02:00
const folder = await this.folderService.get(id);
if (folder == null) {
return Response.notFound();
}
let folderView = await folder.decrypt();
2018-05-15 18:18:47 +02:00
folderView = Folder.toView(req, folderView);
2018-05-15 17:30:56 +02:00
const encFolder = await this.folderService.encrypt(folderView);
try {
await this.folderService.saveWithServer(encFolder);
return Response.success();
} catch (e) {
return Response.error(e);
}
}
}