@authx/scopes

中文文档
3.1.0-alpha.56发布于 2024/06/07MITTypeScriptES Module零依赖近 30 天下载 3853

这是AuthX范围功能的小集合. 这些瞄准镜是人可读取的,完全OAuth2-相容的,支持模式匹配和设置代数.

中文使用文档
文档目录22 节
  1. 范围解剖
  2. 范围
  3. 范围模板
  4. 参数化范围
  5. 参数化范围模板
  6. 安装
  7. API 密码
  8. 是 Valid 范围
  9. 是 ValidScope 模板
  10. 常态
  11. 简化
  12. 等价键
  13. 超级集
  14. 是 Strict 超级集
  15. 子集
  16. 是 Strict 子集
  17. 获取接口
  18. 获取偏差
  19. 有接口
  20. 发展
  21. 脚本
  22. 文件

范围

这是AuthX范围功能的小集合. 这些瞄准镜是人可读取的,完全OAuth2-相容的,支持模式匹配和设置代数.


范围解剖 | 安装 | API 密码 | 发展


范围解剖

范围由3个领域组成,分别由 : 字符 :

billing:customer.abc:read.basic
|_____| |__________| |________|
   |         |           |
 realm    context      action

每个域包含部分,由 . 字符键.

范围

A 类 范围文字 范围总是以象征性、赠款或授权的形式存在,它已得到充分界定,随时可供资源使用.

范围文字中的分节可以是:

  • "单一的"符号: *
  • "多倍任意"符号: **
  • 匹配图案的"替换段" : /[a-zA-Z0-9_-]*/

例如,这些都是合法的 范围文字数字 :

billing:customer.abc:read.basic
billing:customer.*:*.basic
billing:**:read.*

范围模板

A 类 范围模板 是一个反相(超相) 范围文字中的值 范围文字,a部分 范围模板 可包含:

  • 匹配图案的“ 超时段 ” : /^\{[a-zA-Z0-9_-]+\}$/

例如,这些都是合法的 范围模板数字 :

billing:customer.{current_user_id}:read.basic
authx:v2.authorization..*.{current_client_id}..{current_grant_id}..{current_user_id}:**

范围模板的目的是描述一个范围,即一个价值目前尚不为人所知,但在创建赠款或授权时将会知道的范围。 范围模板从未以符号或授权方式被传递.

参数化范围

A 类 参数化范围 是一种双变量(一种方式的超集,另一种方式的子集) 范围文字中的值 范围文字,a部分 参数化范围 可包含:

  • 匹配图案的"参数化段": /^\([a-zA-Z0-9_-]+\)$/

A 中的一个域 参数化范围 不得在"参数化部分"两侧含有"多相"符号,因为"参数化部分"的位置会变得模糊.

例如,这些都是合法的 参数化范围数字 :

billing:customer.(user_id):read.basic
authx:v2.authorization..*.(client_id)..(grant_id)..(current_user_id):**
authx:v2.**.(client_id)..(grant_id)..(current_user_id):**

但以下内容无效:

authx:v2.**.(client_id)..**:**

参数化范围的目的是从特定范围文字中取出一个值. 这对于资源是有用的,在资源中,在行动开始之前知道某一数值比在行动开始之后检查范围好。 例如,一个应用程序可能想要知道所有可能的值: user_id 参数化范围 billing:customer.(user_id):read.basic 在查询数据库前 .

参数化的瞄准镜从未通过任何标志或授权.

参数化范围模板

某些功能接受一个参数化的范围模板,既允许"参数化部分",也允许"元件化部分".

安装

安装方式 npm install --save @authx/scopes

API 密码

请看 测试 用于完整的例子.

是 Valid 范围

  • isValidScope(scope: string): boolean
    isValidScope(collection: string[]): boolean
    

验证一个范围的格式是否正确.

import { validate } from "@authx/scopes";

validate("realm:context.identifier:action.**");
// => true

validate("realm:context.{identifier}:action");
// => false

validate("realm:context.***:action");
// => false

是 ValidScope 模板

  • isValidScope(scope: string): boolean
    isValidScope(collection: string[]): boolean
    

验证范围模板的格式是否正确.

import { validate } from "@authx/scopes";

validate("realm:context.identifier:action.**");
// => true

validate("realm:context.{identifier}:action");
// => true

validate("realm:context.***:action");
// => false

常态

  • normalize(scope: string): string
    normalize(collection: string[]): string[]
    
  • 掷出 InvalidScopeError 如果范围无效.

将范围正常化为最简单的代表.

import { normalize } from "@authx/scopes";

normalize("realm:**.**:action");
// => 'realm:*.**:action'

简化

  • simplify(collection: string[]): string[]
    
  • 掷出 InvalidScopeError 如果在 collection 无效.

简化范围收集 collection ,省略任何被收藏中另一个范围所使冗余的范围。 返回的收藏中的所有范围都实现了正常化.

import { simplify } from "@authx/scopes";

simplify(["realm:resource.*:action", "realm:**:action"]);
// => ["realm:**:action"]

等价键

  • isEqual(scopeOrCollectionA: string[] | string, scopeOrCollectionB: string[] | string): boolean
    
  • 掷出 InvalidScopeError 如果在 scopeOrCollectionA 或者说 scopeOrCollectionB 无效.

检查是否 scopeOrCollectionA 财务报告和审定财务报表 scopeOrCollectionB 都是一样的,忽略了多余的范围.

import { getIntersection } from "@authx/scopes";

getIntersection(["realm:**:*"], ["realm:**:action", "realm:**:*"]);
// => true

超级集

  • isSuperset(scopeOrCollectionA: string[] | string, scopeOrCollectionB: string[] | string): boolean
    
  • 掷出 InvalidScopeError 如果在 scopeOrCollectionA 或者说 scopeOrCollectionB 无效.

检查是否 scopeOrCollectionA 等于,或一个超集 scopeOrCollectionB。用于检查用户是否能够执行特定动作.

import { isSuperset } from "@authx/scopes";

isSuperset(["realm:**:*"], ["realm:**:action", "realm:**:*"]);
// => true

是 Strict 超级集

  • isStrictSuperset(scopeOrCollectionA: string[] | string, scopeOrCollectionB: string[] | string): boolean
    
  • 掷出 InvalidScopeError 如果在 scopeOrCollectionA 或者说 scopeOrCollectionB 无效.

检查是否 scopeOrCollectionA 是一个严格的超级集 scopeOrCollectionB。 。 。 .

import { isStrictSuperset } from "@authx/scopes";

isStrictSuperset(["realm:**:*"], ["realm:**:action", "realm:**:*"]);
// => false

子集

  • isSubset(scopeOrCollectionA: string[] | string, scopeOrCollectionB: string[] | string): boolean
    
  • 掷出 InvalidScopeError 如果在 scopeOrCollectionA 或者说 scopeOrCollectionB 无效.

检查是否 scopeOrCollectionA 等于或子集为 scopeOrCollectionB。 。 。 .

import { isSubset } from "@authx/scopes";

isSubset(["realm:**:action", "realm:**:*"], ["realm:**:*"]);
// => true

是 Strict 子集

  • isStrictSubset(scopeOrCollectionA: string[] | string, scopeOrCollectionB: string[] | string): boolean
    
  • 掷出 InvalidScopeError 如果在 scopeOrCollectionA 或者说 scopeOrCollectionB 无效.

检查是否 scopeOrCollectionA 是一个严格的子集 scopeOrCollectionB。 。 。 .

import { isStrictSubset } from "@authx/scopes";

isStrictSubset(["realm:**:action", "realm:**:*"], ["realm:**:*"]);
// => false

获取接口

  • getIntersection(scopeOrCollectionA: string[] | string, scopeOrCollectionB: string[] | string): string[]
    
  • 掷出 InvalidScopeError 如果在 scopeOrCollectionA 或者说 scopeOrCollectionB 无效.

获取交叉点 scopeOrCollectionA 财务报告和审定财务报表 scopeOrCollectionB,返回一组代表所有相交点的瞄准镜,或两种输入都具有的每一种能力.

import { getIntersection } from "@authx/scopes";

getIntersection(["realm:resource.*:action.*"], ["realm:**:action.read"]);
// => ["realm:resource.*:action.read"]

获取偏差

  • getDifference(collectionA: string[], collectionB: string[]): string[]
    
  • 掷出 InvalidScopeError 如果在 collectionA 或者说 collectionB 无效.

获取 collectionA 财务报告和审定财务报表 collectionB,返回在 collectionB 但是没有 collectionA。返回的收藏中包含规范范围 已写入 collectionB,即使返回范围与 collectionA。 。 。 .

import { getDifference } from "@authx/scopes";

getDifference(
  ["realm:resource.*:action.*"],
  ["realm:resource.foo:action.read", "realm:other:action.read"],
);
// => ["realm:other:action.read"]

有接口

  • hasIntersection(scopeOrCollectionA: string[] | string, scopeOrCollectionB: string[] | string): string[]
    
  • 掷出 InvalidScopeError 如果在 scopeOrCollectionA 或者说 scopeOrCollectionB 无效.

检查是否 scopeOrCollectionA 财务报告和审定财务报表 scopeOrCollectionB 交叉键。 在检查用户是否能够执行由该选项所代表的动作的任何子集时,此功能是有用的 subject 范围.

import { hasIntersection } from "@authx/scopes";

hasIntersection(["realm:resource.*:action.*"], ["realm:**:action.read"]);
// => true

发展

脚本

这些脚本可以使用 npm run <script>。 。 。 .

format

使用更漂亮的格式化此软件包中的代码 .

lint

对照更美的和斯林特的规则检查此套件的内容 .

prepare

构建来自 /src 页:1 /dist 带有优化的目录.

prepare:development

构建来自 /src 页:1 /dist 目录,并随着对源文件的修改而重建.

test

运行全部测试 /dist 目录.

test:development

运行全部测试 /dist 目录,并在更改时重新运行测试.

文件

弧线

这持有库的源代码.

盘点

编译和捆绑的代码 最终在这里分发。 这被git忽略了.

版本与发布记录

@authx/scopes 共发布 56 个版本,首次发布于 2019/05/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.0-alpha.56latest2024/06/072 年前
3.1.0-alpha.552024/05/112 年前
3.1.0-alpha.522023/02/023 年前
3.1.0-alpha.512023/01/063 年前
3.1.0-alpha.502022/07/234 年前
3.1.0-alpha.492022/07/094 年前

其他发布通道:3.1.0-alpha.40 → 3.1.0-alpha.40,3.1.0-alpha.41 → 3.1.0-alpha.41,3.1.0-alpha.42 → 3.1.0-alpha.42,3.1.0-alpha.43 → 3.1.0-alpha.43,3.1.0-alpha.44 → 3.1.0-alpha.44,3.1.0-alpha.45 → 3.1.0-alpha.45。

关于 @authx/scopes 的常见问题

@authx/scopes 是什么?

这是AuthX范围功能的小集合. 这些瞄准镜是人可读取的,完全OAuth2-相容的,支持模式匹配和设置代数.

如何安装 @authx/scopes?

在项目目录运行 npm install @authx/scopes,或使用 pnpm add / yarn add / bun add。

@authx/scopes 支持 TypeScript 吗?

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

@authx/scopes 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@authx/scopes 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@authx/scopes 可以商用吗?

@authx/scopes 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@authx/scopes 的下载量有多少?

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

@authx/scopes 还在维护吗?

最新版本 3.1.0-alpha.56 发布于 2024/06/07(2 年前);至今共发布 56 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/05/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。