mirror of
https://github.com/bitwarden/browser
synced 2025-12-19 01:33:33 +00:00
cli response objects
This commit is contained in:
@@ -7,87 +7,85 @@ import { CollectionService } from 'jslib/abstractions/collection.service';
|
||||
import { FolderService } from 'jslib/abstractions/folder.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 {
|
||||
constructor(private cipherService: CipherService, private folderService: FolderService,
|
||||
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) {
|
||||
case 'item':
|
||||
await this.getCipher(id);
|
||||
break;
|
||||
return await this.getCipher(id);
|
||||
case 'totp':
|
||||
await this.getTotp(id);
|
||||
break;
|
||||
return await this.getTotp(id);
|
||||
case 'folder':
|
||||
await this.getFolder(id);
|
||||
break;
|
||||
return await this.getFolder(id);
|
||||
case 'collection':
|
||||
await this.getCollection(id);
|
||||
break;
|
||||
return await this.getCollection(id);
|
||||
default:
|
||||
console.log('Unknown object: ' + object);
|
||||
break;
|
||||
return Response.badRequest('Unknown object.');
|
||||
}
|
||||
}
|
||||
|
||||
private async getCipher(id: string) {
|
||||
const cipher = await this.cipherService.get(id);
|
||||
if (cipher == null) {
|
||||
console.log('Not found.');
|
||||
return;
|
||||
return Response.notFound();
|
||||
}
|
||||
|
||||
const decCipher = await cipher.decrypt();
|
||||
console.log(JSON.stringify(decCipher));
|
||||
const res = new CipherResponse(decCipher);
|
||||
return Response.success(res);
|
||||
}
|
||||
|
||||
private async getTotp(id: string) {
|
||||
const cipher = await this.cipherService.get(id);
|
||||
if (cipher == null) {
|
||||
console.log('Not found.');
|
||||
return;
|
||||
return Response.notFound();
|
||||
}
|
||||
|
||||
if (cipher.type !== CipherType.Login) {
|
||||
console.log('Not a login.');
|
||||
return;
|
||||
return Response.badRequest('Not a login.');
|
||||
}
|
||||
|
||||
const decCipher = await cipher.decrypt();
|
||||
if (decCipher.login.totp == null || decCipher.login.totp === '') {
|
||||
console.log('No TOTP available.');
|
||||
return;
|
||||
return Response.error('No TOTP available for this login.');
|
||||
}
|
||||
|
||||
const totp = await this.totpService.getCode(decCipher.login.totp);
|
||||
if (totp == null) {
|
||||
console.log('Couldn\'t generate TOTP code.');
|
||||
return;
|
||||
return Response.error('Couldn\'t generate TOTP code.');
|
||||
}
|
||||
|
||||
console.log(JSON.stringify(totp));
|
||||
const res = new StringResponse(totp);
|
||||
return Response.success(res);
|
||||
}
|
||||
|
||||
private async getFolder(id: string) {
|
||||
const folder = await this.folderService.get(id);
|
||||
if (folder == null) {
|
||||
console.log('Not found.');
|
||||
return;
|
||||
return Response.notFound();
|
||||
}
|
||||
|
||||
const decFolder = await folder.decrypt();
|
||||
console.log(JSON.stringify(decFolder));
|
||||
const res = new FolderResponse(decFolder);
|
||||
return Response.success(res);
|
||||
}
|
||||
|
||||
private async getCollection(id: string) {
|
||||
const collection = await this.collectionService.get(id);
|
||||
if (collection == null) {
|
||||
console.log('Not found.');
|
||||
return;
|
||||
return Response.notFound();
|
||||
}
|
||||
|
||||
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 { 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 {
|
||||
constructor(private cipherService: CipherService, private folderService: FolderService,
|
||||
private collectionService: CollectionService) { }
|
||||
|
||||
async run(object: string, cmd: program.Command) {
|
||||
async run(object: string, cmd: program.Command): Promise<Response> {
|
||||
switch (object) {
|
||||
case 'items':
|
||||
await this.listCiphers();
|
||||
break;
|
||||
return await this.listCiphers();
|
||||
case 'folders':
|
||||
await this.listFolders();
|
||||
break;
|
||||
return await this.listFolders();
|
||||
case 'collections':
|
||||
await this.listCollections();
|
||||
break;
|
||||
return await this.listCollections();
|
||||
default:
|
||||
console.log('Unknown object: ' + object);
|
||||
break;
|
||||
return Response.badRequest('Unknown object.');
|
||||
}
|
||||
}
|
||||
|
||||
private async listCiphers() {
|
||||
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() {
|
||||
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() {
|
||||
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';
|
||||
|
||||
export class LoginCommand {
|
||||
constructor(private authService: AuthService) {
|
||||
import { Response } from '../models/response';
|
||||
|
||||
}
|
||||
export class LoginCommand {
|
||||
constructor(private authService: AuthService) { }
|
||||
|
||||
async run(email: string, password: string, cmd: program.Command) {
|
||||
try {
|
||||
const result = await this.authService.logIn(email, password);
|
||||
console.log(result);
|
||||
// TODO: 2FA
|
||||
return Response.success();
|
||||
} 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 { Response } from '../models/response';
|
||||
|
||||
export class SyncCommand {
|
||||
constructor(private syncService: SyncService) { }
|
||||
|
||||
async run(cmd: program.Command) {
|
||||
async run(cmd: program.Command): Promise<Response> {
|
||||
try {
|
||||
const result = await this.syncService.fullSync(false);
|
||||
console.log(result);
|
||||
return Response.success();
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return Response.success(e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user