confirm org members command

This commit is contained in:
Kyle Spearrin 2019-10-07 10:42:02 -04:00
parent 341421380f
commit 99c4291ee7
2 changed files with 85 additions and 0 deletions

View File

@ -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<Response> {
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 <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);
}
}
}

View File

@ -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 <object> <id>')
.option('--organizationid <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.')