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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-06-14 17:10:53 +02:00
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { Utils } from "@bitwarden/common/misc/utils";
import { OrganizationUserConfirmRequest } from "@bitwarden/common/models/request/organizationUserConfirmRequest";
import { Response } from "@bitwarden/node/cli/models/response";
2019-10-07 16:42:02 +02:00
export class ConfirmCommand {
constructor(private apiService: ApiService, private cryptoService: CryptoService) {}
2021-12-20 18:04:00 +01:00
2022-01-19 16:45:14 +01:00
async run(object: string, id: string, cmdOptions: Record<string, any>): Promise<Response> {
2019-10-07 16:42:02 +02:00
if (id != null) {
id = id.toLowerCase();
}
2021-12-20 18:04:00 +01:00
2022-01-19 16:45:14 +01:00
const normalizedOptions = new Options(cmdOptions);
2019-10-07 16:42:02 +02:00
switch (object.toLowerCase()) {
case "org-member":
2022-01-19 16:45:14 +01:00
return await this.confirmOrganizationMember(id, normalizedOptions);
2019-10-07 16:42:02 +02:00
default:
return Response.badRequest("Unknown object.");
}
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 === "") {
2019-10-07 16:42:02 +02: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.");
2019-10-07 16:42:02 +02:00
}
2022-01-19 16:45:14 +01:00
if (!Utils.isGuid(options.organizationId)) {
return Response.badRequest("`" + options.organizationId + "` is not a GUID.");
2019-10-07 16:42:02 +02:00
}
try {
2022-01-19 16:45:14 +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.");
}
2022-01-19 16:45:14 +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;
2022-01-19 16:45:14 +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);
}
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
}
}