@kne/fastify-oidc 0.1.14 → 0.1.16
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/index.js +23 -2
- package/libs/controllers/config.js +41 -12
- package/libs/idp/index.js +6 -0
- package/libs/resource/verify-token.js +57 -19
- package/libs/utils/platform-assertion.js +108 -0
- package/libs/utils/platforms.js +110 -0
- package/package.json +8 -2
- package/platform-assertion.js +1 -0
package/index.js
CHANGED
|
@@ -13,6 +13,8 @@ const createAuthenticate = require('./libs/resource/authenticate');
|
|
|
13
13
|
const createTenantSync = require('./libs/resource/tenant-sync');
|
|
14
14
|
const createAppRegistry = require('./libs/resource/app-registry');
|
|
15
15
|
const createRemoteTenant = require('./libs/resource/remote-tenant');
|
|
16
|
+
const { createPlatformsRegistry } = require('./libs/utils/platforms');
|
|
17
|
+
const { createPlatformAssertionSigner, createPlatformAssertionVerifier, createMemoryJtiStore } = require('./libs/utils/platform-assertion');
|
|
16
18
|
const { locale, createTranslator } = require('./libs/utils/intl');
|
|
17
19
|
|
|
18
20
|
const DAY = 24 * 60 * 60;
|
|
@@ -57,6 +59,8 @@ module.exports = fp(
|
|
|
57
59
|
fileNamespace: 'fileManager',
|
|
58
60
|
clients: [],
|
|
59
61
|
resourceServers: [],
|
|
62
|
+
/** central 多平台:初始 platforms[],也可运行时 platforms.setAll / upsert */
|
|
63
|
+
platforms: [],
|
|
60
64
|
seedSelf: true,
|
|
61
65
|
selfClient: {},
|
|
62
66
|
serviceScopes: ['user:read', 'tenant:read', 'app:register', 'tenant:sync'],
|
|
@@ -109,7 +113,23 @@ module.exports = fp(
|
|
|
109
113
|
const translator = createTranslator({ fastify, options });
|
|
110
114
|
const idp = runtime.idpEnabled ? createIdp({ fastify, options, translator }) : null;
|
|
111
115
|
const revocation = createRevocation({ store: options.revocationStore, ttl: Math.max(options.ttl.accessToken, options.ttl.idToken) + options.clockTolerance });
|
|
112
|
-
const
|
|
116
|
+
const platforms = createPlatformsRegistry({ initial: options.platforms || [] });
|
|
117
|
+
const verifier = createVerifier({
|
|
118
|
+
options,
|
|
119
|
+
getLocalJWKS: idp ? () => idp.getLocalJWKS() : null,
|
|
120
|
+
getPlatforms: () => platforms.getAll()
|
|
121
|
+
});
|
|
122
|
+
let readyPromise = Promise.resolve();
|
|
123
|
+
const platformAssertion = idp
|
|
124
|
+
? createPlatformAssertionSigner({
|
|
125
|
+
issuer: runtime.issuer,
|
|
126
|
+
clockTolerance: options.clockTolerance,
|
|
127
|
+
getPrivateJwk: async () => {
|
|
128
|
+
await readyPromise;
|
|
129
|
+
return idp.getSigningJwk();
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
: createPlatformAssertionVerifier({ clockTolerance: options.clockTolerance, jtiStore: createMemoryJtiStore() });
|
|
113
133
|
const serviceClient = createServiceClient({ options });
|
|
114
134
|
const userMirror = createUserMirror({ fastify, options, serviceClient });
|
|
115
135
|
const dpop = createDPoP({ clockTolerance: options.clockTolerance, replayStore: options.dpopReplayStore });
|
|
@@ -139,7 +159,6 @@ module.exports = fp(
|
|
|
139
159
|
]);
|
|
140
160
|
}
|
|
141
161
|
|
|
142
|
-
let readyPromise = Promise.resolve();
|
|
143
162
|
if (idp) {
|
|
144
163
|
readyPromise = fastify.sequelize.syncPromise.then(() => idp.init());
|
|
145
164
|
readyPromise.catch(err => fastify.log.error({ err }, 'fastify-oidc: IdP 初始化失败'));
|
|
@@ -157,6 +176,8 @@ module.exports = fp(
|
|
|
157
176
|
['idp', idp],
|
|
158
177
|
['revocation', revocation],
|
|
159
178
|
['verifier', verifier],
|
|
179
|
+
['platforms', platforms],
|
|
180
|
+
['platformAssertion', platformAssertion],
|
|
160
181
|
['serviceClient', serviceClient],
|
|
161
182
|
['userMirror', userMirror],
|
|
162
183
|
['tenantSync', tenantSync],
|
|
@@ -2,10 +2,12 @@ const fp = require('fastify-plugin');
|
|
|
2
2
|
const { str, bool, success } = require('../schema/response');
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* 前端登录方式发现:免登录,只返回公开信息,不能包含任何密钥。
|
|
6
|
+
* 多平台 central:附带 platforms[],前端选平台或用 ?iss= / ?platform=。
|
|
6
7
|
*/
|
|
7
8
|
module.exports = fp(async (fastify, options) => {
|
|
8
9
|
const { runtime } = options;
|
|
10
|
+
const oidc = () => fastify[options.name];
|
|
9
11
|
|
|
10
12
|
fastify.get(
|
|
11
13
|
`${options.prefix}/config`,
|
|
@@ -13,25 +15,52 @@ module.exports = fp(async (fastify, options) => {
|
|
|
13
15
|
schema: {
|
|
14
16
|
tags: ['OIDC-配置'],
|
|
15
17
|
summary: '获取前端登录配置(免登录)',
|
|
16
|
-
description: '前端据此决定登录方式(发起 OIDC 授权的 issuer、client_id、resource
|
|
18
|
+
description: '前端据此决定登录方式(发起 OIDC 授权的 issuer、client_id、resource),只包含公开信息;多平台时含 platforms',
|
|
17
19
|
response: success('前端登录配置', {
|
|
18
20
|
type: 'object',
|
|
19
21
|
properties: {
|
|
20
22
|
mode: str('部署模式:standalone 独立部署(本项目即 IdP),central 由主项目统一登录'),
|
|
21
23
|
isMain: bool('standalone 模式下是否为主项目'),
|
|
22
|
-
issuer: str('OIDC issuer
|
|
23
|
-
clientId: str('
|
|
24
|
-
audience: str('
|
|
24
|
+
issuer: str('默认 OIDC issuer 地址(单平台或兼容字段)'),
|
|
25
|
+
clientId: str('默认前端 client_id'),
|
|
26
|
+
audience: str('默认 resource(API 资源标识)'),
|
|
27
|
+
platforms: {
|
|
28
|
+
type: 'array',
|
|
29
|
+
description: '已配对的多个主平台(公开信息);长度>1 时登录页应选择平台'
|
|
30
|
+
}
|
|
25
31
|
}
|
|
26
32
|
})
|
|
27
33
|
}
|
|
28
34
|
},
|
|
29
|
-
async
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
async request => {
|
|
36
|
+
const platforms = oidc().platforms ? oidc().platforms.publicList() : [];
|
|
37
|
+
const hintIss = request.query?.iss;
|
|
38
|
+
const hintPlatform = request.query?.platform;
|
|
39
|
+
let selected = null;
|
|
40
|
+
if (platforms.length) {
|
|
41
|
+
if (hintIss) {
|
|
42
|
+
selected = platforms.find(p => p.issuer === hintIss || p.issuer === String(hintIss).replace(/\/+$/, ''));
|
|
43
|
+
} else if (hintPlatform) {
|
|
44
|
+
const key = String(hintPlatform).replace(/\/+$/, '').toLowerCase();
|
|
45
|
+
selected = platforms.find(p => p.platformOrigin.toLowerCase() === key || p.platformName === hintPlatform);
|
|
46
|
+
} else if (options.mode === 'central' && platforms.length === 1) {
|
|
47
|
+
// central 单平台:默认用该平台登录
|
|
48
|
+
selected = platforms[0];
|
|
49
|
+
}
|
|
50
|
+
// standalone:即使 hydrate 了配对主平台(供验运维令牌),默认登录仍用本进程 IdP,
|
|
51
|
+
// 否则前端会去主平台拿 token,本地 account 用户对不上 → getSuperAdminInfo 401
|
|
52
|
+
}
|
|
53
|
+
const issuer = selected?.issuer || runtime.issuer;
|
|
54
|
+
const clientId = selected?.clientId || runtime.clientId;
|
|
55
|
+
const audience = selected?.audience || runtime.audience;
|
|
56
|
+
return {
|
|
57
|
+
mode: options.mode,
|
|
58
|
+
isMain: options.mode === 'standalone' && !!options.isMain,
|
|
59
|
+
issuer,
|
|
60
|
+
clientId,
|
|
61
|
+
audience,
|
|
62
|
+
platforms
|
|
63
|
+
};
|
|
64
|
+
}
|
|
36
65
|
);
|
|
37
66
|
});
|
package/libs/idp/index.js
CHANGED
|
@@ -129,6 +129,11 @@ module.exports = ({ fastify, options, translator }) => {
|
|
|
129
129
|
|
|
130
130
|
const close = () => timers.forEach(clearInterval);
|
|
131
131
|
|
|
132
|
+
const getSigningJwk = async () => {
|
|
133
|
+
const { keys } = await getServices().key.getPrivateJwks();
|
|
134
|
+
return (keys && keys[0]) || null;
|
|
135
|
+
};
|
|
136
|
+
|
|
132
137
|
return {
|
|
133
138
|
identity,
|
|
134
139
|
init,
|
|
@@ -138,6 +143,7 @@ module.exports = ({ fastify, options, translator }) => {
|
|
|
138
143
|
getAdapter: name => assertReady().getAdapter(name),
|
|
139
144
|
getLocalJWKS: () => state && state.localJWKS,
|
|
140
145
|
getPublicJwks: () => state && state.publicJwks,
|
|
146
|
+
getSigningJwk,
|
|
141
147
|
handle: (req, res) => assertReady().callback(req, res)
|
|
142
148
|
};
|
|
143
149
|
};
|
|
@@ -1,29 +1,65 @@
|
|
|
1
|
-
const { jwtVerify, createRemoteJWKSet } = require('jose');
|
|
1
|
+
const { jwtVerify, createRemoteJWKSet, decodeJwt } = require('jose');
|
|
2
2
|
const { createError } = require('../utils/intl');
|
|
3
3
|
const { TENANT_SYNC_EVENT } = require('../utils/app-permissions');
|
|
4
4
|
|
|
5
5
|
const LOGOUT_EVENT = 'http://schemas.openid.net/event/backchannel-logout';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* standalone
|
|
8
|
+
* standalone:本进程 JWKS。
|
|
9
|
+
* central 单平台:主项目 jwks_uri。
|
|
10
|
+
* central 多平台:先读 token.iss,命中 platforms 后用对应 JWKS + audience 验签。
|
|
9
11
|
*/
|
|
10
|
-
module.exports = ({ options, getLocalJWKS }) => {
|
|
12
|
+
module.exports = ({ options, getLocalJWKS, getPlatforms }) => {
|
|
11
13
|
const { runtime } = options;
|
|
12
|
-
|
|
14
|
+
const remoteByUri = new Map();
|
|
15
|
+
|
|
16
|
+
const getRemoteJWKS = jwksUri => {
|
|
17
|
+
let set = remoteByUri.get(jwksUri);
|
|
18
|
+
if (!set) {
|
|
19
|
+
set = createRemoteJWKSet(new URL(jwksUri));
|
|
20
|
+
remoteByUri.set(jwksUri, set);
|
|
21
|
+
}
|
|
22
|
+
return set;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const resolveVerifyContext = (token, { audience } = {}) => {
|
|
26
|
+
// 先按 token.iss 匹配已配对 platforms(standalone 远程挂主平台时本地也有 IdP JWKS,
|
|
27
|
+
// 若优先走 local 会把主平台签发的运维令牌验成「服务令牌无效」)
|
|
28
|
+
const platforms = typeof getPlatforms === 'function' ? getPlatforms() : [];
|
|
29
|
+
if (platforms && platforms.length) {
|
|
30
|
+
let iss;
|
|
31
|
+
try {
|
|
32
|
+
iss = decodeJwt(token).iss;
|
|
33
|
+
} catch (e) {
|
|
34
|
+
throw createError(null, 'authenticationFailed');
|
|
35
|
+
}
|
|
36
|
+
const platform = platforms.find(p => p.issuer === iss || p.issuer === String(iss || '').replace(/\/+$/, ''));
|
|
37
|
+
if (platform && platform.status === 'paired') {
|
|
38
|
+
return {
|
|
39
|
+
jwks: getRemoteJWKS(platform.jwksUri || `${platform.issuer}/jwks`),
|
|
40
|
+
issuer: platform.issuer,
|
|
41
|
+
audience: audience || platform.audience || runtime.audience
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
13
45
|
|
|
14
|
-
const getJWKS = () => {
|
|
15
46
|
const local = getLocalJWKS && getLocalJWKS();
|
|
16
47
|
if (local) {
|
|
17
|
-
return local;
|
|
48
|
+
return { jwks: local, issuer: runtime.issuer, audience: audience || runtime.audience };
|
|
18
49
|
}
|
|
19
|
-
remote = remote || createRemoteJWKSet(new URL(runtime.jwksUri));
|
|
20
|
-
return remote;
|
|
21
|
-
};
|
|
22
50
|
|
|
23
|
-
|
|
24
|
-
|
|
51
|
+
return {
|
|
52
|
+
jwks: getRemoteJWKS(runtime.jwksUri),
|
|
25
53
|
issuer: runtime.issuer,
|
|
26
|
-
audience
|
|
54
|
+
audience: audience || runtime.audience
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const verifyAccessToken = async (token, { audience } = {}) => {
|
|
59
|
+
const ctx = resolveVerifyContext(token, { audience });
|
|
60
|
+
const { payload } = await jwtVerify(token, ctx.jwks, {
|
|
61
|
+
issuer: ctx.issuer,
|
|
62
|
+
audience: ctx.audience,
|
|
27
63
|
typ: 'at+jwt',
|
|
28
64
|
clockTolerance: options.clockTolerance
|
|
29
65
|
});
|
|
@@ -31,9 +67,10 @@ module.exports = ({ options, getLocalJWKS }) => {
|
|
|
31
67
|
};
|
|
32
68
|
|
|
33
69
|
const verifyLogoutToken = async (token, { audience }) => {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
70
|
+
const ctx = resolveVerifyContext(token, { audience });
|
|
71
|
+
const { payload } = await jwtVerify(token, ctx.jwks, {
|
|
72
|
+
issuer: ctx.issuer,
|
|
73
|
+
audience: ctx.audience || audience,
|
|
37
74
|
clockTolerance: options.clockTolerance
|
|
38
75
|
});
|
|
39
76
|
if (!payload.events || typeof payload.events[LOGOUT_EVENT] !== 'object') {
|
|
@@ -49,9 +86,10 @@ module.exports = ({ options, getLocalJWKS }) => {
|
|
|
49
86
|
};
|
|
50
87
|
|
|
51
88
|
const verifyTenantSyncToken = async (token, { audience }) => {
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
89
|
+
const ctx = resolveVerifyContext(token, { audience });
|
|
90
|
+
const { payload } = await jwtVerify(token, ctx.jwks, {
|
|
91
|
+
issuer: ctx.issuer,
|
|
92
|
+
audience: ctx.audience || audience,
|
|
55
93
|
typ: 'tenant-sync+jwt',
|
|
56
94
|
clockTolerance: options.clockTolerance,
|
|
57
95
|
requiredClaims: ['jti', 'exp', 'tenant_id']
|
|
@@ -62,5 +100,5 @@ module.exports = ({ options, getLocalJWKS }) => {
|
|
|
62
100
|
return payload;
|
|
63
101
|
};
|
|
64
102
|
|
|
65
|
-
return { verifyAccessToken, verifyLogoutToken, verifyTenantSyncToken };
|
|
103
|
+
return { verifyAccessToken, verifyLogoutToken, verifyTenantSyncToken, resolveVerifyContext };
|
|
66
104
|
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
const crypto = require('node:crypto');
|
|
2
|
+
const { SignJWT, jwtVerify, createRemoteJWKSet, importJWK } = require('jose');
|
|
3
|
+
|
|
4
|
+
const ASSERTION_TYP = 'platform-link+jwt';
|
|
5
|
+
const DEFAULT_TTL = 5 * 60;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 主平台签发「平台身份断言」:远程 redeem / activate 用登记 origin 的 JWKS 验签,不信任请求体自报 origin。
|
|
9
|
+
*/
|
|
10
|
+
const createPlatformAssertionSigner = ({ getPrivateJwk, issuer, clockTolerance = 5 }) => {
|
|
11
|
+
if (typeof getPrivateJwk !== 'function') {
|
|
12
|
+
throw new Error('fastify-oidc: createPlatformAssertionSigner 需要 getPrivateJwk');
|
|
13
|
+
}
|
|
14
|
+
if (!issuer) {
|
|
15
|
+
throw new Error('fastify-oidc: createPlatformAssertionSigner 需要 issuer');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const sign = async ({ audience, ttl = DEFAULT_TTL, extra = {} } = {}) => {
|
|
19
|
+
if (!audience) {
|
|
20
|
+
throw new Error('fastify-oidc: platform assertion 需要 audience(远程 baseUrl)');
|
|
21
|
+
}
|
|
22
|
+
const jwk = await getPrivateJwk();
|
|
23
|
+
if (!jwk) {
|
|
24
|
+
throw new Error('fastify-oidc: IdP 私钥尚未就绪,无法签发平台断言');
|
|
25
|
+
}
|
|
26
|
+
const key = await importJWK(jwk, jwk.alg || 'RS256');
|
|
27
|
+
const now = Math.floor(Date.now() / 1000);
|
|
28
|
+
return new SignJWT(Object.assign({}, extra))
|
|
29
|
+
.setProtectedHeader({ alg: jwk.alg || 'RS256', typ: ASSERTION_TYP, kid: jwk.kid })
|
|
30
|
+
.setIssuer(issuer)
|
|
31
|
+
.setAudience(audience)
|
|
32
|
+
.setIssuedAt(now)
|
|
33
|
+
.setExpirationTime(now + ttl)
|
|
34
|
+
.setJti(crypto.randomUUID())
|
|
35
|
+
.sign(key);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
return { sign, typ: ASSERTION_TYP };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 远程侧验签:用登记时的 jwksUri 验签;jtiStore 可选防重放(async has/add)。
|
|
43
|
+
*/
|
|
44
|
+
const createPlatformAssertionVerifier = ({ clockTolerance = 5, jtiStore = null } = {}) => {
|
|
45
|
+
const jwksCache = new Map();
|
|
46
|
+
|
|
47
|
+
const getJWKS = jwksUri => {
|
|
48
|
+
let set = jwksCache.get(jwksUri);
|
|
49
|
+
if (!set) {
|
|
50
|
+
set = createRemoteJWKSet(new URL(jwksUri));
|
|
51
|
+
jwksCache.set(jwksUri, set);
|
|
52
|
+
}
|
|
53
|
+
return set;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const verify = async (token, { issuer, audience, jwksUri } = {}) => {
|
|
57
|
+
if (!token || !issuer || !audience || !jwksUri) {
|
|
58
|
+
throw new Error('fastify-oidc: verifyPlatformAssertion 需要 token、issuer、audience、jwksUri');
|
|
59
|
+
}
|
|
60
|
+
const { payload, protectedHeader } = await jwtVerify(token, getJWKS(jwksUri), {
|
|
61
|
+
issuer,
|
|
62
|
+
audience,
|
|
63
|
+
clockTolerance,
|
|
64
|
+
typ: ASSERTION_TYP
|
|
65
|
+
});
|
|
66
|
+
if (!payload.jti) {
|
|
67
|
+
throw new Error('fastify-oidc: platform assertion 缺少 jti');
|
|
68
|
+
}
|
|
69
|
+
if (jtiStore) {
|
|
70
|
+
if (await jtiStore.has(payload.jti)) {
|
|
71
|
+
throw new Error('fastify-oidc: platform assertion jti 已使用');
|
|
72
|
+
}
|
|
73
|
+
const ttlMs = Math.max(1000, ((payload.exp || 0) - Math.floor(Date.now() / 1000) + clockTolerance) * 1000);
|
|
74
|
+
await jtiStore.add(payload.jti, ttlMs);
|
|
75
|
+
}
|
|
76
|
+
return { payload, protectedHeader };
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return { verify, typ: ASSERTION_TYP };
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** 内存 jti 防重放(单进程);生产可换 Redis */
|
|
83
|
+
const createMemoryJtiStore = () => {
|
|
84
|
+
const map = new Map();
|
|
85
|
+
const has = async jti => {
|
|
86
|
+
const exp = map.get(jti);
|
|
87
|
+
if (!exp) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
if (exp <= Date.now()) {
|
|
91
|
+
map.delete(jti);
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
return true;
|
|
95
|
+
};
|
|
96
|
+
const add = async (jti, ttlMs) => {
|
|
97
|
+
map.set(jti, Date.now() + ttlMs);
|
|
98
|
+
};
|
|
99
|
+
return { has, add };
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
module.exports = {
|
|
103
|
+
ASSERTION_TYP,
|
|
104
|
+
DEFAULT_TTL,
|
|
105
|
+
createPlatformAssertionSigner,
|
|
106
|
+
createPlatformAssertionVerifier,
|
|
107
|
+
createMemoryJtiStore
|
|
108
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* central 多平台:运行时 platforms[](按 platformOrigin / issuer 唯一)。
|
|
3
|
+
* 登录与验签按 token.iss 选择对应 JWKS + audience。
|
|
4
|
+
*/
|
|
5
|
+
const normalizePlatform = (item = {}) => {
|
|
6
|
+
const platformOrigin = String(item.platformOrigin || '')
|
|
7
|
+
.replace(/\/+$/, '')
|
|
8
|
+
.trim();
|
|
9
|
+
const issuer = String(item.issuer || '')
|
|
10
|
+
.replace(/\/+$/, '')
|
|
11
|
+
.trim();
|
|
12
|
+
if (!platformOrigin || !issuer) {
|
|
13
|
+
throw new Error('fastify-oidc: platform 需要 platformOrigin 与 issuer');
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
platformOrigin,
|
|
17
|
+
platformName: item.platformName || platformOrigin,
|
|
18
|
+
issuer,
|
|
19
|
+
audience: item.audience || null,
|
|
20
|
+
spaClientId: item.spaClientId || item.clientId || null,
|
|
21
|
+
serviceClientId: item.serviceClientId || null,
|
|
22
|
+
jwksUri: item.jwksUri || `${issuer}/jwks`,
|
|
23
|
+
status: item.status || 'paired',
|
|
24
|
+
pairedAt: item.pairedAt || null
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const createPlatformsRegistry = ({ initial = [], onChange = null } = {}) => {
|
|
29
|
+
let list = [];
|
|
30
|
+
|
|
31
|
+
const setAll = (platforms = []) => {
|
|
32
|
+
const next = [];
|
|
33
|
+
const seen = new Set();
|
|
34
|
+
for (const raw of platforms) {
|
|
35
|
+
const item = normalizePlatform(raw);
|
|
36
|
+
const key = item.platformOrigin.toLowerCase();
|
|
37
|
+
if (seen.has(key)) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
seen.add(key);
|
|
41
|
+
next.push(item);
|
|
42
|
+
}
|
|
43
|
+
list = next;
|
|
44
|
+
if (typeof onChange === 'function') {
|
|
45
|
+
onChange(list.slice());
|
|
46
|
+
}
|
|
47
|
+
return list.slice();
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const upsert = platform => {
|
|
51
|
+
const item = normalizePlatform(platform);
|
|
52
|
+
const idx = list.findIndex(p => p.platformOrigin.toLowerCase() === item.platformOrigin.toLowerCase());
|
|
53
|
+
if (idx >= 0) {
|
|
54
|
+
list[idx] = Object.assign({}, list[idx], item);
|
|
55
|
+
} else {
|
|
56
|
+
list.push(item);
|
|
57
|
+
}
|
|
58
|
+
if (typeof onChange === 'function') {
|
|
59
|
+
onChange(list.slice());
|
|
60
|
+
}
|
|
61
|
+
return item;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const remove = platformOrigin => {
|
|
65
|
+
const key = String(platformOrigin || '')
|
|
66
|
+
.replace(/\/+$/, '')
|
|
67
|
+
.toLowerCase();
|
|
68
|
+
const before = list.length;
|
|
69
|
+
list = list.filter(p => p.platformOrigin.toLowerCase() !== key);
|
|
70
|
+
if (list.length !== before && typeof onChange === 'function') {
|
|
71
|
+
onChange(list.slice());
|
|
72
|
+
}
|
|
73
|
+
return list.length !== before;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const getAll = () => list.slice();
|
|
77
|
+
|
|
78
|
+
const findByIssuer = issuer => {
|
|
79
|
+
const key = String(issuer || '')
|
|
80
|
+
.replace(/\/+$/, '')
|
|
81
|
+
.toLowerCase();
|
|
82
|
+
return list.find(p => p.issuer.toLowerCase() === key) || null;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const findByOrigin = platformOrigin => {
|
|
86
|
+
const key = String(platformOrigin || '')
|
|
87
|
+
.replace(/\/+$/, '')
|
|
88
|
+
.toLowerCase();
|
|
89
|
+
return list.find(p => p.platformOrigin.toLowerCase() === key) || null;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const publicList = () =>
|
|
93
|
+
list
|
|
94
|
+
.filter(p => p.status === 'paired')
|
|
95
|
+
.map(p => ({
|
|
96
|
+
platformOrigin: p.platformOrigin,
|
|
97
|
+
platformName: p.platformName,
|
|
98
|
+
issuer: p.issuer,
|
|
99
|
+
clientId: p.spaClientId,
|
|
100
|
+
audience: p.audience
|
|
101
|
+
}));
|
|
102
|
+
|
|
103
|
+
if (initial.length) {
|
|
104
|
+
setAll(initial);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return { setAll, upsert, remove, getAll, findByIssuer, findByOrigin, publicList };
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
module.exports = { normalizePlatform, createPlatformsRegistry };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-oidc",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.16",
|
|
4
4
|
"description": "基于 oidc-provider 的 Fastify OIDC 认证插件,支持 standalone / central 多项目单点登录、租户与跨项目令牌",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -20,8 +20,14 @@
|
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
22
|
"index.js",
|
|
23
|
-
"libs"
|
|
23
|
+
"libs",
|
|
24
|
+
"platform-assertion.js"
|
|
24
25
|
],
|
|
26
|
+
"exports": {
|
|
27
|
+
".": "./index.js",
|
|
28
|
+
"./platform-assertion": "./platform-assertion.js",
|
|
29
|
+
"./libs/utils/crypto": "./libs/utils/crypto.js"
|
|
30
|
+
},
|
|
25
31
|
"repository": {
|
|
26
32
|
"type": "git",
|
|
27
33
|
"url": "git+https://github.com/kne-union/fastify-oidc.git"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./libs/utils/platform-assertion');
|