@cedar-policy/cedar-authorization

中文文档
0.2.0发布于 2026/03/27Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 3.1万

JS/TS tools for authorization with the Cedar language

原始使用文档
文档目录8 节
  1. Cedar-Authorization
  2. Installation
  3. Features
  4. Usage
  5. API Reference
  6. Security
  7. License
  8. Publishing

Cedar-Authorization

A JavaScript/TypeScript library for authorization with the Cedar policy language. This package provides tools to integrate Cedar authorization into your applications, generate schemas from OpenAPI specifications, and manage authorization policies.

Installation

npm i --save @cedar-policy/cedar-authorization

Features

  • Authorization Engine: Implement authorization checks using Cedar policies

  • Schema Generation: Generate Cedar schemas from OpenAPI specifications

  • Policy Generation: Create starter policies based on your schema

  • CLI Tools: Command-line utilities for schema and policy generation

Usage

Authorization Engine

This package provides an interface for an "Authorization Engine" which takes a Cedar request and entities, and returns an authorization result.

You may find example usages here: https://github.com/cedar-policy/cedar-authorization/blob/main/tests/cedarAuthorizer.test.ts

CLI Tools

Schema Generation: Generate Cedar schemas from OpenAPI specifications

npx @cedar-policy/cedar-authorization generate-schema \
  --api-spec path/to/openapi.json \
  --namespace YourNamespace \
  --mapping-type SimpleRest

This will generate two schema files:

v2.cedarschema.json - Compatible with Cedar 2.x and 3.x

v4.cedarschema.json - Compatible with Cedar 4.x and required by the nodejs Cedar plugins

When generating OpenApi specs, keep the following limitations in mind:

  1. Version Support:
  • Only OpenAPI v3 specifications are supported (uses OpenAPIV3 types from openapi-types)
  • Earlier versions (like Swagger 2.0) are not supported
  1. Namespace Requirements:
  • Must have exactly one namespace
  • Namespace must follow strict formatting rules:
    • Must start with a letter or underscore
    • Can only include alphanumeric characters and underscores
    • Components can be separated by double colons (::)
    • Each component must start with a letter or underscore
  • Cannot use reserved words as namespaces ('if', 'in', 'is', '__cedar')
  1. Schema Limitations:
  • Only supports local schema references (must start with '#/components/schemas/') - can't link to a separate openApi file or url.
  1. Parameter Restrictions for operations:
  • Only supports 'path' and 'query' parameter types
  • Other parameter types (like header, cookie) are skipped with a warning
  • Parameters defined as direct $ref are not supported
  • Parameters must have "name", "schema", and "in" properties
  1. Server Configuration:
  • If multiple servers are defined in the OpenAPI spec, a basePath parameter is required
  • The provided basePath must match one of the server entries
  • Server URLs must be valid URLs that can be parsed
  1. Operation/Path Requirements:
  • Only supports standard HTTP methods (defined in SUPPORTED_HTTP_METHODS)
  • OPTIONS method is explicitly ignored
  • Each operation must have valid operation objects
  • x-cedar extensions, if present, must have valid appliesToResourceTypes
  1. Resource Type Limitations:
  • Default resource types (User, UserGroup, Application) are automatically included
  • Custom resource types must be referenced in action definitions
  • If a resource type is referenced but not defined in schemas, it gets an empty record shape

These limitations mean that complex OpenAPI specs with advanced features like non-standard extensions or sophisticated parameter types may not be fully supported by the tool.

Policy Generation: Create starter policies based on your schema

npx @cedar-policy/cedar-authorization generate-policies \
  --schema path/to/schema.json

This will create policy files in a policies directory with starter policies based on your schema.

CLI Tools: Command-line utilities for schema and policy generation

API Reference

Core Types

interface AuthorizationRequest {
  principal: EntityUid;
  action: EntityUid;
  resource: EntityUid;
  context: Record<string, CedarValueJson>;
}

interface Entity {
  uid: EntityUid;
  attrs: Record<string, CedarValueJson>;
  parents: EntityUid[];
}

type AuthorizationResult =
  { type: 'deny' } |
  { type: 'allow', authorizerInfo: AuthorizationResultInformation } |
  { type: 'error', message: string };

CedarInlineAuthorizationEngine

class CedarInlineAuthorizationEngine implements AuthorizationEngine {
  constructor(config: CedarInlineAuthorizerConfiguration);
  isAuthorized(request: AuthorizationRequest, entities: Entity[]): Promise<AuthorizationResult>;
}

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.

Publishing

Publishing to npm is done according to these links:

版本与发布记录

@cedar-policy/cedar-authorization 共发布 3 个版本,首次发布于 2025/06/16;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.0latest2026/03/277 个月前
0.1.12025/06/261.4 年前
0.1.02025/06/161.4 年前

关于 @cedar-policy/cedar-authorization 的常见问题

@cedar-policy/cedar-authorization 是什么?

JS/TS tools for authorization with the Cedar language

如何安装 @cedar-policy/cedar-authorization?

在项目目录运行 npm install @cedar-policy/cedar-authorization,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 cedar-authorization,可以用 npx cedar-authorization 直接运行。

@cedar-policy/cedar-authorization 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cedar-policy/cedar-authorization 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cedar-policy/cedar-authorization 可以商用吗?

@cedar-policy/cedar-authorization 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@cedar-policy/cedar-authorization 的下载量有多少?

根据 npm 官方下载统计,@cedar-policy/cedar-authorization 在 2026/10/10 – 2026/11/08 这 30 天内被下载 31,277 次,日均约 1,043 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cedar-policy/cedar-authorization 还在维护吗?

最新版本 0.2.0 发布于 2026/03/27(7 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/06/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。