@autional/shared 0.1.0-rc.63 → 0.1.0-rc.64

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autional/shared",
3
- "version": "0.1.0-rc.63",
3
+ "version": "0.1.0-rc.64",
4
4
  "description": "Autional shared layer — API client, auth, types, hooks, utils",
5
5
  "license": "AGPL-3.0",
6
6
  "main": "./src/index.ts",
@@ -12,6 +12,8 @@
12
12
  "./slug-from-url": "./src/auth/slug-from-url.ts",
13
13
  "./portal": "./src/portal/registry.ts",
14
14
  "./fetch": "./src/api/fetch-client.ts",
15
+ "./jwt": "./src/auth/jwt-payload.ts",
16
+ "./pkce": "./src/auth/pkce.ts",
15
17
  "./generated/api": "./src/generated/api.ts",
16
18
  "./generated/types": "./src/generated/types.ts",
17
19
  "./generated/queries": "./src/generated/queries.ts",
@@ -0,0 +1,44 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { decodeJwtPayload } from '@autional/shared/jwt';
3
+ import { generatePKCE } from '@autional/shared/pkce';
4
+
5
+ /**
6
+ * 子路径导出的意义:站点(配置中心/密钥中心)要复用 JWT 解码与 PKCE,
7
+ * 但不该为此引入整包 index(axios/react-query/zustand/生成类型 130 个模块)。
8
+ * 这两个模块都零 import。
9
+ */
10
+ describe('@autional/shared 的 /jwt 与 /pkce 子路径', () => {
11
+ it('/jwt 解 base64url 载荷(含 -/_ 归一)', () => {
12
+ // {"sub":"u1","exp":4102444800,"tenant_id":"t-1"} —— 用 base64url(含 -/_)编码
13
+ const payload = decodeJwtPayload(
14
+ 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1MSIsImV4cCI6NDEwMjQ0NDgwMCwidGVuYW50X2lkIjoidC0xIn0.sig',
15
+ );
16
+ expect(payload).toMatchObject({ sub: 'u1', tenant_id: 't-1' });
17
+ });
18
+
19
+ it('/jwt 对坏输入返回 null(不抛)', () => {
20
+ expect(decodeJwtPayload('not-a-jwt')).toBeNull();
21
+ });
22
+
23
+ it('/pkce 生成 RFC 7636 的 verifier/challenge 对', async () => {
24
+ const pair = await generatePKCE();
25
+ // verifier ∈ 43–128 字符、字符集 A-Za-z0-9-._~
26
+ expect(pair.verifier).toMatch(/^[A-Za-z0-9\-._~]{43,128}$/);
27
+ // S256 challenge = base64url(sha256(verifier)),固定 43 字符
28
+ expect(pair.challenge).toMatch(/^[A-Za-z0-9_-]{43}$/);
29
+
30
+ const another = await generatePKCE();
31
+ expect(another.verifier).not.toBe(pair.verifier);
32
+ });
33
+
34
+ it('两个模块都不引入运行时依赖(源码级断言)', async () => {
35
+ const fs = await import('node:fs/promises');
36
+ for (const rel of ['../jwt-payload.ts', '../pkce.ts']) {
37
+ const src = await fs.readFile(new URL(rel, import.meta.url), 'utf8');
38
+ const imports = src
39
+ .split(/\r?\n/)
40
+ .filter((line) => /^\s*import\s/.test(line) || /^\s*\} from\s+['"]/.test(line));
41
+ expect(imports, rel).toEqual([]);
42
+ }
43
+ });
44
+ });