@kne/fastify-oidc 0.1.12 → 0.1.14
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 +57 -2
- package/index.js +1 -0
- package/libs/controllers/admin.js +164 -40
- package/libs/controllers/backchannel.js +5 -2
- package/libs/controllers/config.js +16 -1
- package/libs/controllers/interaction.js +110 -13
- package/libs/controllers/service.js +145 -17
- package/libs/controllers/tenant-apps.js +15 -1
- package/libs/controllers/tenant-proxy.js +87 -19
- package/libs/controllers/tenant-sync.js +5 -2
- package/libs/idp/create-provider.js +31 -1
- package/libs/idp/identity.js +14 -0
- package/libs/idp/index.js +1 -0
- package/libs/idp/mount.js +8 -2
- package/libs/locale/en-US.js +5 -0
- package/libs/locale/zh-CN.js +5 -0
- package/libs/models/mfa-challenge.js +53 -0
- package/libs/models/session-mfa.js +31 -0
- package/libs/schema/response.js +103 -0
- package/libs/services/interaction.js +42 -1
- package/libs/services/mfa.js +125 -0
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
+
const { str, any, obj, success } = require('../schema/response');
|
|
2
3
|
|
|
3
4
|
module.exports = fp(async (fastify, options) => {
|
|
4
5
|
const { services, translator } = fastify[options.name];
|
|
@@ -7,13 +8,15 @@ module.exports = fp(async (fastify, options) => {
|
|
|
7
8
|
}
|
|
8
9
|
const { wrap } = translator;
|
|
9
10
|
const base = `${options.prefix}/interaction/:uid`;
|
|
10
|
-
const params = { type: 'object', required: ['uid'], properties: { uid: { type: 'string' } } };
|
|
11
|
-
const tags = ['OIDC
|
|
11
|
+
const params = { type: 'object', required: ['uid'], properties: { uid: { type: 'string', description: '交互 uid(oidc-provider 跳转到登录交互页时带上的 uid)' } } };
|
|
12
|
+
const tags = ['OIDC-登录交互'];
|
|
13
|
+
const HINT = '依赖浏览器携带的 oidc-provider 交互 cookie;交互过期或 uid 不匹配返回 400';
|
|
14
|
+
const redirect = description => success(description, { type: 'object', properties: { redirectTo: str('前端需要跳转的地址(回到授权端点继续流程)') } });
|
|
12
15
|
|
|
13
16
|
fastify.get(
|
|
14
17
|
base,
|
|
15
18
|
{
|
|
16
|
-
schema: { tags, summary: '交互入口,跳转到前端登录交互页', params }
|
|
19
|
+
schema: { tags, summary: '交互入口,跳转到前端登录交互页', description: '303 重定向到前端交互页(interactionPage?uid=<uid>),不返回 JSON', params }
|
|
17
20
|
},
|
|
18
21
|
async (request, reply) => {
|
|
19
22
|
const target = new URL(options.interactionPage, options.runtime.origin);
|
|
@@ -25,7 +28,54 @@ module.exports = fp(async (fastify, options) => {
|
|
|
25
28
|
fastify.get(
|
|
26
29
|
`${base}/details`,
|
|
27
30
|
{
|
|
28
|
-
schema: {
|
|
31
|
+
schema: {
|
|
32
|
+
tags,
|
|
33
|
+
summary: '获取当前交互详情(步骤、client、用户、可选租户)',
|
|
34
|
+
description: HINT,
|
|
35
|
+
params,
|
|
36
|
+
response: success('交互详情', {
|
|
37
|
+
type: 'object',
|
|
38
|
+
additionalProperties: true,
|
|
39
|
+
properties: {
|
|
40
|
+
uid: str('交互 uid'),
|
|
41
|
+
prompt: obj('当前步骤', {
|
|
42
|
+
name: str('步骤:login 登录,tenant 选择租户,mfa 多因素认证(短信),consent 确认授权'),
|
|
43
|
+
reasons: any('需要该步骤的原因'),
|
|
44
|
+
details: any('步骤详情(如缺少的 scope)')
|
|
45
|
+
}),
|
|
46
|
+
params: obj('授权请求参数', {
|
|
47
|
+
clientId: str('client_id'),
|
|
48
|
+
scope: str('申请的 scope'),
|
|
49
|
+
resource: any('申请的资源服务'),
|
|
50
|
+
tenantId: str('指定的租户 ID'),
|
|
51
|
+
loginHint: str('登录提示'),
|
|
52
|
+
uiLocales: str('界面语言')
|
|
53
|
+
}),
|
|
54
|
+
client: obj('发起授权的 client,不存在时为 null', {
|
|
55
|
+
clientId: str('client_id'),
|
|
56
|
+
clientName: str('应用名称'),
|
|
57
|
+
logoUri: str('logo 地址'),
|
|
58
|
+
clientUri: str('应用主页')
|
|
59
|
+
}),
|
|
60
|
+
user: obj('已登录用户,未登录时为 null', {
|
|
61
|
+
id: str('用户 ID'),
|
|
62
|
+
nickname: str('昵称'),
|
|
63
|
+
avatar: any('头像文件 ID'),
|
|
64
|
+
email: str('邮箱'),
|
|
65
|
+
phone: str('手机号')
|
|
66
|
+
}),
|
|
67
|
+
tenants: obj('可选租户(仅 tenant 步骤返回,否则为 null):{ list, defaultTenantId }'),
|
|
68
|
+
mfa: obj('多因素认证信息(仅 mfa 步骤返回)', {
|
|
69
|
+
method: str('验证方式,目前固定为 sms'),
|
|
70
|
+
tenantId: str('要求验证的租户 ID'),
|
|
71
|
+
maxAge: any('租户配置的 MFA 有效期(秒)'),
|
|
72
|
+
phoneMissing: any('账号没有手机号时为 true,此时无法完成验证,调用 mfa/send 会以 access_denied 结束交互'),
|
|
73
|
+
phoneMasked: str('脱敏手机号'),
|
|
74
|
+
resendAfter: any('距离可以重新发送验证码的秒数,0 表示可以发送')
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
}
|
|
29
79
|
},
|
|
30
80
|
wrap(async (request, reply) => services.interaction.details({ request, reply, uid: request.params.uid }))
|
|
31
81
|
);
|
|
@@ -36,18 +86,26 @@ module.exports = fp(async (fastify, options) => {
|
|
|
36
86
|
schema: {
|
|
37
87
|
tags,
|
|
38
88
|
summary: '账号密码登录',
|
|
89
|
+
description: `仅 login 步骤可调用,${HINT}。账号状态不允许登录时只返回 status,否则返回 redirectTo`,
|
|
39
90
|
params,
|
|
40
91
|
body: {
|
|
41
92
|
type: 'object',
|
|
42
93
|
required: ['password'],
|
|
43
94
|
properties: {
|
|
44
|
-
type: { type: 'string', enum: ['email', 'phone'], default: 'email' },
|
|
45
|
-
email: { type: 'string' },
|
|
46
|
-
phone: { type: 'string' },
|
|
47
|
-
password: { type: 'string' },
|
|
48
|
-
remember: { type: 'boolean', default: true }
|
|
95
|
+
type: { type: 'string', enum: ['email', 'phone'], default: 'email', description: '登录方式:email 邮箱(默认),phone 手机号' },
|
|
96
|
+
email: { type: 'string', description: '邮箱,type=email 时必填' },
|
|
97
|
+
phone: { type: 'string', description: '手机号,type=phone 时必填' },
|
|
98
|
+
password: { type: 'string', description: '密码' },
|
|
99
|
+
remember: { type: 'boolean', default: true, description: '是否保持登录(持久会话 cookie),默认 true' }
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
response: success('登录结果', {
|
|
103
|
+
type: 'object',
|
|
104
|
+
properties: {
|
|
105
|
+
redirectTo: str('登录成功后前端需要跳转的地址'),
|
|
106
|
+
status: any('账号状态,仅在账号不允许登录(如未激活、已禁用)时返回')
|
|
49
107
|
}
|
|
50
|
-
}
|
|
108
|
+
})
|
|
51
109
|
}
|
|
52
110
|
},
|
|
53
111
|
wrap(async (request, reply) => {
|
|
@@ -62,17 +120,56 @@ module.exports = fp(async (fastify, options) => {
|
|
|
62
120
|
schema: {
|
|
63
121
|
tags,
|
|
64
122
|
summary: '选择租户',
|
|
123
|
+
description: `仅 tenant 步骤可调用,${HINT}。租户不在用户可用列表中返回 400`,
|
|
65
124
|
params,
|
|
66
|
-
body: { type: 'object', required: ['tenantId'], properties: { tenantId: { type: 'string' } } }
|
|
125
|
+
body: { type: 'object', required: ['tenantId'], properties: { tenantId: { type: 'string', description: '选择的租户 ID(来自交互详情的 tenants.list)' } } },
|
|
126
|
+
response: redirect('选择成功')
|
|
67
127
|
}
|
|
68
128
|
},
|
|
69
129
|
wrap(async (request, reply) => services.interaction.selectTenant({ request, reply, uid: request.params.uid, tenantId: request.body.tenantId }))
|
|
70
130
|
);
|
|
71
131
|
|
|
132
|
+
fastify.post(
|
|
133
|
+
`${base}/mfa/send`,
|
|
134
|
+
{
|
|
135
|
+
schema: {
|
|
136
|
+
tags,
|
|
137
|
+
summary: '发送 MFA 短信验证码',
|
|
138
|
+
description: `仅 mfa 步骤可调用,${HINT}。验证码发到账号绑定的手机号,只在本次交互内有效;发送间隔内重复调用返回 400。账号没有手机号时以 access_denied 结束交互并返回 redirectTo`,
|
|
139
|
+
params,
|
|
140
|
+
response: success('发送结果', {
|
|
141
|
+
type: 'object',
|
|
142
|
+
properties: {
|
|
143
|
+
phoneMasked: str('脱敏手机号'),
|
|
144
|
+
expiresIn: any('验证码有效期(秒)'),
|
|
145
|
+
resendAfter: any('距离可以重新发送的秒数'),
|
|
146
|
+
redirectTo: str('账号没有手机号、交互已结束时前端需要跳转的地址')
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
wrap(async (request, reply) => services.interaction.mfaSend({ request, reply, uid: request.params.uid }))
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
fastify.post(
|
|
155
|
+
`${base}/mfa/verify`,
|
|
156
|
+
{
|
|
157
|
+
schema: {
|
|
158
|
+
tags,
|
|
159
|
+
summary: '校验 MFA 短信验证码',
|
|
160
|
+
description: `仅 mfa 步骤可调用,${HINT}。验证码错误、过期或错误次数过多返回 400,错误次数过多后需重新发送;通过后返回 redirectTo,id_token 的 amr 会包含 sms、mfa`,
|
|
161
|
+
params,
|
|
162
|
+
body: { type: 'object', required: ['code'], properties: { code: { type: 'string', description: '短信验证码' } } },
|
|
163
|
+
response: redirect('验证通过')
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
wrap(async (request, reply) => services.interaction.mfaVerify({ request, reply, uid: request.params.uid, code: request.body.code }))
|
|
167
|
+
);
|
|
168
|
+
|
|
72
169
|
fastify.post(
|
|
73
170
|
`${base}/confirm`,
|
|
74
171
|
{
|
|
75
|
-
schema: { tags, summary: '确认授权(consent)', params }
|
|
172
|
+
schema: { tags, summary: '确认授权(consent)', description: `仅 consent 步骤可调用,${HINT}。授予本次申请缺少的 scope 与资源权限`, params, response: redirect('授权成功') }
|
|
76
173
|
},
|
|
77
174
|
wrap(async (request, reply) => services.interaction.confirm({ request, reply, uid: request.params.uid }))
|
|
78
175
|
);
|
|
@@ -80,7 +177,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
80
177
|
fastify.post(
|
|
81
178
|
`${base}/abort`,
|
|
82
179
|
{
|
|
83
|
-
schema: { tags, summary: '取消登录', params }
|
|
180
|
+
schema: { tags, summary: '取消登录', description: `${HINT}。以 access_denied 结束授权流程,client 会收到错误回调`, params, response: redirect('已取消') }
|
|
84
181
|
},
|
|
85
182
|
wrap(async (request, reply) => services.interaction.abort({ request, reply, uid: request.params.uid }))
|
|
86
183
|
);
|
|
@@ -3,6 +3,7 @@ const httpErrors = require('http-errors');
|
|
|
3
3
|
const { pick } = require('lodash');
|
|
4
4
|
const { createError } = require('../utils/intl');
|
|
5
5
|
const { collectTenantImages } = require('../utils/tenant-images');
|
|
6
|
+
const { str, num, bool, any, list, obj, success, tenantList } = require('../schema/response');
|
|
6
7
|
|
|
7
8
|
const { NotFound, Forbidden } = httpErrors;
|
|
8
9
|
|
|
@@ -18,7 +19,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
18
19
|
const { authenticate, translator, services } = fastify[options.name];
|
|
19
20
|
const { wrap } = translator;
|
|
20
21
|
const getIdp = () => fastify[options.name].idp;
|
|
21
|
-
const tags = ['OIDC
|
|
22
|
+
const tags = ['OIDC-服务接口'];
|
|
23
|
+
const SERVICE_HINT = '供 central 子项目以 client_credentials 令牌(Bearer)调用';
|
|
22
24
|
|
|
23
25
|
// 头像、租户 logo 是主项目文件库的 id,子项目无法解析,额外给出可直接下载的地址
|
|
24
26
|
const resolveFileUrl = async fileId => {
|
|
@@ -52,7 +54,29 @@ module.exports = fp(async (fastify, options) => {
|
|
|
52
54
|
`${options.prefix}/service/user`,
|
|
53
55
|
{
|
|
54
56
|
onRequest: [authenticate.client('user:read')],
|
|
55
|
-
schema: {
|
|
57
|
+
schema: {
|
|
58
|
+
tags,
|
|
59
|
+
summary: '获取用户资料',
|
|
60
|
+
description: `${SERVICE_HINT},需要 user:read scope。用户不存在返回 404`,
|
|
61
|
+
query: { type: 'object', required: ['id'], properties: { id: { type: 'string', description: '用户 ID' } } },
|
|
62
|
+
response: success('用户资料', {
|
|
63
|
+
type: 'object',
|
|
64
|
+
additionalProperties: true,
|
|
65
|
+
properties: {
|
|
66
|
+
id: str('用户 ID'),
|
|
67
|
+
nickname: str('昵称'),
|
|
68
|
+
avatar: any('头像文件 ID(主项目文件库)'),
|
|
69
|
+
avatarUrl: str('头像可直接下载的地址,无法解析时为 null'),
|
|
70
|
+
email: str('邮箱'),
|
|
71
|
+
phone: str('手机号'),
|
|
72
|
+
gender: str('性别:F 女,M 男'),
|
|
73
|
+
birthday: any('出生日期'),
|
|
74
|
+
description: str('个人简介'),
|
|
75
|
+
status: any('用户状态:0 正常'),
|
|
76
|
+
isSuperAdmin: bool('是否为平台超级管理员;账号插件不支持判断时不返回')
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
}
|
|
56
80
|
},
|
|
57
81
|
wrap(async request => {
|
|
58
82
|
const { identity } = getIdp();
|
|
@@ -89,7 +113,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
89
113
|
return app;
|
|
90
114
|
};
|
|
91
115
|
|
|
92
|
-
const appQuery = { app: { type: 'string', description: '子应用 code,传入后只返回已开通该应用的数据,权限去掉 apps:<code>:
|
|
116
|
+
const appQuery = { app: { type: 'string', description: '子应用 code,传入后只返回已开通该应用的数据,权限去掉 apps:<code>: 前缀;必须是当前服务 client 注册的应用' } };
|
|
93
117
|
|
|
94
118
|
fastify.get(
|
|
95
119
|
`${options.prefix}/service/tenant-user`,
|
|
@@ -98,7 +122,22 @@ module.exports = fp(async (fastify, options) => {
|
|
|
98
122
|
schema: {
|
|
99
123
|
tags,
|
|
100
124
|
summary: '获取用户在租户内的身份、角色与权限',
|
|
101
|
-
|
|
125
|
+
description: `${SERVICE_HINT},需要 tenant:read scope。用户不是该租户成员(或租户未开通该应用)返回 404`,
|
|
126
|
+
query: {
|
|
127
|
+
type: 'object',
|
|
128
|
+
required: ['userId', 'tenantId'],
|
|
129
|
+
properties: Object.assign({ userId: { type: 'string', description: '用户 ID' }, tenantId: { type: 'string', description: '租户 ID' } }, appQuery)
|
|
130
|
+
},
|
|
131
|
+
response: success(
|
|
132
|
+
'租户成员信息(fastify-tenant getTenantUserInfo 原样返回,传 app 时 permissions 已去前缀)',
|
|
133
|
+
obj('租户成员信息', {
|
|
134
|
+
id: any('租户成员 ID'),
|
|
135
|
+
tenantId: any('租户 ID'),
|
|
136
|
+
permissions: any('权限 code 列表'),
|
|
137
|
+
roleDetails: any('角色详情列表'),
|
|
138
|
+
tenant: any('租户信息')
|
|
139
|
+
})
|
|
140
|
+
)
|
|
102
141
|
}
|
|
103
142
|
},
|
|
104
143
|
wrap(async request => {
|
|
@@ -120,7 +159,13 @@ module.exports = fp(async (fastify, options) => {
|
|
|
120
159
|
`${options.prefix}/service/tenant-list`,
|
|
121
160
|
{
|
|
122
161
|
onRequest: [authenticate.client('tenant:read')],
|
|
123
|
-
schema: {
|
|
162
|
+
schema: {
|
|
163
|
+
tags,
|
|
164
|
+
summary: '用户可用租户列表',
|
|
165
|
+
description: `${SERVICE_HINT},需要 tenant:read scope。传 app 且应用已停用时返回空列表`,
|
|
166
|
+
query: { type: 'object', required: ['userId'], properties: Object.assign({ userId: { type: 'string', description: '用户 ID' } }, appQuery) },
|
|
167
|
+
response: success('可用租户列表', tenantList)
|
|
168
|
+
}
|
|
124
169
|
},
|
|
125
170
|
wrap(async request => {
|
|
126
171
|
assertTenantEnabled();
|
|
@@ -139,20 +184,30 @@ module.exports = fp(async (fastify, options) => {
|
|
|
139
184
|
schema: {
|
|
140
185
|
tags,
|
|
141
186
|
summary: '子应用注册权限树到应用中心',
|
|
187
|
+
description: `${SERVICE_HINT},需要 app:register scope。同一 code 只能由首次注册的服务 client 更新;已删除的应用重新注册时恢复启用`,
|
|
142
188
|
body: {
|
|
143
189
|
type: 'object',
|
|
144
190
|
required: ['code'],
|
|
145
191
|
properties: {
|
|
146
|
-
code: { type: 'string' },
|
|
147
|
-
name: { type: 'string' },
|
|
148
|
-
description: { type: 'string' },
|
|
149
|
-
tenantMode: { type: 'string', enum: ['client', 'independent'] },
|
|
150
|
-
clientId: { type: 'string' },
|
|
151
|
-
resource: { type: 'string' },
|
|
152
|
-
permissions: { type: 'object', additionalProperties: true },
|
|
153
|
-
tenantSyncUri: { type: 'string' }
|
|
192
|
+
code: { type: 'string', description: '应用编码,只能包含字母、数字、_、-' },
|
|
193
|
+
name: { type: 'string', description: '应用名称,不传使用 code' },
|
|
194
|
+
description: { type: 'string', description: '应用描述' },
|
|
195
|
+
tenantMode: { type: 'string', enum: ['client', 'independent'], description: '租户模式:client 租户由宿主提供(默认),independent 子应用本地租户' },
|
|
196
|
+
clientId: { type: 'string', description: '子应用登录用的 client_id' },
|
|
197
|
+
resource: { type: 'string', description: '子应用资源服务 identifier(access_token 的 aud)' },
|
|
198
|
+
permissions: { type: 'object', additionalProperties: true, description: '权限树(不含 apps 前缀),不传视为空树 { modules: [] }' },
|
|
199
|
+
tenantSyncUri: { type: 'string', description: 'independent 模式接收租户变更通知的地址,client 模式忽略' }
|
|
154
200
|
}
|
|
155
|
-
}
|
|
201
|
+
},
|
|
202
|
+
response: success('注册结果', {
|
|
203
|
+
type: 'object',
|
|
204
|
+
properties: {
|
|
205
|
+
code: str('应用编码'),
|
|
206
|
+
status: str('应用状态:open 启用,closed 停用'),
|
|
207
|
+
permissionsHash: str('权限树摘要'),
|
|
208
|
+
changed: bool('权限树是否有变化(新注册、恢复或权限树变更)')
|
|
209
|
+
}
|
|
210
|
+
})
|
|
156
211
|
}
|
|
157
212
|
},
|
|
158
213
|
wrap(async request => services.app.register(Object.assign({}, request.body, { serviceClientId: request.user.clientId })))
|
|
@@ -165,7 +220,50 @@ module.exports = fp(async (fastify, options) => {
|
|
|
165
220
|
schema: {
|
|
166
221
|
tags,
|
|
167
222
|
summary: '租户快照(基本信息、租户权限、角色、组织),供 independent 子应用接管',
|
|
168
|
-
|
|
223
|
+
description: `${SERVICE_HINT},需要 tenant:sync scope。id 均为宿主侧 id;租户未开通应用时 enabled=false 且不返回权限、角色、组织;宿主已删除租户时 removed=true`,
|
|
224
|
+
query: { type: 'object', required: ['tenantId', 'app'], properties: Object.assign({ tenantId: { type: 'string', description: '宿主租户 ID' } }, appQuery) },
|
|
225
|
+
response: success('租户快照', {
|
|
226
|
+
type: 'object',
|
|
227
|
+
additionalProperties: true,
|
|
228
|
+
properties: {
|
|
229
|
+
tenant: obj('租户基本信息', {
|
|
230
|
+
id: str('租户 ID'),
|
|
231
|
+
name: str('租户名称'),
|
|
232
|
+
status: any('租户状态'),
|
|
233
|
+
logo: any('租户 logo 文件 ID(宿主文件库)'),
|
|
234
|
+
companyName: str('公司名称'),
|
|
235
|
+
company: any('公司信息')
|
|
236
|
+
}),
|
|
237
|
+
enabled: bool('租户是否已开通且应用处于启用状态'),
|
|
238
|
+
removed: bool('宿主是否已删除该租户;未删除时不返回'),
|
|
239
|
+
permissions: any('租户已开通的应用权限 code(已去前缀)'),
|
|
240
|
+
roles: list(
|
|
241
|
+
'角色列表',
|
|
242
|
+
obj('角色', {
|
|
243
|
+
id: str('角色 ID'),
|
|
244
|
+
code: str('角色 code,没有 code 时为 role-<id>'),
|
|
245
|
+
name: any('角色名称'),
|
|
246
|
+
description: any('角色描述'),
|
|
247
|
+
type: any('角色类型'),
|
|
248
|
+
status: any('角色状态'),
|
|
249
|
+
permissions: any('角色权限 code(已去前缀)')
|
|
250
|
+
})
|
|
251
|
+
),
|
|
252
|
+
orgs: list(
|
|
253
|
+
'组织列表',
|
|
254
|
+
obj('组织', {
|
|
255
|
+
id: str('组织 ID'),
|
|
256
|
+
parentId: str('上级组织 ID,根组织为 null'),
|
|
257
|
+
name: any('组织名称'),
|
|
258
|
+
description: any('组织描述'),
|
|
259
|
+
index: any('排序'),
|
|
260
|
+
status: any('组织状态'),
|
|
261
|
+
leaderTenantUserId: str('负责人租户成员 ID')
|
|
262
|
+
})
|
|
263
|
+
),
|
|
264
|
+
fileUrls: obj('快照中图片文件的下载地址:{ 宿主文件 id: 地址 },解析不到的不返回;租户已删除时不返回')
|
|
265
|
+
}
|
|
266
|
+
})
|
|
169
267
|
}
|
|
170
268
|
},
|
|
171
269
|
wrap(async request => {
|
|
@@ -186,11 +284,41 @@ module.exports = fp(async (fastify, options) => {
|
|
|
186
284
|
schema: {
|
|
187
285
|
tags,
|
|
188
286
|
summary: '租户成员分页(含角色 code 与所属组织),供 independent 子应用接管',
|
|
287
|
+
description: `${SERVICE_HINT},需要 tenant:sync scope。只返回已关联账号的成员,按加入时间正序;应用停用或租户未开通时返回空列表`,
|
|
189
288
|
query: {
|
|
190
289
|
type: 'object',
|
|
191
290
|
required: ['tenantId', 'app'],
|
|
192
|
-
properties: Object.assign(
|
|
193
|
-
|
|
291
|
+
properties: Object.assign(
|
|
292
|
+
{
|
|
293
|
+
tenantId: { type: 'string', description: '宿主租户 ID' },
|
|
294
|
+
currentPage: { type: 'number', default: 1, description: '页码,从 1 开始' },
|
|
295
|
+
perPage: { type: 'number', default: 200, maximum: 1000, description: '每页条数,最大 1000' }
|
|
296
|
+
},
|
|
297
|
+
appQuery
|
|
298
|
+
)
|
|
299
|
+
},
|
|
300
|
+
response: success('租户成员分页', {
|
|
301
|
+
type: 'object',
|
|
302
|
+
additionalProperties: true,
|
|
303
|
+
properties: {
|
|
304
|
+
pageData: list(
|
|
305
|
+
'当前页成员',
|
|
306
|
+
obj('租户成员', {
|
|
307
|
+
id: str('租户成员 ID'),
|
|
308
|
+
userId: str('账号用户 ID'),
|
|
309
|
+
name: any('成员姓名'),
|
|
310
|
+
email: any('邮箱'),
|
|
311
|
+
phone: any('手机号'),
|
|
312
|
+
avatar: any('头像文件 ID(宿主文件库)'),
|
|
313
|
+
status: any('成员状态'),
|
|
314
|
+
roleCodes: any('角色 code 列表'),
|
|
315
|
+
tenantOrgIds: any('所属组织 ID 列表')
|
|
316
|
+
})
|
|
317
|
+
),
|
|
318
|
+
totalCount: num('成员总数'),
|
|
319
|
+
fileUrls: obj('头像下载地址:{ 宿主文件 id: 地址 };返回空列表时不返回')
|
|
320
|
+
}
|
|
321
|
+
})
|
|
194
322
|
}
|
|
195
323
|
},
|
|
196
324
|
wrap(async request => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
+
const { str, obj, success } = require('../schema/response');
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* 宿主租户端:当前租户已开通且启用的子应用。当前租户与 fastify-tenant 的 tenantUser 一致(token 中的租户,否则默认租户)
|
|
@@ -15,7 +16,20 @@ module.exports = fp(async (fastify, options) => {
|
|
|
15
16
|
`${options.prefix}/tenant/apps`,
|
|
16
17
|
{
|
|
17
18
|
onRequest: [authenticate.user, resolveTenantUser],
|
|
18
|
-
schema: {
|
|
19
|
+
schema: {
|
|
20
|
+
tags: ['OIDC-应用中心'],
|
|
21
|
+
summary: '当前租户已开通的子应用',
|
|
22
|
+
description: '宿主租户端使用:返回已启用且当前租户已开通权限的子应用;没有当前租户时返回空数组',
|
|
23
|
+
response: success('子应用列表', {
|
|
24
|
+
type: 'array',
|
|
25
|
+
items: obj('子应用', {
|
|
26
|
+
code: str('应用编码'),
|
|
27
|
+
name: str('应用名称'),
|
|
28
|
+
description: str('描述'),
|
|
29
|
+
tenantMode: str('租户模式:client / independent')
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
}
|
|
19
33
|
},
|
|
20
34
|
wrap(async request => {
|
|
21
35
|
const tenantId = request[options.tenantUserContextName]?.tenantId;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
+
const { str, any, list, obj, success } = require('../schema/response');
|
|
2
3
|
|
|
3
4
|
const FORWARD_HEADERS = ['content-type', 'accept', 'accept-language', 'x-app-name'];
|
|
4
5
|
|
|
@@ -39,26 +40,93 @@ module.exports = fp(async (fastify, options) => {
|
|
|
39
40
|
defaultTenantId
|
|
40
41
|
});
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
const tags = ['OIDC-租户代理'];
|
|
44
|
+
|
|
45
|
+
fastify.get(
|
|
46
|
+
`${proxy.prefix}/getUserInfo`,
|
|
47
|
+
{
|
|
48
|
+
onRequest: [authenticate.user, authenticate.tenantUser],
|
|
49
|
+
schema: {
|
|
50
|
+
tags,
|
|
51
|
+
summary: '获取当前用户与当前租户信息',
|
|
52
|
+
description: 'client 子应用使用,与 fastify-tenant getUserInfo 同形;租户信息经服务接口获取,权限已去掉 apps:<code>: 前缀',
|
|
53
|
+
response: success('当前用户与租户信息', {
|
|
54
|
+
type: 'object',
|
|
55
|
+
additionalProperties: true,
|
|
56
|
+
properties: {
|
|
57
|
+
userInfo: any('当前用户'),
|
|
58
|
+
tenantUserInfo: any('当前租户成员信息'),
|
|
59
|
+
company: any('当前租户的公司信息'),
|
|
60
|
+
tenant: any('当前租户')
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
async request => {
|
|
66
|
+
const info = request[options.tenantUserContextName];
|
|
67
|
+
return { userInfo: request.userInfo, tenantUserInfo: info, company: info?.tenant?.tenantCompany || info?.tenant?.company || null, tenant: info?.tenant || null };
|
|
68
|
+
}
|
|
69
|
+
);
|
|
46
70
|
|
|
47
|
-
fastify.get(
|
|
71
|
+
fastify.get(
|
|
72
|
+
`${proxy.prefix}/available-list`,
|
|
73
|
+
{
|
|
74
|
+
onRequest: [authenticate.user],
|
|
75
|
+
schema: {
|
|
76
|
+
tags,
|
|
77
|
+
summary: '当前用户可用租户列表',
|
|
78
|
+
description: 'client 子应用使用,经服务接口获取(只含已开通本应用的租户),与 fastify-tenant available-list 同形',
|
|
79
|
+
response: success('可用租户列表', {
|
|
80
|
+
type: 'object',
|
|
81
|
+
additionalProperties: true,
|
|
82
|
+
properties: {
|
|
83
|
+
list: list(
|
|
84
|
+
'可用租户',
|
|
85
|
+
obj('租户成员身份', {
|
|
86
|
+
id: str('用户在该租户的成员 ID'),
|
|
87
|
+
tenantId: str('租户 ID'),
|
|
88
|
+
status: str('成员状态,固定 open'),
|
|
89
|
+
tenant: obj('租户', {
|
|
90
|
+
id: str('租户 ID'),
|
|
91
|
+
name: str('租户名称'),
|
|
92
|
+
logo: any('租户 logo 文件 ID'),
|
|
93
|
+
status: str('租户状态,固定 open'),
|
|
94
|
+
company: obj('公司,没有时为 null', { name: str('公司名称') })
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
),
|
|
98
|
+
defaultTenantId: str('默认租户 ID')
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
async request => toTenantList(await serviceClient.getTenantList({ userId: request.user.userId }))
|
|
104
|
+
);
|
|
48
105
|
|
|
49
|
-
fastify.all(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
106
|
+
fastify.all(
|
|
107
|
+
`${proxy.prefix}/*`,
|
|
108
|
+
{
|
|
109
|
+
onRequest: [authenticate.user],
|
|
110
|
+
schema: {
|
|
111
|
+
tags,
|
|
112
|
+
summary: '转发到宿主 fastify-tenant',
|
|
113
|
+
description: '以 token exchange 把当前令牌换成宿主 API 令牌后,原样转发 JSON 请求(路径、query、body)到宿主 /api/tenant,按宿主响应的状态码与内容返回'
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
async (request, reply) => {
|
|
117
|
+
const subjectToken = String(request.headers.authorization || '').replace(/^(bearer|dpop)\s+/i, '');
|
|
118
|
+
const headers = { authorization: `Bearer ${await getHostToken(subjectToken)}` };
|
|
119
|
+
FORWARD_HEADERS.forEach(name => request.headers[name] && (headers[name] = request.headers[name]));
|
|
120
|
+
const url = new URL(`${target}/${request.params['*']}`);
|
|
121
|
+
url.search = new URL(request.raw.url, 'http://localhost').search;
|
|
122
|
+
const hasBody = !['GET', 'HEAD'].includes(request.method) && request.body !== undefined;
|
|
123
|
+
const response = await fetch(url, { method: request.method, headers, body: hasBody ? JSON.stringify(request.body) : undefined });
|
|
124
|
+
reply.code(response.status);
|
|
125
|
+
const contentType = response.headers.get('content-type');
|
|
126
|
+
if (contentType) {
|
|
127
|
+
reply.header('content-type', contentType);
|
|
128
|
+
}
|
|
129
|
+
return reply.send(Buffer.from(await response.arrayBuffer()));
|
|
61
130
|
}
|
|
62
|
-
|
|
63
|
-
});
|
|
131
|
+
);
|
|
64
132
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
2
|
const httpErrors = require('http-errors');
|
|
3
3
|
const { createError } = require('../utils/intl');
|
|
4
|
+
const { empty } = require('../schema/response');
|
|
4
5
|
|
|
5
6
|
const { BadRequest } = httpErrors;
|
|
6
7
|
|
|
@@ -35,9 +36,11 @@ module.exports = fp(async (fastify, options) => {
|
|
|
35
36
|
`${options.prefix}/tenant-sync`,
|
|
36
37
|
{
|
|
37
38
|
schema: {
|
|
38
|
-
tags: ['OIDC
|
|
39
|
+
tags: ['OIDC-服务接口'],
|
|
39
40
|
summary: '接收宿主租户变更通知(independent 子应用)',
|
|
40
|
-
|
|
41
|
+
description: '验签并校验 jti 防重放后立即返回,后台回拉该租户快照;令牌无效或重放返回 400',
|
|
42
|
+
body: { type: 'object', required: ['tenant_sync_token'], properties: { tenant_sync_token: { type: 'string', description: '宿主签发的租户同步令牌(JWT),含 tenant_id' } } },
|
|
43
|
+
response: empty('已接收,后台同步')
|
|
41
44
|
}
|
|
42
45
|
},
|
|
43
46
|
translator.wrap(async (request, reply) => {
|
|
@@ -107,8 +107,36 @@ module.exports = async ({ fastify, options, identity, translator, jwks, getLocal
|
|
|
107
107
|
return interactionPolicy.Check.REQUEST_PROMPT;
|
|
108
108
|
});
|
|
109
109
|
|
|
110
|
+
// 在 tenant 之后执行:tenantCheck 已把本次选定的租户写入 sessionTenant
|
|
111
|
+
const mfaCheck = new interactionPolicy.Check('mfa_required', '需要多因素认证', async ctx => {
|
|
112
|
+
const { oidc } = ctx;
|
|
113
|
+
const accountId = oidc.session.accountId;
|
|
114
|
+
if (!accountId || !identity.tenantEnabled()) {
|
|
115
|
+
return interactionPolicy.Check.NO_NEED_TO_PROMPT;
|
|
116
|
+
}
|
|
117
|
+
// 本次授权刚完成验证(maxAge 为 0 时也不会重复要求)
|
|
118
|
+
if (oidc.result?.mfa && String(oidc.result.mfa.accountId) === String(accountId)) {
|
|
119
|
+
return interactionPolicy.Check.NO_NEED_TO_PROMPT;
|
|
120
|
+
}
|
|
121
|
+
const sessionUid = oidc.session.uid;
|
|
122
|
+
const current = await services.sessionTenant.get({ sessionUid });
|
|
123
|
+
if (!current || current.accountId !== String(accountId) || !(await identity.isTenantMember(accountId, current.tenantId, services.app.scopeOfClient(oidc.client.clientId)))) {
|
|
124
|
+
return interactionPolicy.Check.NO_NEED_TO_PROMPT;
|
|
125
|
+
}
|
|
126
|
+
const mfa = await identity.getTenantMfa(current.tenantId);
|
|
127
|
+
if (!mfa.enabled) {
|
|
128
|
+
return interactionPolicy.Check.NO_NEED_TO_PROMPT;
|
|
129
|
+
}
|
|
130
|
+
const verifiedAt = await services.mfa.getSessionVerifiedAt({ sessionUid, accountId });
|
|
131
|
+
if (verifiedAt && Date.now() - new Date(verifiedAt).getTime() <= mfa.maxAge * 1000) {
|
|
132
|
+
return interactionPolicy.Check.NO_NEED_TO_PROMPT;
|
|
133
|
+
}
|
|
134
|
+
return interactionPolicy.Check.REQUEST_PROMPT;
|
|
135
|
+
});
|
|
136
|
+
|
|
110
137
|
const policy = interactionPolicy.base();
|
|
111
138
|
policy.add(new interactionPolicy.Prompt({ name: 'tenant', requestable: false }, ctx => ({ tenant_id: ctx.oidc.params.tenant_id }), tenantCheck), 1);
|
|
139
|
+
policy.add(new interactionPolicy.Prompt({ name: 'mfa', requestable: false }, () => ({}), mfaCheck), 2);
|
|
112
140
|
|
|
113
141
|
const ttl = options.ttl;
|
|
114
142
|
const configuration = {
|
|
@@ -119,7 +147,8 @@ module.exports = async ({ fastify, options, identity, translator, jwks, getLocal
|
|
|
119
147
|
keys: [deriveKey(options.cookieSecret || options.keyEncryptionSecret, 'cookie').toString('base64url')]
|
|
120
148
|
},
|
|
121
149
|
claims: {
|
|
122
|
-
openid
|
|
150
|
+
// amr 归到 openid scope 才会写进 id_token,RP 据此判断本次登录是否做过 MFA(含 sms、mfa)
|
|
151
|
+
openid: ['sub', 'amr'],
|
|
123
152
|
profile: ['name', 'nickname', 'picture', 'gender', 'birthdate'],
|
|
124
153
|
email: ['email'],
|
|
125
154
|
phone: ['phone_number']
|
|
@@ -280,6 +309,7 @@ module.exports = async ({ fastify, options, identity, translator, jwks, getLocal
|
|
|
280
309
|
}
|
|
281
310
|
if (!(await getAdapter('Session').findByUid(session.uid))) {
|
|
282
311
|
await services.sessionTenant.remove({ sessionUid: session.uid });
|
|
312
|
+
await services.mfa.removeSession({ sessionUid: session.uid });
|
|
283
313
|
}
|
|
284
314
|
} catch (e) {
|
|
285
315
|
fastify.log.warn({ err: e }, 'fastify-oidc: 登出后清理会话失败');
|