mirror of
https://github.com/bitwarden/browser.git
synced 2024-09-14 02:08:50 +02:00
cli response objects
This commit is contained in:
parent
3dd38cbe12
commit
4f3f84539d
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@ -12,7 +12,9 @@
|
|||||||
"-r",
|
"-r",
|
||||||
"tsconfig-paths/register",
|
"tsconfig-paths/register",
|
||||||
"${workspaceFolder}/src/main.ts",
|
"${workspaceFolder}/src/main.ts",
|
||||||
"sync"
|
"get",
|
||||||
|
"item",
|
||||||
|
"f6709d4f-69c3-4957-93e4-a82d01259693"
|
||||||
],
|
],
|
||||||
"env": {
|
"env": {
|
||||||
"NODE_ENV": "debug"
|
"NODE_ENV": "debug"
|
||||||
|
@ -7,87 +7,85 @@ import { CollectionService } from 'jslib/abstractions/collection.service';
|
|||||||
import { FolderService } from 'jslib/abstractions/folder.service';
|
import { FolderService } from 'jslib/abstractions/folder.service';
|
||||||
import { TotpService } from 'jslib/abstractions/totp.service';
|
import { TotpService } from 'jslib/abstractions/totp.service';
|
||||||
|
|
||||||
|
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';
|
||||||
|
|
||||||
export class GetCommand {
|
export class GetCommand {
|
||||||
constructor(private cipherService: CipherService, private folderService: FolderService,
|
constructor(private cipherService: CipherService, private folderService: FolderService,
|
||||||
private collectionService: CollectionService, private totpService: TotpService) { }
|
private collectionService: CollectionService, private totpService: TotpService) { }
|
||||||
|
|
||||||
async run(object: string, id: string, cmd: program.Command) {
|
async run(object: string, id: string, cmd: program.Command): Promise<Response> {
|
||||||
switch (object) {
|
switch (object) {
|
||||||
case 'item':
|
case 'item':
|
||||||
await this.getCipher(id);
|
return await this.getCipher(id);
|
||||||
break;
|
|
||||||
case 'totp':
|
case 'totp':
|
||||||
await this.getTotp(id);
|
return await this.getTotp(id);
|
||||||
break;
|
|
||||||
case 'folder':
|
case 'folder':
|
||||||
await this.getFolder(id);
|
return await this.getFolder(id);
|
||||||
break;
|
|
||||||
case 'collection':
|
case 'collection':
|
||||||
await this.getCollection(id);
|
return await this.getCollection(id);
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
console.log('Unknown object: ' + object);
|
return Response.badRequest('Unknown object.');
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getCipher(id: string) {
|
private async getCipher(id: string) {
|
||||||
const cipher = await this.cipherService.get(id);
|
const cipher = await this.cipherService.get(id);
|
||||||
if (cipher == null) {
|
if (cipher == null) {
|
||||||
console.log('Not found.');
|
return Response.notFound();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const decCipher = await cipher.decrypt();
|
const decCipher = await cipher.decrypt();
|
||||||
console.log(JSON.stringify(decCipher));
|
const res = new CipherResponse(decCipher);
|
||||||
|
return Response.success(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getTotp(id: string) {
|
private async getTotp(id: string) {
|
||||||
const cipher = await this.cipherService.get(id);
|
const cipher = await this.cipherService.get(id);
|
||||||
if (cipher == null) {
|
if (cipher == null) {
|
||||||
console.log('Not found.');
|
return Response.notFound();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cipher.type !== CipherType.Login) {
|
if (cipher.type !== CipherType.Login) {
|
||||||
console.log('Not a login.');
|
return Response.badRequest('Not a login.');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const decCipher = await cipher.decrypt();
|
const decCipher = await cipher.decrypt();
|
||||||
if (decCipher.login.totp == null || decCipher.login.totp === '') {
|
if (decCipher.login.totp == null || decCipher.login.totp === '') {
|
||||||
console.log('No TOTP available.');
|
return Response.error('No TOTP available for this login.');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const totp = await this.totpService.getCode(decCipher.login.totp);
|
const totp = await this.totpService.getCode(decCipher.login.totp);
|
||||||
if (totp == null) {
|
if (totp == null) {
|
||||||
console.log('Couldn\'t generate TOTP code.');
|
return Response.error('Couldn\'t generate TOTP code.');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(JSON.stringify(totp));
|
const res = new StringResponse(totp);
|
||||||
|
return Response.success(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getFolder(id: string) {
|
private async getFolder(id: string) {
|
||||||
const folder = await this.folderService.get(id);
|
const folder = await this.folderService.get(id);
|
||||||
if (folder == null) {
|
if (folder == null) {
|
||||||
console.log('Not found.');
|
return Response.notFound();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const decFolder = await folder.decrypt();
|
const decFolder = await folder.decrypt();
|
||||||
console.log(JSON.stringify(decFolder));
|
const res = new FolderResponse(decFolder);
|
||||||
|
return Response.success(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getCollection(id: string) {
|
private async getCollection(id: string) {
|
||||||
const collection = await this.collectionService.get(id);
|
const collection = await this.collectionService.get(id);
|
||||||
if (collection == null) {
|
if (collection == null) {
|
||||||
console.log('Not found.');
|
return Response.notFound();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const decCollection = await collection.decrypt();
|
const decCollection = await collection.decrypt();
|
||||||
console.log(JSON.stringify(decCollection));
|
const res = new CollectionResponse(decCollection);
|
||||||
|
return Response.success(res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,39 +4,44 @@ import { CipherService } from 'jslib/abstractions/cipher.service';
|
|||||||
import { CollectionService } from 'jslib/services/collection.service';
|
import { CollectionService } from 'jslib/services/collection.service';
|
||||||
import { FolderService } from 'jslib/services/folder.service';
|
import { FolderService } from 'jslib/services/folder.service';
|
||||||
|
|
||||||
|
import { Response } from '../models/response';
|
||||||
|
import { CipherResponse } from '../models/response/cipherResponse';
|
||||||
|
import { CollectionResponse } from '../models/response/collectionResponse';
|
||||||
|
import { FolderResponse } from '../models/response/folderResponse';
|
||||||
|
import { ListResponse } from '../models/response/listResponse';
|
||||||
|
|
||||||
export class ListCommand {
|
export class ListCommand {
|
||||||
constructor(private cipherService: CipherService, private folderService: FolderService,
|
constructor(private cipherService: CipherService, private folderService: FolderService,
|
||||||
private collectionService: CollectionService) { }
|
private collectionService: CollectionService) { }
|
||||||
|
|
||||||
async run(object: string, cmd: program.Command) {
|
async run(object: string, cmd: program.Command): Promise<Response> {
|
||||||
switch (object) {
|
switch (object) {
|
||||||
case 'items':
|
case 'items':
|
||||||
await this.listCiphers();
|
return await this.listCiphers();
|
||||||
break;
|
|
||||||
case 'folders':
|
case 'folders':
|
||||||
await this.listFolders();
|
return await this.listFolders();
|
||||||
break;
|
|
||||||
case 'collections':
|
case 'collections':
|
||||||
await this.listCollections();
|
return await this.listCollections();
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
console.log('Unknown object: ' + object);
|
return Response.badRequest('Unknown object.');
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async listCiphers() {
|
private async listCiphers() {
|
||||||
const ciphers = await this.cipherService.getAllDecrypted();
|
const ciphers = await this.cipherService.getAllDecrypted();
|
||||||
console.log(JSON.stringify(ciphers));
|
const res = new ListResponse(ciphers.map((o) => new CipherResponse(o)));
|
||||||
|
return Response.success(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async listFolders() {
|
private async listFolders() {
|
||||||
const folders = await this.folderService.getAllDecrypted();
|
const folders = await this.folderService.getAllDecrypted();
|
||||||
console.log(JSON.stringify(folders));
|
const res = new ListResponse(folders.map((o) => new FolderResponse(o)));
|
||||||
|
return Response.success(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async listCollections() {
|
private async listCollections() {
|
||||||
const collections = await this.collectionService.getAllDecrypted();
|
const collections = await this.collectionService.getAllDecrypted();
|
||||||
console.log(JSON.stringify(collections));
|
const res = new ListResponse(collections.map((o) => new CollectionResponse(o)));
|
||||||
|
return Response.success(res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,17 +4,18 @@ import { AuthResult } from 'jslib/models/domain/authResult';
|
|||||||
|
|
||||||
import { AuthService } from 'jslib/abstractions/auth.service';
|
import { AuthService } from 'jslib/abstractions/auth.service';
|
||||||
|
|
||||||
export class LoginCommand {
|
import { Response } from '../models/response';
|
||||||
constructor(private authService: AuthService) {
|
|
||||||
|
|
||||||
}
|
export class LoginCommand {
|
||||||
|
constructor(private authService: AuthService) { }
|
||||||
|
|
||||||
async run(email: string, password: string, cmd: program.Command) {
|
async run(email: string, password: string, cmd: program.Command) {
|
||||||
try {
|
try {
|
||||||
const result = await this.authService.logIn(email, password);
|
const result = await this.authService.logIn(email, password);
|
||||||
console.log(result);
|
// TODO: 2FA
|
||||||
|
return Response.success();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
return Response.success(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,15 +2,17 @@ import * as program from 'commander';
|
|||||||
|
|
||||||
import { SyncService } from 'jslib/abstractions/sync.service';
|
import { SyncService } from 'jslib/abstractions/sync.service';
|
||||||
|
|
||||||
|
import { Response } from '../models/response';
|
||||||
|
|
||||||
export class SyncCommand {
|
export class SyncCommand {
|
||||||
constructor(private syncService: SyncService) { }
|
constructor(private syncService: SyncService) { }
|
||||||
|
|
||||||
async run(cmd: program.Command) {
|
async run(cmd: program.Command): Promise<Response> {
|
||||||
try {
|
try {
|
||||||
const result = await this.syncService.fullSync(false);
|
const result = await this.syncService.fullSync(false);
|
||||||
console.log(result);
|
return Response.success();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
return Response.success(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
30
src/models/response.ts
Normal file
30
src/models/response.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { BaseResponse } from './response/baseResponse';
|
||||||
|
|
||||||
|
export class Response {
|
||||||
|
static error(message: string): Response {
|
||||||
|
var res = new Response();
|
||||||
|
res.success = false;
|
||||||
|
res.message = message;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static notFound(): Response {
|
||||||
|
return Response.error('Not found.');
|
||||||
|
}
|
||||||
|
|
||||||
|
static badRequest(message: string): Response {
|
||||||
|
return Response.error(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
static success(data?: BaseResponse): Response {
|
||||||
|
var res = new Response();
|
||||||
|
res.success = true;
|
||||||
|
res.data = data;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
success: boolean;
|
||||||
|
message: string;
|
||||||
|
errorCode: number;
|
||||||
|
data: BaseResponse;
|
||||||
|
}
|
9
src/models/response/baseResponse.ts
Normal file
9
src/models/response/baseResponse.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
export abstract class BaseResponse {
|
||||||
|
object: string;
|
||||||
|
|
||||||
|
constructor(object?: string) {
|
||||||
|
if (object != null) {
|
||||||
|
this.object = object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
src/models/response/cipherResponse.ts
Normal file
22
src/models/response/cipherResponse.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { CipherView } from 'jslib/models/view/cipherView';
|
||||||
|
|
||||||
|
import { BaseResponse } from './baseResponse';
|
||||||
|
|
||||||
|
import { CipherType } from 'jslib/enums';
|
||||||
|
|
||||||
|
export class CipherResponse extends BaseResponse {
|
||||||
|
id: string;
|
||||||
|
organizationId: string;
|
||||||
|
type: CipherType;
|
||||||
|
name: string;
|
||||||
|
notes: string;
|
||||||
|
|
||||||
|
constructor(o: CipherView) {
|
||||||
|
super('item');
|
||||||
|
this.id = o.id;
|
||||||
|
this.organizationId = o.organizationId;
|
||||||
|
this.type = o.type;
|
||||||
|
this.name = o.name;
|
||||||
|
this.notes = o.notes;
|
||||||
|
}
|
||||||
|
}
|
16
src/models/response/collectionResponse.ts
Normal file
16
src/models/response/collectionResponse.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { CollectionView } from 'jslib/models/view/collectionView';
|
||||||
|
|
||||||
|
import { BaseResponse } from './baseResponse';
|
||||||
|
|
||||||
|
export class CollectionResponse extends BaseResponse {
|
||||||
|
id: string;
|
||||||
|
organizationId: string;
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
constructor(o: CollectionView) {
|
||||||
|
super('collection');
|
||||||
|
this.id = o.id;
|
||||||
|
this.organizationId = o.organizationId;
|
||||||
|
this.name = o.name;
|
||||||
|
}
|
||||||
|
}
|
14
src/models/response/folderResponse.ts
Normal file
14
src/models/response/folderResponse.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { FolderView } from 'jslib/models/view/folderView';
|
||||||
|
|
||||||
|
import { BaseResponse } from './baseResponse';
|
||||||
|
|
||||||
|
export class FolderResponse extends BaseResponse {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
constructor(o: FolderView) {
|
||||||
|
super('folder');
|
||||||
|
this.id = o.id;
|
||||||
|
this.name = o.name;
|
||||||
|
}
|
||||||
|
}
|
10
src/models/response/listResponse.ts
Normal file
10
src/models/response/listResponse.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { BaseResponse } from './baseResponse';
|
||||||
|
|
||||||
|
export class ListResponse extends BaseResponse {
|
||||||
|
data: BaseResponse[];
|
||||||
|
|
||||||
|
constructor(data: BaseResponse[]) {
|
||||||
|
super('list');
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
}
|
14
src/models/response/stringResponse.ts
Normal file
14
src/models/response/stringResponse.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { CipherView } from 'jslib/models/view/cipherView';
|
||||||
|
|
||||||
|
import { BaseResponse } from './baseResponse';
|
||||||
|
|
||||||
|
import { CipherType } from 'jslib/enums';
|
||||||
|
|
||||||
|
export class StringResponse extends BaseResponse {
|
||||||
|
data: string;
|
||||||
|
|
||||||
|
constructor(data: string) {
|
||||||
|
super('string');
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,10 @@ import { GetCommand } from './commands/get.command';
|
|||||||
|
|
||||||
import { Main } from './main';
|
import { Main } from './main';
|
||||||
|
|
||||||
|
import { Response } from './models/response';
|
||||||
|
import { ListResponse } from './models/response/listResponse';
|
||||||
|
import { StringResponse } from './models/response/stringResponse';
|
||||||
|
|
||||||
export class Program {
|
export class Program {
|
||||||
constructor(private main: Main) { }
|
constructor(private main: Main) { }
|
||||||
|
|
||||||
@ -21,8 +25,8 @@ export class Program {
|
|||||||
.option('-c, --code <code>', '2FA code.')
|
.option('-c, --code <code>', '2FA code.')
|
||||||
.action(async (email: string, password: string, cmd: program.Command) => {
|
.action(async (email: string, password: string, cmd: program.Command) => {
|
||||||
const command = new LoginCommand(this.main.authService);
|
const command = new LoginCommand(this.main.authService);
|
||||||
await command.run(email, password, cmd);
|
const response = await command.run(email, password, cmd);
|
||||||
process.exit();
|
this.processResponse(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
@ -39,8 +43,8 @@ export class Program {
|
|||||||
.option('-f, --force', 'Force a full sync.')
|
.option('-f, --force', 'Force a full sync.')
|
||||||
.action(async (cmd) => {
|
.action(async (cmd) => {
|
||||||
const command = new SyncCommand(this.main.syncService);
|
const command = new SyncCommand(this.main.syncService);
|
||||||
await command.run(cmd);
|
const response = await command.run(cmd);
|
||||||
process.exit();
|
this.processResponse(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
@ -49,8 +53,8 @@ export class Program {
|
|||||||
.action(async (object, cmd) => {
|
.action(async (object, cmd) => {
|
||||||
const command = new ListCommand(this.main.cipherService, this.main.folderService,
|
const command = new ListCommand(this.main.cipherService, this.main.folderService,
|
||||||
this.main.collectionService);
|
this.main.collectionService);
|
||||||
await command.run(object, cmd);
|
const response = await command.run(object, cmd);
|
||||||
process.exit();
|
this.processResponse(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
@ -59,8 +63,8 @@ export class Program {
|
|||||||
.action(async (object, id, cmd) => {
|
.action(async (object, id, cmd) => {
|
||||||
const command = new GetCommand(this.main.cipherService, this.main.folderService,
|
const command = new GetCommand(this.main.cipherService, this.main.folderService,
|
||||||
this.main.collectionService, this.main.totpService);
|
this.main.collectionService, this.main.totpService);
|
||||||
await command.run(object, id, cmd);
|
const response = await command.run(object, id, cmd);
|
||||||
process.exit();
|
this.processResponse(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
@ -84,4 +88,22 @@ export class Program {
|
|||||||
program
|
program
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private processResponse(response: Response) {
|
||||||
|
if (response.success) {
|
||||||
|
if (response.data != null) {
|
||||||
|
if (response.data.object === 'string') {
|
||||||
|
console.log((response.data as StringResponse).data);
|
||||||
|
} else if (response.data.object === 'list') {
|
||||||
|
console.log(JSON.stringify((response.data as ListResponse).data));
|
||||||
|
} else {
|
||||||
|
console.log(JSON.stringify(response.data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
process.exit();
|
||||||
|
} else {
|
||||||
|
console.log(response.message);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user