@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 CHANGED
@@ -53,7 +53,7 @@ npm i --save @kne/fastify-oidc
53
53
  | 环节 | 实现 | 说明 |
54
54
  |------|------|------|
55
55
  | 协议端点 | `libs/idp/mount.js` | 把 `mountPath`(默认 `/oidc`)下的请求原样交给 oidc-provider |
56
- | 交互 | `libs/services/interaction.js` | 登录、选择租户、授权确认、取消,返回 `{ redirectTo }` 由前端跳转 |
56
+ | 交互 | `libs/services/interaction.js` | 登录、选择租户、短信 MFA、授权确认、取消,返回 `{ redirectTo }` 由前端跳转 |
57
57
  | 账号 / 租户桥接 | `libs/idp/identity.js` | 唯一依赖 fastify-account / fastify-tenant 的地方 |
58
58
  | 资源侧鉴权 | `libs/resource/*` | JWT 验签、DPoP、撤销列表、`request.user` 归一化、legacy token 回退 |
59
59
  | 管理 | `libs/controllers/admin.js` | client、资源服务、签名密钥、会话 |
@@ -87,6 +87,21 @@ client 只能申请 `allowedResources` 中的资源;子项目 token 的 `aud`
87
87
 
88
88
  token 中的租户相关 claim 以命名空间为前缀(默认 `${ORIGIN}/`,central 模式为 issuer 的 origin):`tenant_id`、`tenant_user_id`、`roles`、`permissions`(按资源服务 `includePermissions` 决定是否携带)。
89
89
 
90
+ ##### 租户短信 MFA
91
+
92
+ 租户在 fastify-tenant 的 MFA 设置(`/api/tenant/mfa-setting`)中开启后,`tenant` 之后会出现 `mfa` 步骤,前端调用 `mfa/send` 发码、`mfa/verify` 校验:
93
+
94
+ | 情况 | 行为 |
95
+ |------|------|
96
+ | 租户未开启 MFA,或宿主 fastify-tenant 版本没有 MFA 设置 | 跳过 |
97
+ | 本会话距上次 MFA 验证未超过租户的 `maxAge`(秒) | 跳过(同一会话切到另一个开启 MFA 的租户时,按该租户的 `maxAge` 判断) |
98
+ | 超过 `maxAge` 或从未验证 | 前端展示短信验证(`prompt=none` 时返回 `interaction_required`) |
99
+ | 账号没有手机号 | `mfa/send` 以 `access_denied` 结束授权,需管理员先给账号绑定手机号 |
100
+
101
+ - 验证码通过 fastify-account 的 `sendVerificationCode` 发送(`type` 默认 `6`),宿主的 `sendMessage` 必须处理该类型的短信,否则验证码发不出去
102
+ - 验证码绑定交互、只能使用一次,错误 5 次锁定,60 秒内不能重发(可通过 `mfa` 选项调整)
103
+ - 通过后 id_token 的 `amr` 为 `['pwd', 'sms', 'mfa']`,RP 可据此判断;已签发的 refresh_token 不受之后开启的 MFA 影响
104
+
90
105
  ##### 令牌撤销
91
106
 
92
107
  JWT access_token 无法真正撤销,插件在资源侧维护一个撤销列表:
@@ -362,6 +377,15 @@ oidc-provider 9.x 为 ESM 包,本插件通过动态 `import()` 加载,要求
362
377
 
363
378
  `getAuthenticate` 默认:`*:manage` 返回 `[authenticate.user, authenticate.admin]`,其余返回 `[authenticate.user]`。
364
379
 
380
+ `mfa` 默认值(租户短信 MFA,是否开启与有效期由 fastify-tenant 的租户 MFA 设置决定,见「租户短信 MFA」):
381
+
382
+ | 键 | 默认值 | 说明 |
383
+ |----|--------|------|
384
+ | codeTTL | `300` | 验证码有效期(秒) |
385
+ | maxAttempts | `5` | 同一验证码最多错误次数,达到后锁定,需重新发送 |
386
+ | resendInterval | `60` | 重新发送的最小间隔(秒) |
387
+ | smsType | `6` | 调用 fastify-account `sendVerificationCode` 时的 `type`,宿主的 `sendMessage` 需处理该类型的短信 |
388
+
365
389
  ##### 资源侧
366
390
 
367
391
  | 属性名 | 类型 | 必填 | 默认值 | 说明 |
@@ -463,7 +487,7 @@ oidc-provider 的交互地址,303 跳转到 `${ORIGIN}${interactionPage}?uid={
463
487
  }
464
488
  ```
465
489
 
466
- `prompt.name` 为 `login` / `tenant` / `consent`;已登录时 `user` 为 `{ id, nickname, avatar, email, phone }`;`tenant` 步骤时 `tenants` 为 `{ list: [{ tenantId, tenantUserId, name, logo, companyName }], defaultTenantId }`。
490
+ `prompt.name` 为 `login` / `tenant` / `mfa` / `consent`;已登录时 `user` 为 `{ id, nickname, avatar, email, phone }`;`tenant` 步骤时 `tenants` 为 `{ list: [{ tenantId, tenantUserId, name, logo, companyName }], defaultTenantId }`;`mfa` 步骤时 `mfa` 为 `{ method: 'sms', tenantId, maxAge, phoneMissing, phoneMasked, resendAfter }`。
467
491
 
468
492
  ###### POST /api/oidc/interaction/{uid}/login
469
493
 
@@ -480,6 +504,14 @@ oidc-provider 的交互地址,303 跳转到 `${ORIGIN}${interactionPage}?uid={
480
504
 
481
505
  参数 `tenantId`(必填)。只能选择当前用户所属且开启的租户。
482
506
 
507
+ ###### POST /api/oidc/interaction/{uid}/mfa/send
508
+
509
+ `mfa` 步骤发送短信验证码到账号绑定的手机号,返回 `{ phoneMasked, expiresIn, resendAfter }`;发送间隔内重复调用返回 400。账号没有手机号时以 `access_denied` 结束交互,返回 `{ redirectTo }`。
510
+
511
+ ###### POST /api/oidc/interaction/{uid}/mfa/verify
512
+
513
+ 参数 `code`(必填)。验证码错误、过期或错误次数过多返回 400;通过后返回 `{ redirectTo }`,本次登录的 id_token `amr` 为 `['pwd', 'sms', 'mfa']`,`auth_time` 保持原登录时间。
514
+
483
515
  ###### POST /api/oidc/interaction/{uid}/confirm
484
516
 
485
517
  确认授权(`skip_consent: false` 的 client 才会出现 consent 步骤)。
@@ -712,6 +744,29 @@ central 模式下主项目的 `avatar` 是主项目文件库的 id,子项目
712
744
  | accountId | string | 用户 id |
713
745
  | tenantId | string | 会话当前租户 |
714
746
 
747
+ ##### sessionMfa(`t_oidc_session_mfa`)
748
+
749
+ | 属性名 | 类型 | 说明 |
750
+ |--------|------|------|
751
+ | sessionUid | string | IdP 会话 uid(唯一) |
752
+ | accountId | string | 用户 id |
753
+ | method | string | 验证方式,如 `sms` |
754
+ | verifiedAt | Date | 本会话最近一次通过 MFA 的时间 |
755
+
756
+ ##### mfaChallenge(`t_oidc_mfa_challenge`)
757
+
758
+ | 属性名 | 类型 | 说明 |
759
+ |--------|------|------|
760
+ | interactionUid | string | 交互 uid(唯一),验证码只在该次交互内有效 |
761
+ | accountId | string | 用户 id |
762
+ | method | string | 验证方式 |
763
+ | codeHash | string | 验证码 HMAC,不存明文 |
764
+ | attempts | number | 已错误次数 |
765
+ | status | string | `pending` 待验证 / `verified` 已使用 / `locked` 错误次数过多 |
766
+ | sentAt / expiresAt | Date | 最近发送时间 / 过期时间 |
767
+
768
+ 新表由 `sequelize.sync()` 创建,无需迁移脚本。
769
+
715
770
  ##### app(`t_oidc_app`)
716
771
 
717
772
  | 属性名 | 类型 | 说明 |
package/index.js CHANGED
@@ -85,6 +85,7 @@ module.exports = fp(
85
85
  options
86
86
  );
87
87
  options.ttl = Object.assign({ accessToken: 10 * 60, idToken: 60 * 60, refreshToken: 14 * DAY, interaction: 10 * 60, session: 14 * DAY, grant: 14 * DAY }, options.ttl);
88
+ options.mfa = Object.assign({ codeTTL: 5 * 60, maxAttempts: 5, resendInterval: 60, smsType: 6 }, options.mfa);
88
89
  options.serviceClient = Object.assign({ clientId: env.OIDC_SERVICE_CLIENT_ID, clientSecret: env.OIDC_SERVICE_CLIENT_SECRET }, options.serviceClient);
89
90
  // 代码里的 app 作默认值(如 tenantMode),部署时宿主注入的环境变量优先;最终没有 code 视为未作为子应用部署
90
91
  const envApp = pickBy({ code: env.OIDC_APP_CODE, name: env.OIDC_APP_NAME, tenantMode: env.OIDC_APP_TENANT_MODE, tenantSyncUri: env.OIDC_APP_TENANT_SYNC_URI });
@@ -1,18 +1,27 @@
1
1
  const fp = require('fastify-plugin');
2
+ const { str, num, any, list, obj, success, empty, pageResult, client, resourceServer, app, key } = require('../schema/response');
2
3
 
3
- const idSchema = { type: 'object', required: ['id'], properties: { id: { type: 'string' } } };
4
+ const idSchema = { type: 'object', required: ['id'], properties: { id: { type: 'string', description: '记录 ID' } } };
4
5
  const listQuery = {
5
6
  type: 'object',
6
7
  properties: {
7
- filter: { type: 'object', additionalProperties: true },
8
- perPage: { type: 'number', default: 20 },
9
- currentPage: { type: 'number', default: 1 }
8
+ filter: {
9
+ type: 'object',
10
+ additionalProperties: true,
11
+ description: '筛选条件',
12
+ properties: {
13
+ keyword: { description: '关键字,模糊匹配编码 / 名称' },
14
+ status: { description: '状态:open 启用,closed 停用' }
15
+ }
16
+ },
17
+ perPage: { type: 'number', default: 20, description: '每页条数' },
18
+ currentPage: { type: 'number', default: 1, description: '页码,从 1 开始' }
10
19
  }
11
20
  };
12
21
  const statusBody = {
13
22
  type: 'object',
14
23
  required: ['id', 'status'],
15
- properties: { id: { type: 'string' }, status: { type: 'string', enum: ['open', 'closed'] } }
24
+ properties: { id: { type: 'string', description: '记录 ID' }, status: { type: 'string', enum: ['open', 'closed'], description: '目标状态:open 启用,closed 停用' } }
16
25
  };
17
26
 
18
27
  module.exports = fp(async (fastify, options) => {
@@ -26,50 +35,57 @@ module.exports = fp(async (fastify, options) => {
26
35
  const clientBody = {
27
36
  type: 'object',
28
37
  properties: {
29
- id: { type: 'string' },
30
- clientId: { type: 'string' },
31
- clientName: { type: 'string' },
32
- clientSecret: { type: 'string' },
33
- metadata: { type: 'object', additionalProperties: true },
34
- allowedResources: { type: 'array', items: { type: 'string' } },
35
- description: { type: 'string' }
38
+ id: { type: 'string', description: '记录 ID,修改时必填,创建时忽略' },
39
+ clientId: { type: 'string', description: 'client_id,仅创建时生效,不传随机生成;不能与已有 client 重复' },
40
+ clientName: { type: 'string', description: '应用名称;修改时不传保持原值' },
41
+ clientSecret: { type: 'string', description: 'client secret,仅创建 confidential client 时生效,不传随机生成;修改请用重置 secret 接口' },
42
+ metadata: {
43
+ type: 'object',
44
+ additionalProperties: true,
45
+ description: 'OIDC client metadata(redirect_uris、post_logout_redirect_uris、grant_types、response_types、token_endpoint_auth_method 等,snake_case),未传的字段使用 public SPA 默认值;修改时传入则整体替换,不传保持原值'
46
+ },
47
+ allowedResources: { type: 'array', items: { type: 'string' }, description: '允许申请的资源服务 identifier 列表;修改时不传保持原值' },
48
+ description: { type: 'string', description: '描述;修改时不传保持原值' }
36
49
  }
37
50
  };
38
51
 
39
52
  const resourceServerBody = {
40
53
  type: 'object',
41
54
  properties: {
42
- id: { type: 'string' },
43
- identifier: { type: 'string' },
44
- name: { type: 'string' },
45
- scope: { type: 'string' },
46
- accessTokenTTL: { type: 'number' },
47
- includePermissions: { type: 'boolean' },
48
- description: { type: 'string' }
55
+ id: { type: 'string', description: '记录 ID,修改时必填,创建时忽略' },
56
+ identifier: { type: 'string', description: 'resource indicator(URL),即 access_token 的 aud;仅创建时生效,不能与已有资源服务重复' },
57
+ name: { type: 'string', description: '资源名称' },
58
+ scope: { type: 'string', description: '支持的 scope,空格分隔;为空使用默认 scope(defaultResourceScope)' },
59
+ accessTokenTTL: { type: 'number', description: 'access_token 有效期(秒),为空使用全局配置' },
60
+ includePermissions: { type: 'boolean', description: 'access_token 中是否携带 permissions,为空使用全局配置' },
61
+ description: { type: 'string', description: '描述' }
49
62
  }
50
63
  };
51
64
 
52
- const crud = (path, tag, service, body) => {
65
+ const crud = ({ path, tag, name, service, body, item, createDescription, saveDescription }) => {
53
66
  const tags = [tag];
54
67
  const onRequest = options.getAuthenticate(`${path}:manage`);
55
68
  fastify.get(
56
69
  `${admin}/${path}/list`,
57
- { onRequest, schema: { tags, summary: `${tag}列表`, query: listQuery } },
70
+ {
71
+ onRequest,
72
+ schema: { tags, summary: `${name}列表`, description: '分页查询,按创建时间倒序', query: listQuery, response: success(`${name}分页列表`, pageResult(name, item)) }
73
+ },
58
74
  wrap(async request => service().list(request.query))
59
75
  );
60
76
  fastify.get(
61
77
  `${admin}/${path}/detail`,
62
- { onRequest, schema: { tags, summary: `${tag}详情`, query: idSchema } },
78
+ { onRequest, schema: { tags, summary: `${name}详情`, query: idSchema, response: success(`${name}详情`, Object.assign({}, item, { nullable: false })) } },
63
79
  wrap(async request => service().detail(request.query))
64
80
  );
65
81
  fastify.post(
66
82
  `${admin}/${path}/create`,
67
- { onRequest, schema: { tags, summary: `创建${tag}`, body } },
83
+ { onRequest, schema: { tags, summary: `创建${name}`, description: createDescription, body, response: success(`创建后的${name}`, Object.assign({}, item, { nullable: false })) } },
68
84
  wrap(async request => service().create(request.body))
69
85
  );
70
86
  fastify.post(
71
87
  `${admin}/${path}/save`,
72
- { onRequest, schema: { tags, summary: `修改${tag}`, body: Object.assign({}, body, { required: ['id'] }) } },
88
+ { onRequest, schema: { tags, summary: `修改${name}`, description: saveDescription, body: Object.assign({}, body, { required: ['id'] }), response: empty('保存成功') } },
73
89
  wrap(async request => {
74
90
  await service().save(request.body);
75
91
  return {};
@@ -77,7 +93,7 @@ module.exports = fp(async (fastify, options) => {
77
93
  );
78
94
  fastify.post(
79
95
  `${admin}/${path}/set-status`,
80
- { onRequest, schema: { tags, summary: `启用 / 停用${tag}`, body: statusBody } },
96
+ { onRequest, schema: { tags, summary: `启用 / 停用${name}`, description: `停用后该${name}不再参与 OIDC 授权`, body: statusBody, response: empty('设置成功') } },
81
97
  wrap(async request => {
82
98
  await service().setStatus(request.body);
83
99
  return {};
@@ -85,7 +101,7 @@ module.exports = fp(async (fastify, options) => {
85
101
  );
86
102
  fastify.post(
87
103
  `${admin}/${path}/remove`,
88
- { onRequest, schema: { tags, summary: `删除${tag}`, body: idSchema } },
104
+ { onRequest, schema: { tags, summary: `删除${name}`, description: `物理删除该${name}`, body: idSchema, response: empty('删除成功') } },
89
105
  wrap(async request => {
90
106
  await service().remove(request.body);
91
107
  return {};
@@ -93,25 +109,74 @@ module.exports = fp(async (fastify, options) => {
93
109
  );
94
110
  };
95
111
 
96
- crud('client', 'OIDC Client', () => services.client, clientBody);
97
- crud('resource-server', 'OIDC 资源服务', () => services.resourceServer, resourceServerBody);
112
+ crud({
113
+ path: 'client',
114
+ tag: 'OIDC管理后台-Client',
115
+ name: 'OIDC Client',
116
+ service: () => services.client,
117
+ body: clientBody,
118
+ item: client,
119
+ createDescription: '创建接入应用。metadata 会经 oidc-provider 校验;confidential client(token_endpoint_auth_method 不为 none)返回中额外带一次 clientSecret 明文,之后不再返回',
120
+ saveDescription: '修改接入应用;clientId 与 clientSecret 不可通过本接口修改。改为 public client 时清除 secret,改为 confidential 且原来没有 secret 时自动生成(不返回明文,需再调用重置 secret)'
121
+ });
122
+ crud({
123
+ path: 'resource-server',
124
+ tag: 'OIDC管理后台-资源服务',
125
+ name: '资源服务',
126
+ service: () => services.resourceServer,
127
+ body: resourceServerBody,
128
+ item: resourceServer,
129
+ createDescription: '注册一个可申请 access_token 的资源服务(API),identifier 必须是合法 URL',
130
+ saveDescription: '修改资源服务;identifier 不可修改,未传的字段保持原值;scope 未传或为空时重置为默认 scope'
131
+ });
98
132
 
99
133
  // 应用由子应用通过服务接口注册,管理端只能查看、启停与删除
100
- const appTags = ['OIDC 应用中心'];
134
+ const appTags = ['OIDC管理后台-子应用'];
101
135
  const appOnRequest = options.getAuthenticate('app:manage');
136
+ const appListQuery = Object.assign({}, listQuery, {
137
+ properties: Object.assign({}, listQuery.properties, {
138
+ filter: Object.assign({}, listQuery.properties.filter, {
139
+ properties: Object.assign({}, listQuery.properties.filter.properties, {
140
+ status: { description: '状态:open 启用,closed 停用;不传返回未删除的全部应用' },
141
+ tenantMode: { description: '租户模式:client / independent' }
142
+ })
143
+ })
144
+ })
145
+ });
102
146
  fastify.get(
103
147
  `${admin}/app/list`,
104
- { onRequest: appOnRequest, schema: { tags: appTags, summary: '子应用列表', query: listQuery } },
148
+ {
149
+ onRequest: appOnRequest,
150
+ schema: {
151
+ tags: appTags,
152
+ summary: '子应用列表',
153
+ description: '分页查询已注册的子应用(不含已删除),按创建时间倒序',
154
+ query: appListQuery,
155
+ response: success('子应用分页列表', pageResult('子应用', app))
156
+ }
157
+ },
105
158
  wrap(async request => services.app.list(request.query))
106
159
  );
107
160
  fastify.get(
108
161
  `${admin}/app/detail`,
109
- { onRequest: appOnRequest, schema: { tags: appTags, summary: '子应用详情(含权限树)', query: idSchema } },
162
+ {
163
+ onRequest: appOnRequest,
164
+ schema: { tags: appTags, summary: '子应用详情(含权限树)', query: idSchema, response: success('子应用详情,permissions 为完整权限树', Object.assign({}, app, { nullable: false })) }
165
+ },
110
166
  wrap(async request => services.app.detail(request.query))
111
167
  );
112
168
  fastify.post(
113
169
  `${admin}/app/set-status`,
114
- { onRequest: appOnRequest, schema: { tags: appTags, summary: '启用 / 停用子应用(停用后从权限树移除)', body: statusBody } },
170
+ {
171
+ onRequest: appOnRequest,
172
+ schema: {
173
+ tags: appTags,
174
+ summary: '启用 / 停用子应用(停用后从权限树移除)',
175
+ description: '停用后该应用的 client 没有可用租户;状态变化时后台通知 independent 子应用回拉快照',
176
+ body: statusBody,
177
+ response: empty('设置成功')
178
+ }
179
+ },
115
180
  wrap(async request => {
116
181
  await services.app.setStatus(request.body);
117
182
  return {};
@@ -119,7 +184,16 @@ module.exports = fp(async (fastify, options) => {
119
184
  );
120
185
  fastify.post(
121
186
  `${admin}/app/remove`,
122
- { onRequest: appOnRequest, schema: { tags: appTags, summary: '删除子应用(子应用下次启动会重新注册)', body: idSchema } },
187
+ {
188
+ onRequest: appOnRequest,
189
+ schema: {
190
+ tags: appTags,
191
+ summary: '删除子应用(子应用下次启动会重新注册)',
192
+ description: '只标记为 removed,保留记录以继续拦截该应用的 client;后台通知 independent 子应用关闭托管租户',
193
+ body: idSchema,
194
+ response: empty('删除成功')
195
+ }
196
+ },
123
197
  wrap(async request => {
124
198
  await services.app.remove(request.body);
125
199
  return {};
@@ -128,19 +202,40 @@ module.exports = fp(async (fastify, options) => {
128
202
 
129
203
  fastify.post(
130
204
  `${admin}/client/rotate-secret`,
131
- { onRequest: options.getAuthenticate('client:manage'), schema: { tags: ['OIDC Client'], summary: '重置 client secret(明文只返回一次)', body: idSchema } },
205
+ {
206
+ onRequest: options.getAuthenticate('client:manage'),
207
+ schema: {
208
+ tags: ['OIDC管理后台-Client'],
209
+ summary: '重置 client secret(明文只返回一次)',
210
+ description: '为 confidential client 生成新的 secret,旧 secret 立即失效;public client 调用返回 400',
211
+ body: idSchema,
212
+ response: success('新的 client secret', { type: 'object', properties: { clientId: str('client_id'), clientSecret: str('新的 client secret 明文,只返回这一次') } })
213
+ }
214
+ },
132
215
  wrap(async request => services.client.rotateSecret(request.body))
133
216
  );
134
217
 
218
+ const keyList = { type: 'array', items: key };
135
219
  fastify.get(
136
220
  `${admin}/key/list`,
137
- { onRequest: options.getAuthenticate('key:manage'), schema: { tags: ['OIDC 签名密钥'], summary: '签名密钥列表' } },
221
+ {
222
+ onRequest: options.getAuthenticate('key:manage'),
223
+ schema: { tags: ['OIDC管理后台-签名密钥'], summary: '签名密钥列表', description: '按 active、next、retired 排序,不返回私钥', response: success('签名密钥列表', keyList) }
224
+ },
138
225
  wrap(async () => services.key.list())
139
226
  );
140
227
 
141
228
  fastify.post(
142
229
  `${admin}/key/rotate`,
143
- { onRequest: options.getAuthenticate('key:manage'), schema: { tags: ['OIDC 签名密钥'], summary: '轮换签名密钥' } },
230
+ {
231
+ onRequest: options.getAuthenticate('key:manage'),
232
+ schema: {
233
+ tags: ['OIDC管理后台-签名密钥'],
234
+ summary: '轮换签名密钥',
235
+ description: '当前 active 密钥退役(仍用于验签),next 密钥启用,并生成新的 next 密钥;清理超过保留期的退役密钥后重载 provider',
236
+ response: success('轮换后的签名密钥列表', keyList)
237
+ }
238
+ },
144
239
  wrap(async () => services.key.rotate())
145
240
  );
146
241
 
@@ -148,7 +243,21 @@ module.exports = fp(async (fastify, options) => {
148
243
  `${admin}/session/list`,
149
244
  {
150
245
  onRequest: options.getAuthenticate('session:manage'),
151
- schema: { tags: ['OIDC 会话'], summary: '用户登录会话列表', query: { type: 'object', required: ['userId'], properties: { userId: { type: 'string' } } } }
246
+ schema: {
247
+ tags: ['OIDC管理后台-会话'],
248
+ summary: '用户登录会话列表',
249
+ description: '列出用户在 IdP 的登录会话及已授权的 client',
250
+ query: { type: 'object', required: ['userId'], properties: { userId: { type: 'string', description: '用户 ID' } } },
251
+ response: success('会话列表', {
252
+ type: 'array',
253
+ items: obj('登录会话', {
254
+ uid: str('会话 uid,结束会话时使用'),
255
+ loginAt: any('登录时间'),
256
+ expiresAt: any('会话过期时间'),
257
+ clients: list('已授权的 client', obj('授权', { clientId: str('client_id'), sid: str('该 client 的会话 sid') }))
258
+ })
259
+ })
260
+ }
152
261
  },
153
262
  wrap(async request => services.grant.listSessions(request.query))
154
263
  );
@@ -157,7 +266,13 @@ module.exports = fp(async (fastify, options) => {
157
266
  `${admin}/session/revoke`,
158
267
  {
159
268
  onRequest: options.getAuthenticate('session:manage'),
160
- schema: { tags: ['OIDC 会话'], summary: '结束指定会话', body: { type: 'object', required: ['uid'], properties: { uid: { type: 'string' } } } }
269
+ schema: {
270
+ tags: ['OIDC管理后台-会话'],
271
+ summary: '结束指定会话',
272
+ description: '撤销该会话下全部授权与令牌,向已授权 client 发送 back-channel logout 通知并删除会话',
273
+ body: { type: 'object', required: ['uid'], properties: { uid: { type: 'string', description: '会话 uid(来自会话列表)' } } },
274
+ response: empty('会话已结束')
275
+ }
161
276
  },
162
277
  wrap(async request => {
163
278
  await services.grant.revokeSession(request.body);
@@ -170,9 +285,18 @@ module.exports = fp(async (fastify, options) => {
170
285
  {
171
286
  onRequest: options.getAuthenticate('session:manage'),
172
287
  schema: {
173
- tags: ['OIDC 会话'],
288
+ tags: ['OIDC管理后台-会话'],
174
289
  summary: '撤销用户全部令牌(logout=true 时同时强制下线)',
175
- body: { type: 'object', required: ['userId'], properties: { userId: { type: 'string' }, logout: { type: 'boolean', default: true } } }
290
+ description: '撤销用户已签发的全部令牌与授权;logout=true 时同时结束全部会话并发送 back-channel logout',
291
+ body: {
292
+ type: 'object',
293
+ required: ['userId'],
294
+ properties: {
295
+ userId: { type: 'string', description: '用户 ID' },
296
+ logout: { type: 'boolean', default: true, description: '是否同时结束会话(强制下线),默认 true;false 时保留会话,前端可静默重新获取令牌' }
297
+ }
298
+ },
299
+ response: success('撤销结果', { type: 'object', properties: { grants: num('撤销的授权数量'), sessions: num('结束的会话数量') } })
176
300
  }
177
301
  },
178
302
  wrap(async request => services.grant.revokeByAccount(request.body.userId, { logout: request.body.logout }))
@@ -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
 
@@ -24,9 +25,11 @@ module.exports = fp(async (fastify, options) => {
24
25
  `${options.prefix}/backchannel-logout`,
25
26
  {
26
27
  schema: {
27
- tags: ['OIDC 服务接口'],
28
+ tags: ['OIDC-服务接口'],
28
29
  summary: '接收 back-channel logout 通知',
29
- body: { type: 'object', required: ['logout_token'], properties: { logout_token: { type: 'string' } } }
30
+ description: 'central 子项目接收主项目的登出通知(application/x-www-form-urlencoded),验签后把 sid(或 sub)加入本地撤销列表;logout_token 无效返回 400',
31
+ body: { type: 'object', required: ['logout_token'], properties: { logout_token: { type: 'string', description: '主项目签发的 logout token(JWT)' } } },
32
+ response: empty('已处理')
30
33
  }
31
34
  },
32
35
  translator.wrap(async (request, reply) => {
@@ -1,4 +1,5 @@
1
1
  const fp = require('fastify-plugin');
2
+ const { str, bool, success } = require('../schema/response');
2
3
 
3
4
  /**
4
5
  * 前端登录方式发现:免登录,只返回公开信息,不能包含任何密钥
@@ -9,7 +10,21 @@ module.exports = fp(async (fastify, options) => {
9
10
  fastify.get(
10
11
  `${options.prefix}/config`,
11
12
  {
12
- schema: { tags: ['OIDC 配置'], summary: '获取前端登录配置(免登录)' }
13
+ schema: {
14
+ tags: ['OIDC-配置'],
15
+ summary: '获取前端登录配置(免登录)',
16
+ description: '前端据此决定登录方式(发起 OIDC 授权的 issuer、client_id、resource),只包含公开信息',
17
+ response: success('前端登录配置', {
18
+ type: 'object',
19
+ properties: {
20
+ mode: str('部署模式:standalone 独立部署(本项目即 IdP),central 由主项目统一登录'),
21
+ isMain: bool('standalone 模式下是否为主项目'),
22
+ issuer: str('OIDC issuer 地址'),
23
+ clientId: str('前端使用的 client_id'),
24
+ audience: str('申请 access_token 时的 resource(API 资源标识)')
25
+ }
26
+ })
27
+ }
13
28
  },
14
29
  async () => ({
15
30
  mode: options.mode,