2019-10-07 16:42:02 +02:00
|
|
|
import * as program from 'commander';
|
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { ApiService } from 'jslib-common/abstractions/api.service';
|
|
|
|
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
|
2019-10-07 16:42:02 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { OrganizationUserConfirmRequest } from 'jslib-common/models/request/organizationUserConfirmRequest';
|
2019-10-07 16:42:02 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { Response } from 'jslib-node/cli/models/response';
|
2019-10-07 16:42:02 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { Utils } from 'jslib-common/misc/utils';
|
2019-10-07 16:42:02 +02:00
|
|
|
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
private async confirmOrganizationMember(id: string, options: program.OptionValues) {
|
|
|
|
if (options.organizationid == null || options.organizationid === '') {
|
2019-10-07 16:42:02 +02:00
|
|
|
return Response.badRequest('--organizationid <organizationid> required.');
|
|
|
|
}
|
|
|
|
if (!Utils.isGuid(id)) {
|
|
|
|
return Response.error('`' + id + '` is not a GUID.');
|
|
|
|
}
|
2021-02-03 18:44:33 +01:00
|
|
|
if (!Utils.isGuid(options.organizationid)) {
|
|
|
|
return Response.error('`' + options.organizationid + '` is not a GUID.');
|
2019-10-07 16:42:02 +02:00
|
|
|
}
|
|
|
|
try {
|
2021-02-03 18:44:33 +01:00
|
|
|
const orgKey = await this.cryptoService.getOrgKey(options.organizationid);
|
2019-10-07 16:42:02 +02:00
|
|
|
if (orgKey == null) {
|
|
|
|
throw new Error('No encryption key for this organization.');
|
|
|
|
}
|
2021-02-03 18:44:33 +01:00
|
|
|
const orgUser = await this.apiService.getOrganizationUser(options.organizationid, id);
|
2019-10-07 16:42:02 +02:00
|
|
|
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;
|
2021-02-03 18:44:33 +01:00
|
|
|
await this.apiService.postOrganizationUserConfirm(options.organizationid, id, req);
|
2019-10-07 16:42:02 +02:00
|
|
|
return Response.success();
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|