mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-24 16:49:26 +01:00
sub out create command
This commit is contained in:
parent
428f46af06
commit
b5507813ab
46
src/commands/create.command.ts
Normal file
46
src/commands/create.command.ts
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import * as program from 'commander';
|
||||||
|
|
||||||
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
||||||
|
import { FolderService } from 'jslib/services/folder.service';
|
||||||
|
|
||||||
|
import { Response } from '../models/response';
|
||||||
|
|
||||||
|
import { CipherRequest } from '../models/request/cipherRequest';
|
||||||
|
import { FolderRequest } from '../models/request/folderRequest';
|
||||||
|
|
||||||
|
export class CreateCommand {
|
||||||
|
constructor(private cipherService: CipherService, private folderService: FolderService) { }
|
||||||
|
|
||||||
|
async run(object: string, requestData: string, cmd: program.Command): Promise<Response> {
|
||||||
|
const reqJson = new Buffer(requestData, 'base64').toString();
|
||||||
|
const req = JSON.parse(reqJson);
|
||||||
|
switch (object.toLowerCase()) {
|
||||||
|
case 'item':
|
||||||
|
return await this.createCipher(req);
|
||||||
|
case 'folder':
|
||||||
|
return await this.createFolder(req);
|
||||||
|
default:
|
||||||
|
return Response.badRequest('Unknown object.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async createCipher(req: CipherRequest) {
|
||||||
|
const cipher = await this.cipherService.encrypt(CipherRequest.toView(req));
|
||||||
|
try {
|
||||||
|
await this.cipherService.saveWithServer(cipher);
|
||||||
|
return Response.success();
|
||||||
|
} catch (e) {
|
||||||
|
return Response.error(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async createFolder(req: FolderRequest) {
|
||||||
|
const folder = await this.folderService.encrypt(FolderRequest.toView(req));
|
||||||
|
try {
|
||||||
|
await this.folderService.saveWithServer(folder);
|
||||||
|
return Response.success();
|
||||||
|
} catch (e) {
|
||||||
|
return Response.error(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,13 @@
|
|||||||
import { CipherType } from 'jslib/enums/cipherType';
|
import { CipherType } from 'jslib/enums/cipherType';
|
||||||
|
|
||||||
import { LoginRequest } from './loginRequest';
|
import { LoginRequest } from './loginRequest';
|
||||||
import { SecureNoteRequest } from './secureNoteRequest';
|
import { SecureNoteRequest } from './secureNoteRequest';
|
||||||
import { CardRequest } from './cardRequest';
|
import { CardRequest } from './cardRequest';
|
||||||
import { IdentityRequest } from './identityRequest';
|
import { IdentityRequest } from './identityRequest';
|
||||||
import { FieldRequest } from './fieldRequest';
|
import { FieldRequest } from './fieldRequest';
|
||||||
|
|
||||||
|
import { CipherView } from 'jslib/models/view/cipherView';
|
||||||
|
|
||||||
export class CipherRequest {
|
export class CipherRequest {
|
||||||
static template(): CipherRequest {
|
static template(): CipherRequest {
|
||||||
var req = new CipherRequest();
|
var req = new CipherRequest();
|
||||||
@ -22,6 +25,12 @@ export class CipherRequest {
|
|||||||
return req;
|
return req;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static toView(req: CipherRequest) {
|
||||||
|
const view = new CipherView();
|
||||||
|
view.name = req.name;
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
type: CipherType;
|
type: CipherType;
|
||||||
folderId: string;
|
folderId: string;
|
||||||
organizationId: string;
|
organizationId: string;
|
||||||
|
@ -1,3 +1,11 @@
|
|||||||
|
import { FolderView } from 'jslib/models/view/folderView';
|
||||||
|
|
||||||
export class FolderRequest {
|
export class FolderRequest {
|
||||||
|
static toView(req: FolderRequest) {
|
||||||
|
const view = new FolderView();
|
||||||
|
view.name = req.name;
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,11 @@ import { SyncCommand } from './commands/sync.command';
|
|||||||
import { Main } from './main';
|
import { Main } from './main';
|
||||||
|
|
||||||
import { Response } from './models/response';
|
import { Response } from './models/response';
|
||||||
|
import { CreateCommand } from './commands/create.command';
|
||||||
|
import { EncodeCommand } from './commands/encode.command';
|
||||||
import { ListResponse } from './models/response/listResponse';
|
import { ListResponse } from './models/response/listResponse';
|
||||||
import { StringResponse } from './models/response/stringResponse';
|
import { StringResponse } from './models/response/stringResponse';
|
||||||
import { TemplateResponse } from './models/response/templateResponse';
|
import { TemplateResponse } from './models/response/templateResponse';
|
||||||
import { EncodeCommand } from './commands/encode.command';
|
|
||||||
|
|
||||||
export class Program {
|
export class Program {
|
||||||
constructor(private main: Main) { }
|
constructor(private main: Main) { }
|
||||||
@ -70,6 +71,15 @@ export class Program {
|
|||||||
this.processResponse(response);
|
this.processResponse(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
program
|
||||||
|
.command('create <object> <encodedData>')
|
||||||
|
.description('Create an object.')
|
||||||
|
.action(async (object, encodedData, cmd) => {
|
||||||
|
const command = new CreateCommand(this.main.cipherService, this.main.folderService);
|
||||||
|
const response = await command.run(object, encodedData, cmd);
|
||||||
|
this.processResponse(response);
|
||||||
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
.command('edit <object> <id>')
|
.command('edit <object> <id>')
|
||||||
.description('Edit an object.')
|
.description('Edit an object.')
|
||||||
|
Loading…
Reference in New Issue
Block a user