@autional/shared 0.1.0-rc.65 → 0.1.0-rc.66
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,7 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
2
|
import {
|
|
3
3
|
expiresAtFromToken,
|
|
4
|
+
mergeTokenClaims,
|
|
4
5
|
pickClaimRoles,
|
|
5
6
|
pickClaimString,
|
|
6
7
|
sessionExpiryAt,
|
|
@@ -136,3 +137,36 @@ describe('sessionExpiryAt', () => {
|
|
|
136
137
|
expect(sessionExpiryAt(jwt({ sub: 'u' }), 12345)).toBe(12345);
|
|
137
138
|
});
|
|
138
139
|
});
|
|
140
|
+
|
|
141
|
+
describe('mergeTokenClaims', () => {
|
|
142
|
+
it('id_token 优先,access_token 补缺(tenant_id 只在 access 里也拿得到)', () => {
|
|
143
|
+
const access = jwt({ sub: 'u1', tenant_id: 't-1', roles: ['admin'] });
|
|
144
|
+
const id = jwt({ sub: 'u1', name: '张三', nonce: 'n-1' });
|
|
145
|
+
expect(mergeTokenClaims(access, id)).toMatchObject({
|
|
146
|
+
name: '张三',
|
|
147
|
+
tenant_id: 't-1',
|
|
148
|
+
nonce: 'n-1',
|
|
149
|
+
roles: ['admin'],
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('同名字段以 id_token 为准', () => {
|
|
154
|
+
const access = jwt({ sub: 'u1', name: '来自 access' });
|
|
155
|
+
const id = jwt({ sub: 'u1', name: '来自 id' });
|
|
156
|
+
expect(mergeTokenClaims(access, id)?.name).toBe('来自 id');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('只有一份令牌时用它', () => {
|
|
160
|
+
const access = jwt({ sub: 'u1', tenant_id: 't-1' });
|
|
161
|
+
expect(mergeTokenClaims(access, undefined)?.tenant_id).toBe('t-1');
|
|
162
|
+
expect(mergeTokenClaims(undefined, access)?.tenant_id).toBe('t-1');
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('两份都缺 → null', () => {
|
|
166
|
+
expect(mergeTokenClaims(undefined, undefined)).toBeNull();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('坏令牌不抛错', () => {
|
|
170
|
+
expect(() => mergeTokenClaims('bad', 'also-bad')).not.toThrow();
|
|
171
|
+
});
|
|
172
|
+
});
|
|
@@ -75,6 +75,25 @@ export function userFromClaims(
|
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
/**
|
|
79
|
+
* 合并 access_token 与 id_token 的声明:**id_token 优先,access_token 补缺**。
|
|
80
|
+
*
|
|
81
|
+
* 为什么需要:登录换票时若只取 id_token,一旦签发方的 id_token 是精简版
|
|
82
|
+
* (只有 sub/name/email,没有 tenant_id / roles),`X-Tenant-ID` 就会丢,
|
|
83
|
+
* 后续请求直接 403——而同样的信息在 access_token 里明明有。
|
|
84
|
+
* 反过来只取 access_token 又会丢掉 id_token 的 nonce、更完整的展示名。
|
|
85
|
+
* 所以按字段合并,而不是二选一。
|
|
86
|
+
*/
|
|
87
|
+
export function mergeTokenClaims(
|
|
88
|
+
accessToken?: string,
|
|
89
|
+
idToken?: string,
|
|
90
|
+
): Record<string, unknown> | null {
|
|
91
|
+
const access = accessToken ? decodeJwtPayload(accessToken) : null;
|
|
92
|
+
const id = idToken ? decodeJwtPayload(idToken) : null;
|
|
93
|
+
if (!access && !id) return null;
|
|
94
|
+
return { ...(access ?? {}), ...(id ?? {}) };
|
|
95
|
+
}
|
|
96
|
+
|
|
78
97
|
/**
|
|
79
98
|
* 过期时刻(epoch ms)。
|
|
80
99
|
*
|