From 99c4291ee7ff567c3149e0d1f31d427e3cf269e5 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 7 Oct 2019 10:42:02 -0400 Subject: [PATCH] confirm org members command --- src/commands/confirm.command.ts | 58 +++++++++++++++++++++++++++++++++ src/program.ts | 27 +++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/commands/confirm.command.ts diff --git a/src/commands/confirm.command.ts b/src/commands/confirm.command.ts new file mode 100644 index 0000000000..fbff835160 --- /dev/null +++ b/src/commands/confirm.command.ts @@ -0,0 +1,58 @@ +import * as program from 'commander'; + +import { ApiService } from 'jslib/abstractions/api.service'; +import { CryptoService } from 'jslib/abstractions/crypto.service'; + +import { OrganizationUserConfirmRequest } from 'jslib/models/request/organizationUserConfirmRequest'; + +import { Response } from 'jslib/cli/models/response'; + +import { Utils } from 'jslib/misc/utils'; + +export class ConfirmCommand { + constructor(private apiService: ApiService, private cryptoService: CryptoService) { } + + async run(object: string, id: string, cmd: program.Command): Promise { + if (id != null) { + id = id.toLowerCase(); + } + + switch (object.toLowerCase()) { + case 'org-member': + return await this.confirmOrganizationMember(id, cmd); + default: + return Response.badRequest('Unknown object.'); + } + } + + private async confirmOrganizationMember(id: string, cmd: program.Command) { + if (cmd.organizationid == null || cmd.organizationid === '') { + return Response.badRequest('--organizationid required.'); + } + if (!Utils.isGuid(id)) { + return Response.error('`' + id + '` is not a GUID.'); + } + if (!Utils.isGuid(cmd.organizationid)) { + return Response.error('`' + cmd.organizationid + '` is not a GUID.'); + } + try { + const orgKey = await this.cryptoService.getOrgKey(cmd.organizationid); + if (orgKey == null) { + throw new Error('No encryption key for this organization.'); + } + const orgUser = await this.apiService.getOrganizationUser(cmd.organizationid, id); + if (orgUser == null) { + throw new Error('Member id does not exist for this organization.'); + } + const publicKeyResponse = await this.apiService.getUserPublicKey(orgUser.userId); + const publicKey = Utils.fromB64ToArray(publicKeyResponse.publicKey); + const key = await this.cryptoService.rsaEncrypt(orgKey.key, publicKey.buffer); + const req = new OrganizationUserConfirmRequest(); + req.key = key.encryptedString; + await this.apiService.postOrganizationUserConfirm(cmd.organizationid, id, req); + return Response.success(); + } catch (e) { + return Response.error(e); + } + } +} diff --git a/src/program.ts b/src/program.ts index e068974c8a..f0cc16fd0b 100644 --- a/src/program.ts +++ b/src/program.ts @@ -4,6 +4,7 @@ import * as program from 'commander'; import { Main } from './bw'; import { ConfigCommand } from './commands/config.command'; +import { ConfirmCommand } from './commands/confirm.command'; import { CreateCommand } from './commands/create.command'; import { DeleteCommand } from './commands/delete.command'; import { EditCommand } from './commands/edit.command'; @@ -447,6 +448,32 @@ export class Program extends BaseProgram { this.processResponse(response); }); + program + .command('confirm ') + .option('--organizationid ', 'Organization id for an organization object.') + .description('Confirm an object to the organization.') + .on('--help', () => { + writeLn('\n Objects:'); + writeLn(''); + writeLn(' org-member'); + writeLn(''); + writeLn(' Id:'); + writeLn(''); + writeLn(' Object\'s globally unique `id`.'); + writeLn(''); + writeLn(' Examples:'); + writeLn(''); + writeLn(' bw confirm org-member 7063feab-4b10-472e-b64c-785e2b870b92 ' + + '--organizationid 310d5ffd-e9a2-4451-af87-ea054dce0f78'); + writeLn('', true); + }) + .action(async (object, id, cmd) => { + await this.exitIfLocked(); + const command = new ConfirmCommand(this.main.apiService, this.main.cryptoService); + const response = await command.run(object, id, cmd); + this.processResponse(response); + }); + program .command('import [format] [input]') .description('Import vault data from a file.')