Cerbos JavaScript SDK
    Preparing search index...

    A client for interacting with an embedded Cerbos policy decision point (PDP).

    Embedded PDP bundles are WebAssembly modules downloaded from Cerbos Hub. Bundle download URLs are available in the "Embedded" section of the "Decision points" page of your Cerbos Hub workspace.

    See the parent class for available methods.

    Hierarchy (View Summary)

    Index

    Constructors

    • Create a client for interacting with an embedded Cerbos policy decision point (PDP), using a specified Loader to load the embedded PDP bundle.

      Parameters

      Returns Embedded

      Bundle download URLs are available in the "Embedded" section of the "Decision points" page of your Cerbos Hub workspace.

      Fetch an embedded PDP bundle via HTTP in a supported browser or Node.js, and automatically update the bundle when newer versions become available:

      const loader = new AutoUpdatingLoader("https://lite.cerbos.cloud/bundle?workspace=...&label=...");
      const cerbos = new Embedded(loader);
    • Create a client for interacting with an embedded Cerbos policy decision point (PDP), using the default Loader to load the embedded PDP bundle.

      Parameters

      • source: Source

        WebAssembly binary code of an embedded PDP bundle, or a URL or HTTP response from which to stream it.

      • Optionaloptions: Options

        Additional settings.

      Returns Embedded

      This is equivalent to new Embedded(new Loader(source, options)).

      Bundle download URLs are available in the "Embedded" section of the "Decision points" page of your Cerbos Hub workspace.

      Fetch an embedded PDP bundle via HTTP in a supported browser or Node.js:

      const cerbos = new Embedded("https://lite.cerbos.cloud/bundle?workspace=...&label=...");
      

    Properties

    loader: Loader

    The Loader used to load the embedded policy decision point bundle.

    Methods

    • Add policies, or update existing policies.

      Parameters

      Returns Promise<void>

      Requires

      Create a policy in code:

      await cerbos.addOrUpdatePolicies({
      policies: [{
      resourcePolicy: {
      resource: "document",
      version: "1",
      rules: [{
      actions: ["*"],
      effect: Effect.ALLOW,
      roles: ["ADMIN"],
      }],
      },
      }],
      });

      Load a policy from a YAML or JSON file with readPolicy:

      import { readPolicy } from "@cerbos/files";

      await cerbos.addOrUpdatePolicies({
      policies: [await readPolicy("path/to/policy.yaml")],
      });

      Load policies and schemas from a directory with readDirectory:

      import { readDirectory } from "@cerbos/files";

      const { policies, schemas } = await readDirectory("path/to/directory");

      await cerbos.addOrUpdateSchemas({ schemas });
      await cerbos.addOrUpdatePolicies({ policies });
    • Add schemas to be used for validating principal or resource attributes, or update existing schemas.

      Parameters

      Returns Promise<void>

      Requires

      Create a schema in code:

      await cerbos.addOrUpdateSchemas({
      schemas: [{
      id: "document.json",
      definition: {
      type: "object",
      properties: {
      owner: { type: "string" }
      }
      },
      }],
      });

      Load a schema from a JSON file with readSchema:

      import { readSchema } from "@cerbos/files";

      await cerbos.addOrUpdateSchemas({
      schemas: [await readSchema("_schemas/path/to/schema.json")],
      });

      Load policies and schemas from a directory with readDirectory:

      import { readDirectory } from "@cerbos/files";

      const { policies, schemas } = await readDirectory("path/to/directory");

      await cerbos.addOrUpdateSchemas({ schemas });
      await cerbos.addOrUpdatePolicies({ policies });
    • Check a principal's permissions on a set of resources.

      Parameters

      Returns Promise<CheckResourcesResponse>

      const decision = await cerbos.checkResources({
      principal: {
      id: "user@example.com",
      roles: ["USER"],
      attr: { tier: "PREMIUM" },
      },
      resources: [
      {
      resource: {
      kind: "document",
      id: "1",
      attr: { owner: "user@example.com" },
      },
      actions: ["view", "edit"],
      },
      {
      resource: {
      kind: "image",
      id: "1",
      attr: { owner: "user@example.com" },
      },
      actions: ["delete"],
      },
      ],
      });

      decision.isAllowed({
      resource: { kind: "document", id: "1" },
      action: "view",
      }); // => true
    • Delete a schema.

      Parameters

      Returns Promise<boolean>

      Requires

      The way this method handles failure depends on the version of the connected PDP server. When the server is running Cerbos v0.25 or later, it returns true if the schema was deleted and false if the schema was not found. With earlier versions of Cerbos, it throws an error if the schema was not found, and returns successfully if the schema was deleted; the returned value should be ignored.

      const deleted = await cerbos.deleteSchema("document.json");
      
    • Check if a principal is allowed to perform an action on a resource.

      Parameters

      Returns Promise<boolean>

      await cerbos.isAllowed({
      principal: {
      id: "user@example.com",
      roles: ["USER"],
      attr: { tier: "PREMIUM" },
      },
      resource: {
      kind: "document",
      id: "1",
      attr: { owner: "user@example.com" },
      },
      action: "view",
      }); // => true