@kne/fastify-oidc 0.1.3 → 0.1.5
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 +4 -2
- package/libs/controllers/service.js +3 -2
- package/libs/idp/identity.js +13 -1
- package/libs/resource/authenticate.js +23 -7
- package/libs/resource/user-mirror.js +32 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -440,7 +440,7 @@ oidc-provider 的交互地址,303 跳转到 `${ORIGIN}${interactionPage}?uid={
|
|
|
440
440
|
|
|
441
441
|
| 接口 | scope | 说明 |
|
|
442
442
|
|------|-------|------|
|
|
443
|
-
| `GET /api/oidc/service/user?id=` | `user:read` | 用户资料 `{ id, nickname, avatar, email, phone, gender, birthday, description, status }` |
|
|
443
|
+
| `GET /api/oidc/service/user?id=` | `user:read` | 用户资料 `{ id, nickname, avatar, email, phone, gender, birthday, description, status, isSuperAdmin }` |
|
|
444
444
|
| `GET /api/oidc/service/tenant-user?userId=&tenantId=` | `tenant:read` | 用户在租户内的身份、角色、权限(fastify-tenant `getTenantUserInfo` 结果) |
|
|
445
445
|
|
|
446
446
|
##### Back-Channel Logout(central)
|
|
@@ -453,7 +453,7 @@ oidc-provider 的交互地址,303 跳转到 `${ORIGIN}${interactionPage}?uid={
|
|
|
453
453
|
|
|
454
454
|
| 方法 | 说明 |
|
|
455
455
|
|------|------|
|
|
456
|
-
| `authenticate.user` | 校验 `Authorization: Bearer / DPoP
|
|
456
|
+
| `authenticate.user` | 校验 `Authorization: Bearer / DPoP`(SSE 请求即 `Accept: text/event-stream` 且无该头时,读 query 中的 `Authorization` / `DPoP`,供原生 EventSource 使用),填充 `request.user` 与兼容字段;无 token 且 `legacyToken` 时回退 `x-user-token`;拒绝 client 令牌 |
|
|
457
457
|
| `authenticate.tenantUser` | 按 `tenantSource` 填充 `request.tenantUserInfo`;legacy 请求委托给 fastify-tenant |
|
|
458
458
|
| `authenticate.admin` | 委托 fastify-account 的超级管理员校验 |
|
|
459
459
|
| `authenticate.scope(...scopes)` | 返回中间件,校验 token scope |
|
|
@@ -479,6 +479,8 @@ oidc-provider 的交互地址,303 跳转到 `${ORIGIN}${interactionPage}?uid={
|
|
|
479
479
|
|
|
480
480
|
兼容字段:`request.authenticatePayload = { id, tenantId }`,`request.userInfo` 为 fastify-account 用户(central 为镜像用户)。
|
|
481
481
|
|
|
482
|
+
central 模式下镜像用户的 `isSuperAdmin` 随主项目同步(升降级均以主项目为准,生效延迟 ≤ `userMirrorTTL`),`authenticate.admin` 仍读本地字段;需配置 `serviceClient`。
|
|
483
|
+
|
|
482
484
|
##### 其它
|
|
483
485
|
|
|
484
486
|
| 方法 | 说明 |
|
|
@@ -26,11 +26,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
26
26
|
schema: { tags, summary: '获取用户资料', query: { type: 'object', required: ['id'], properties: { id: { type: 'string' } } } }
|
|
27
27
|
},
|
|
28
28
|
wrap(async request => {
|
|
29
|
-
const
|
|
29
|
+
const { identity } = getIdp();
|
|
30
|
+
const user = await identity.getUser(request.query.id);
|
|
30
31
|
if (!user) {
|
|
31
32
|
throw createError(NotFound, 'userNotFound');
|
|
32
33
|
}
|
|
33
|
-
return Object.assign(pick(user, PROFILE_FIELDS), { id: String(user.id) });
|
|
34
|
+
return Object.assign(pick(user, PROFILE_FIELDS), { id: String(user.id), isSuperAdmin: await identity.isSuperAdmin(user.id) });
|
|
34
35
|
})
|
|
35
36
|
);
|
|
36
37
|
|
package/libs/idp/identity.js
CHANGED
|
@@ -26,6 +26,18 @@ module.exports = ({ fastify, options }) => {
|
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
+
const isSuperAdmin = async id => {
|
|
30
|
+
const check = account().services.admin?.checkIsSuperAdmin;
|
|
31
|
+
if (!check) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
return (await check({ id })) === true;
|
|
36
|
+
} catch (e) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
29
41
|
const toProfileClaims = user => ({
|
|
30
42
|
sub: String(user.id),
|
|
31
43
|
name: user.nickname || undefined,
|
|
@@ -104,5 +116,5 @@ module.exports = ({ fastify, options }) => {
|
|
|
104
116
|
};
|
|
105
117
|
};
|
|
106
118
|
|
|
107
|
-
return { tenantEnabled, getUser, toProfileClaims, findAccount, verifyCredentials, listTenants, isTenantMember, getTenantContext };
|
|
119
|
+
return { tenantEnabled, getUser, isSuperAdmin, toProfileClaims, findAccount, verifyCredentials, listTenants, isTenantMember, getTenantContext };
|
|
108
120
|
};
|
|
@@ -4,18 +4,34 @@ const { createError } = require('../utils/intl');
|
|
|
4
4
|
|
|
5
5
|
const { Unauthorized, Forbidden } = httpErrors;
|
|
6
6
|
|
|
7
|
+
const parseAuthorization = value => {
|
|
8
|
+
const index = value.indexOf(' ');
|
|
9
|
+
const scheme = value.slice(0, index).toLowerCase();
|
|
10
|
+
const token = value.slice(index + 1).trim();
|
|
11
|
+
if (!['bearer', 'dpop'].includes(scheme) || !token) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
return { scheme, token };
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const isEventStream = request => String(request.headers.accept || '').includes('text/event-stream');
|
|
18
|
+
|
|
19
|
+
// 原生 EventSource 无法设置请求头,SSE 请求允许从 query 读取 Authorization / DPoP
|
|
7
20
|
const extractToken = request => {
|
|
8
21
|
const header = request.headers.authorization;
|
|
9
|
-
if (
|
|
22
|
+
if (header) {
|
|
23
|
+
return parseAuthorization(header);
|
|
24
|
+
}
|
|
25
|
+
if (!isEventStream(request)) {
|
|
10
26
|
return null;
|
|
11
27
|
}
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
if (!
|
|
28
|
+
const query = request.query || {};
|
|
29
|
+
const value = query.Authorization || query.authorization;
|
|
30
|
+
const extracted = typeof value === 'string' ? parseAuthorization(value) : null;
|
|
31
|
+
if (!extracted) {
|
|
16
32
|
return null;
|
|
17
33
|
}
|
|
18
|
-
return {
|
|
34
|
+
return Object.assign(extracted, { proof: query.DPoP || query.dpop });
|
|
19
35
|
};
|
|
20
36
|
|
|
21
37
|
/**
|
|
@@ -53,7 +69,7 @@ module.exports = ({ fastify, options, verifier, dpop, revocation, userMirror, se
|
|
|
53
69
|
}
|
|
54
70
|
try {
|
|
55
71
|
await dpop.verify({
|
|
56
|
-
proof: request.headers.dpop,
|
|
72
|
+
proof: extracted.proof || request.headers.dpop,
|
|
57
73
|
method: request.method,
|
|
58
74
|
url: getRequestUrl(request),
|
|
59
75
|
accessToken: extracted.token,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const crypto = require('node:crypto');
|
|
1
2
|
const { pick } = require('lodash');
|
|
2
3
|
|
|
3
4
|
const PROFILE_FIELDS = ['nickname', 'avatar', 'email', 'phone', 'gender', 'birthday', 'description'];
|
|
@@ -16,27 +17,54 @@ module.exports = ({ fastify, options, serviceClient }) => {
|
|
|
16
17
|
return null;
|
|
17
18
|
}
|
|
18
19
|
try {
|
|
19
|
-
|
|
20
|
+
const remote = await serviceClient.getUserProfile(userId);
|
|
21
|
+
const profile = pick(remote, PROFILE_FIELDS);
|
|
22
|
+
// 主项目超级管理员身份以主项目为准,升降级都同步到镜像,子项目 authenticate.admin 照常读本地 isSuperAdmin
|
|
23
|
+
if (typeof remote?.isSuperAdmin === 'boolean' && getModel().rawAttributes.isSuperAdmin) {
|
|
24
|
+
profile.isSuperAdmin = remote.isSuperAdmin;
|
|
25
|
+
}
|
|
26
|
+
return profile;
|
|
20
27
|
} catch (e) {
|
|
21
28
|
fastify.log.warn({ err: e, userId }, 'fastify-oidc: 获取主项目用户资料失败,使用本地镜像');
|
|
22
29
|
return null;
|
|
23
30
|
}
|
|
24
31
|
};
|
|
25
32
|
|
|
26
|
-
|
|
33
|
+
// fastify-account 的 user.userAccountId 非空:镜像用户挂一个随机密码的占位账号,central 模式下本地密码登录不可用
|
|
34
|
+
const createPlaceholderAccount = async () => {
|
|
35
|
+
const { userAccount } = fastify[options.accountNamespace].models;
|
|
36
|
+
if (!userAccount || !getModel().rawAttributes.userAccountId) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
return userAccount.create({ password: crypto.randomBytes(32).toString('hex'), salt: crypto.randomBytes(16).toString('hex') });
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const createUser = async values => {
|
|
27
43
|
const model = getModel();
|
|
28
|
-
const values = Object.assign({ nickname: userId }, profile, { id: userId, status: 0 });
|
|
29
44
|
try {
|
|
30
45
|
return await model.create(values, { hooks: false });
|
|
31
46
|
} catch (e) {
|
|
32
47
|
if (e.name !== 'SequelizeUniqueConstraintError') {
|
|
33
48
|
throw e;
|
|
34
49
|
}
|
|
35
|
-
fastify.log.warn({ userId }, 'fastify-oidc: 镜像用户邮箱或手机号与本地已有用户冲突,忽略这两个字段');
|
|
50
|
+
fastify.log.warn({ userId: values.id }, 'fastify-oidc: 镜像用户邮箱或手机号与本地已有用户冲突,忽略这两个字段');
|
|
36
51
|
return model.create(Object.assign({}, values, { email: null, phone: null }), { hooks: false });
|
|
37
52
|
}
|
|
38
53
|
};
|
|
39
54
|
|
|
55
|
+
const createMirror = async (userId, profile) => {
|
|
56
|
+
const account = await createPlaceholderAccount();
|
|
57
|
+
const values = Object.assign({ nickname: userId }, profile, { id: userId, status: 0 }, account ? { userAccountId: account.id } : {});
|
|
58
|
+
try {
|
|
59
|
+
const row = await createUser(values);
|
|
60
|
+
await account?.update({ belongToUserId: row.id });
|
|
61
|
+
return row;
|
|
62
|
+
} catch (e) {
|
|
63
|
+
await account?.destroy({ force: true }).catch(() => {});
|
|
64
|
+
throw e;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
40
68
|
const ensure = async userId => {
|
|
41
69
|
const cached = cache.get(userId);
|
|
42
70
|
if (cached && cached.expiresAt > Date.now()) {
|