@longzai-intelligence-auth/api-key-strategy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # @longzai-intelligence-auth/api-key-strategy
2
+
3
+ Auth 项目的 API Key 验证策略包,组合 API Key 凭证提取(载体维度)与自定义校验回调(算法维度),绑定认证核心的令牌契约。
4
+
5
+ ## 定位
6
+
7
+ 位于 Auth 项目 strategies 策略层,依赖 core 契约层与 credential 载体能力层。算法维度不依赖令牌验证器端口,由调用方注入校验回调实现哈希查表或远程校验。
8
+
9
+ ## 核心能力
10
+
11
+ 提供 API Key 凭证提取与校验回调的组合策略工厂;定义校验通过的主体信息类型;支持自定义请求头名称;返回请求级令牌验证器。
12
+
13
+ ## 实现情况
14
+
15
+ 已实现组合策略并统一导出,承载单元测试,随校验逻辑扩展持续补充对应能力。
@@ -0,0 +1,60 @@
1
+ import { CredentialExtractor } from "@longzai-intelligence-auth/credential";
2
+ import { RequestTokenVerifier } from "@longzai-intelligence-auth/core";
3
+ //#region src/index.d.ts
4
+ /**
5
+ * API Key 校验通过后的主体信息
6
+ */
7
+ type ApiKeyPrincipal = {
8
+ /**
9
+ * 主体标识(用户/客户端 ID)
10
+ */
11
+ principalId: string;
12
+ /**
13
+ * 租户 ID(可选,未提供时由上层填默认值)
14
+ */
15
+ tenantId?: string;
16
+ /**
17
+ * 角色列表
18
+ */
19
+ roles: string[];
20
+ /**
21
+ * 权限列表
22
+ */
23
+ permissions: string[];
24
+ };
25
+ /**
26
+ * API Key 校验回调
27
+ *
28
+ * @param key - 从请求头提取的 API Key 原始值
29
+ * @returns 校验通过返回主体信息;失败或不合法返回 null
30
+ */
31
+ type ApiKeyValidator = (key: string) => Promise<ApiKeyPrincipal | null>;
32
+ /**
33
+ * API Key 验证策略配置
34
+ */
35
+ type ApiKeyStrategyOptions = {
36
+ /**
37
+ * API Key 校验回调(必填)
38
+ *
39
+ * 调用方在此实现哈希查表、远程校验等具体逻辑。
40
+ */
41
+ validator: ApiKeyValidator;
42
+ /**
43
+ * API Key 所在的请求头名称(默认 "x-api-key")
44
+ */
45
+ headerName?: string;
46
+ /**
47
+ * 自定义凭证提取器(高级用法)
48
+ * 默认使用 createApiKeyExtractor({ headerName })
49
+ */
50
+ extractor?: CredentialExtractor;
51
+ };
52
+ /**
53
+ * 创建 API Key 验证策略
54
+ *
55
+ * @param options - API Key 验证策略配置
56
+ * @returns 实现 RequestTokenVerifier 的组合策略
57
+ */
58
+ declare function createApiKeyStrategy(options: ApiKeyStrategyOptions): RequestTokenVerifier;
59
+ //#endregion
60
+ export { ApiKeyPrincipal, ApiKeyStrategyOptions, ApiKeyValidator, createApiKeyStrategy };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ import{createApiKeyExtractor as e}from"@longzai-intelligence-auth/credential";function t(e){return{sub:e.principalId,type:`access`,...e.tenantId!==void 0&&{tenantId:e.tenantId},...e.roles.length>0&&{roles:e.roles},...e.permissions.length>0&&{permissions:e.permissions}}}function n(n){let{validator:r,headerName:i,extractor:a}=n,o=a??e({headerName:i});return{async verify(e){let n=o.extract(e);if(!n)return{success:!1,error:`请求未携带 API Key 凭证`};let i=await r(n);return i?{success:!0,payload:t(i)}:{success:!1,error:`API Key 校验失败`}}}}export{n as createApiKeyStrategy};
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@longzai-intelligence-auth/api-key-strategy",
3
+ "version": "0.1.0",
4
+ "license": "UNLICENSED",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "type": "module",
9
+ "sideEffects": false,
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js"
16
+ }
17
+ },
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "scripts": {
22
+ "build": "lzi-builder",
23
+ "build:prod": "NODE_ENV=production bun run build",
24
+ "prepublishOnly": "bun run build:prod",
25
+ "typecheck": "bun run typecheck:app && bun run typecheck:node && bun run typecheck:test",
26
+ "typecheck:app": "lzi-tsgo typecheck tsconfig/app.json",
27
+ "typecheck:node": "lzi-tsgo typecheck tsconfig/node.json",
28
+ "typecheck:test": "lzi-tsgo typecheck tsconfig/test.json",
29
+ "lint": "oxlint && oxfmt --check",
30
+ "lint:fix": "oxlint --fix && oxfmt",
31
+ "test": "bun test",
32
+ "test:watch": "bun test --watch",
33
+ "test:coverage": "bun test --coverage",
34
+ "test:unit": "bun test src/__tests__/unit/",
35
+ "test:integration": "bun test src/__tests__/integration/",
36
+ "clean": "lzi-dev-cli clean"
37
+ },
38
+ "dependencies": {
39
+ "@longzai-intelligence-auth/core": "0.1.0",
40
+ "@longzai-intelligence-auth/credential": "0.1.0"
41
+ }
42
+ }