bitwarden-estensione-browser/apps/cli/src/commands/confirm.command.ts

63 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-12-20 18:04:00 +01:00
import { ApiService } from "jslib-common/abstractions/api.service";
import { CryptoService } from "jslib-common/abstractions/crypto.service";
2022-03-03 18:24:41 +01:00
import { Utils } from "jslib-common/misc/utils";
2021-12-20 18:04:00 +01:00
import { OrganizationUserConfirmRequest } from "jslib-common/models/request/organizationUserConfirmRequest";
import { Response } from "jslib-node/cli/models/response";
2019-10-07 16:42:02 +02:00
export class ConfirmCommand {
2021-12-20 18:04:00 +01:00
constructor(private apiService: ApiService, private cryptoService: CryptoService) {}
2022-01-19 16:45:14 +01:00
async run(object: string, id: string, cmdOptions: Record<string, any>): Promise<Response> {
2021-12-20 18:04:00 +01:00
if (id != null) {
id = id.toLowerCase();
}
2022-01-19 16:45:14 +01:00
const normalizedOptions = new Options(cmdOptions);
2021-12-20 18:04:00 +01:00
switch (object.toLowerCase()) {
case "org-member":
2022-01-19 16:45:14 +01:00
return await this.confirmOrganizationMember(id, normalizedOptions);
2021-12-20 18:04:00 +01:00
default:
return Response.badRequest("Unknown object.");
2019-10-07 16:42:02 +02:00
}
2021-12-20 18:04:00 +01:00
}
2019-10-07 16:42:02 +02:00
2022-01-19 16:45:14 +01:00
private async confirmOrganizationMember(id: string, options: Options) {
if (options.organizationId == null || options.organizationId === "") {
2021-12-20 18:04:00 +01:00
return Response.badRequest("--organizationid <organizationid> required.");
}
if (!Utils.isGuid(id)) {
2022-01-19 16:45:14 +01:00
return Response.badRequest("`" + id + "` is not a GUID.");
2021-12-20 18:04:00 +01:00
}
2022-01-19 16:45:14 +01:00
if (!Utils.isGuid(options.organizationId)) {
return Response.badRequest("`" + options.organizationId + "` is not a GUID.");
2021-12-20 18:04:00 +01:00
}
try {
2022-01-19 16:45:14 +01:00
const orgKey = await this.cryptoService.getOrgKey(options.organizationId);
2021-12-20 18:04:00 +01:00
if (orgKey == null) {
throw new Error("No encryption key for this organization.");
}
2022-01-19 16:45:14 +01:00
const orgUser = await this.apiService.getOrganizationUser(options.organizationId, id);
2021-12-20 18:04:00 +01: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;
2022-01-19 16:45:14 +01:00
await this.apiService.postOrganizationUserConfirm(options.organizationId, id, req);
2021-12-20 18:04:00 +01:00
return Response.success();
} catch (e) {
return Response.error(e);
2019-10-07 16:42:02 +02:00
}
2021-12-20 18:04:00 +01:00
}
2019-10-07 16:42:02 +02:00
}
2022-01-19 16:45:14 +01:00
class Options {
organizationId: string;
constructor(passedOptions: Record<string, any>) {
this.organizationId = passedOptions?.organizationid || passedOptions?.organizationId;
2022-01-19 16:45:14 +01:00
}
}