1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-25 20:53:22 +00:00

[AC-2740] Add device-approval to bw serve (#9512)

* Extract bw serve endpoint configuration to a configurator class

* Add device-approval endpoints to bw serve
This commit is contained in:
Thomas Rittson
2024-06-13 11:32:51 +10:00
committed by GitHub
parent b35930074c
commit 89aa6220ca
14 changed files with 613 additions and 442 deletions

View File

@@ -6,6 +6,8 @@ import { MessageResponse } from "@bitwarden/cli/models/response/message.response
import { OrganizationService } from "@bitwarden/common/admin-console/services/organization/organization.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { ServiceContainer } from "../../service-container";
export class ApproveAllCommand {
constructor(
private organizationAuthRequestService: OrganizationAuthRequestService,
@@ -49,4 +51,11 @@ export class ApproveAllCommand {
return Response.error(e);
}
}
static create(serviceContainer: ServiceContainer) {
return new ApproveAllCommand(
serviceContainer.organizationAuthRequestService,
serviceContainer.organizationService,
);
}
}

View File

@@ -5,6 +5,7 @@ import { OrganizationService } from "@bitwarden/common/admin-console/abstraction
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { OrganizationAuthRequestService } from "../../../../bit-common/src/admin-console/auth-requests";
import { ServiceContainer } from "../../service-container";
export class ApproveCommand {
constructor(
@@ -51,4 +52,11 @@ export class ApproveCommand {
return Response.error(e);
}
}
static create(serviceContainer: ServiceContainer) {
return new ApproveCommand(
serviceContainer.organizationService,
serviceContainer.organizationAuthRequestService,
);
}
}

View File

@@ -6,6 +6,7 @@ import { OrganizationService } from "@bitwarden/common/admin-console/abstraction
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { OrganizationAuthRequestService } from "../../../../bit-common/src/admin-console/auth-requests";
import { ServiceContainer } from "../../service-container";
export class DenyAllCommand {
constructor(
@@ -46,4 +47,11 @@ export class DenyAllCommand {
return Response.error(e);
}
}
static create(serviceContainer: ServiceContainer) {
return new DenyAllCommand(
serviceContainer.organizationService,
serviceContainer.organizationAuthRequestService,
);
}
}

View File

@@ -5,6 +5,7 @@ import { OrganizationService } from "@bitwarden/common/admin-console/abstraction
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { OrganizationAuthRequestService } from "../../../../bit-common/src/admin-console/auth-requests";
import { ServiceContainer } from "../../service-container";
export class DenyCommand {
constructor(
@@ -43,4 +44,11 @@ export class DenyCommand {
return Response.error(e);
}
}
static create(serviceContainer: ServiceContainer) {
return new DenyCommand(
serviceContainer.organizationService,
serviceContainer.organizationAuthRequestService,
);
}
}

View File

@@ -42,11 +42,7 @@ export class DeviceApprovalProgram extends BaseProgram {
await this.exitIfFeatureFlagDisabled(FeatureFlag.BulkDeviceApproval);
await this.exitIfLocked();
const cmd = new ListCommand(
this.serviceContainer.organizationAuthRequestService,
this.serviceContainer.organizationService,
);
const cmd = ListCommand.create(this.serviceContainer);
const response = await cmd.run(options.organizationid);
this.processResponse(response);
});
@@ -61,10 +57,7 @@ export class DeviceApprovalProgram extends BaseProgram {
await this.exitIfFeatureFlagDisabled(FeatureFlag.BulkDeviceApproval);
await this.exitIfLocked();
const cmd = new ApproveCommand(
this.serviceContainer.organizationService,
this.serviceContainer.organizationAuthRequestService,
);
const cmd = ApproveCommand.create(this.serviceContainer);
const response = await cmd.run(options.organizationid, id);
this.processResponse(response);
});
@@ -78,10 +71,7 @@ export class DeviceApprovalProgram extends BaseProgram {
await this.exitIfFeatureFlagDisabled(FeatureFlag.BulkDeviceApproval);
await this.exitIfLocked();
const cmd = new ApproveAllCommand(
this.serviceContainer.organizationAuthRequestService,
this.serviceContainer.organizationService,
);
const cmd = ApproveAllCommand.create(this.serviceContainer);
const response = await cmd.run(options.organizationid);
this.processResponse(response);
});
@@ -96,10 +86,7 @@ export class DeviceApprovalProgram extends BaseProgram {
await this.exitIfFeatureFlagDisabled(FeatureFlag.BulkDeviceApproval);
await this.exitIfLocked();
const cmd = new DenyCommand(
this.serviceContainer.organizationService,
this.serviceContainer.organizationAuthRequestService,
);
const cmd = DenyCommand.create(this.serviceContainer);
const response = await cmd.run(options.organizationid, id);
this.processResponse(response);
});
@@ -113,10 +100,7 @@ export class DeviceApprovalProgram extends BaseProgram {
await this.exitIfFeatureFlagDisabled(FeatureFlag.BulkDeviceApproval);
await this.exitIfLocked();
const cmd = new DenyAllCommand(
this.serviceContainer.organizationService,
this.serviceContainer.organizationAuthRequestService,
);
const cmd = DenyAllCommand.create(this.serviceContainer);
const response = await cmd.run(options.organizationid);
this.processResponse(response);
});

View File

@@ -1 +1,6 @@
export { DeviceApprovalProgram } from "./device-approval.program";
export * from "./device-approval.program";
export * from "./approve.command";
export * from "./approve-all.command";
export * from "./deny.command";
export * from "./deny-all.command";
export * from "./list.command";

View File

@@ -6,6 +6,8 @@ import { ListResponse } from "@bitwarden/cli/models/response/list.response";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { ServiceContainer } from "../../service-container";
import { PendingAuthRequestResponse } from "./pending-auth-request.response";
export class ListCommand {
@@ -39,4 +41,11 @@ export class ListCommand {
return Response.error(e);
}
}
static create(serviceContainer: ServiceContainer) {
return new ListCommand(
serviceContainer.organizationAuthRequestService,
serviceContainer.organizationService,
);
}
}

View File

@@ -0,0 +1,95 @@
import * as koaRouter from "@koa/router";
import { OssServeConfigurator } from "@bitwarden/cli/oss-serve-configurator";
import {
ApproveAllCommand,
ApproveCommand,
DenyAllCommand,
DenyCommand,
ListCommand,
} from "./admin-console/device-approval";
import { ServiceContainer } from "./service-container";
export class BitServeConfigurator extends OssServeConfigurator {
constructor(protected override serviceContainer: ServiceContainer) {
super(serviceContainer);
}
override configureRouter(router: koaRouter): void {
// Register OSS endpoints
super.configureRouter(router);
// Register bit endpoints
this.serveDeviceApprovals(router);
}
private serveDeviceApprovals(router: koaRouter) {
router.get("/device-approval/:organizationId", async (ctx, next) => {
if (await this.errorIfLocked(ctx.response)) {
await next();
return;
}
const response = await ListCommand.create(this.serviceContainer).run(
ctx.params.organizationId,
);
this.processResponse(ctx.response, response);
await next();
});
router.post("/device-approval/:organizationId/approve-all", async (ctx, next) => {
if (await this.errorIfLocked(ctx.response)) {
await next();
return;
}
const response = await ApproveAllCommand.create(this.serviceContainer).run(
ctx.params.organizationId,
);
this.processResponse(ctx.response, response);
await next();
});
router.post("/device-approval/:organizationId/approve/:requestId", async (ctx, next) => {
if (await this.errorIfLocked(ctx.response)) {
await next();
return;
}
const response = await ApproveCommand.create(this.serviceContainer).run(
ctx.params.organizationId,
ctx.params.requestId,
);
this.processResponse(ctx.response, response);
await next();
});
router.post("/device-approval/:organizationId/deny-all", async (ctx, next) => {
if (await this.errorIfLocked(ctx.response)) {
await next();
return;
}
const response = await DenyAllCommand.create(this.serviceContainer).run(
ctx.params.organizationId,
);
this.processResponse(ctx.response, response);
await next();
});
router.post("/device-approval/:organizationId/deny/:requestId", async (ctx, next) => {
if (await this.errorIfLocked(ctx.response)) {
await next();
return;
}
const response = await DenyCommand.create(this.serviceContainer).run(
ctx.params.organizationId,
ctx.params.requestId,
);
this.processResponse(ctx.response, response);
await next();
});
}
}

View File

@@ -1,7 +1,9 @@
import { program } from "commander";
import { registerOssPrograms } from "@bitwarden/cli/register-oss-programs";
import { ServeProgram } from "@bitwarden/cli/serve.program";
import { BitServeConfigurator } from "./bit-serve-configurator";
import { registerBitPrograms } from "./register-bit-programs";
import { ServiceContainer } from "./service-container";
@@ -12,6 +14,9 @@ async function main() {
await registerOssPrograms(serviceContainer);
await registerBitPrograms(serviceContainer);
const serveConfigurator = new BitServeConfigurator(serviceContainer);
new ServeProgram(serviceContainer, serveConfigurator).register();
program.parse(process.argv);
}