@kne/fastify-oidc 0.1.1 → 0.1.3

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
@@ -66,7 +66,7 @@ npm i --save @kne/fastify-oidc
66
66
 
67
67
  | 资源 | identifier | scope | 用途 |
68
68
  |------|------------|-------|------|
69
- | 本项目 API | `${ORIGIN}/api` | `api` | SPA 调用本项目接口 |
69
+ | 本项目 API(名称为 `name`) | `${ORIGIN}/api` | `api` | SPA 调用本项目接口 |
70
70
  | 服务接口 | `${issuer origin}${prefix}` | `user:read tenant:read` | 子项目以 client_credentials 读取用户 / 租户 |
71
71
 
72
72
  client 只能申请 `allowedResources` 中的资源;子项目 token 的 `aud` 是子项目 API,拿到主项目会被拒绝。
@@ -151,6 +151,7 @@ fastify.register(require('@kne/fastify-file-manager'), {
151
151
  |----------|------|------|
152
152
  | `ORIGIN` | 是 | 本项目对外访问的 origin,如 `https://a.example.com` |
153
153
  | `AUTH_MODE` | 否 | `standalone`(默认)/ `central` |
154
+ | `OIDC_IS_MAIN` | 否 | `true` 表示主系统(子项目以本项目为 IdP),前端登录页据此不显示 SSO 入口 |
154
155
  | `OIDC_KEY_SECRET` | 是(standalone) | 加密签名私钥与 client_secret 的密钥,未配置时使用不安全的默认值并告警 |
155
156
  | `OIDC_COOKIE_SECRET` | 否 | 交互 cookie 签名密钥,默认由 `OIDC_KEY_SECRET` 派生 |
156
157
 
@@ -219,7 +220,7 @@ import { loadModule } from '@kne/remote-loader';
219
220
  import createAjax from '@kne/axios-fetch';
220
221
 
221
222
  export const globalInit = async () => {
222
- const { createOidcClient } = await loadModule('components-admin:Oidc');
223
+ const { default: createOidcClient } = await loadModule('components-admin:Oidc@createOidcClient');
223
224
  const oidc = createOidcClient({
224
225
  issuer: window.runtimeOidcIssuer || `${window.location.origin}/oidc`,
225
226
  clientId: window.runtimeOidcClientId,
@@ -258,6 +259,7 @@ oidc-provider 9.x 为 ESM 包,本插件通过动态 `import()` 加载,要求
258
259
  | prefix | string | 否 | `/api/oidc` | 插件接口前缀(交互、管理、服务接口) |
259
260
  | dbTableNamePrefix | string | 否 | `t_` | 表名前缀,表名形如 `t_oidc_payload` |
260
261
  | mode | string | 否 | `AUTH_MODE` 或 `standalone` | `standalone` / `central` |
262
+ | isMain | boolean | 否 | `OIDC_IS_MAIN === 'true'` | 是否为主系统(其它子项目以本项目为 IdP),仅 standalone 有效;通过 `GET /api/oidc/config` 告知前端,主系统登录页不显示 SSO 入口 |
261
263
  | origin | string | 是 | `ORIGIN` | 本项目对外 origin |
262
264
  | issuer | string | central 必填 | `OIDC_ISSUER`;standalone 为 `${origin}${mountPath}` | issuer 必须带路径 |
263
265
  | mountPath | string | 否 | `/oidc` | standalone 未配置 issuer 时 IdP 的挂载路径 |
@@ -287,7 +289,7 @@ oidc-provider 9.x 为 ESM 包,本插件通过动态 `import()` 加载,要求
287
289
  | includePermissions | boolean | 否 | `false` | 资源服务未设置时,token 是否携带 permissions |
288
290
  | defaultResourceScope | string | 否 | `api` | 资源服务默认 scope |
289
291
  | seedSelf | boolean | 否 | `true` | 启动时自动登记本项目 SPA client、本项目 API、服务接口资源 |
290
- | selfClient | object | 否 | `{}` | 覆盖默认 SPA client 的 metadata |
292
+ | selfClient | object | 否 | `{}` | 覆盖默认 SPA client 的字段与 metadata,如 `{ clientName: '系统名称' }`(默认名称为 `name`);仅首次登记时生效,已存在的 client 不会被覆盖 |
291
293
  | clients | array | 否 | `[]` | 启动时登记的 client(已存在则跳过),字段同管理接口 `client/create`,metadata 直接平铺 |
292
294
  | resourceServers | array | 否 | `[]` | 启动时登记的资源服务(已存在则跳过) |
293
295
  | serviceScopes | array | 否 | `['user:read', 'tenant:read']` | 服务接口资源的 scope |
@@ -339,6 +341,30 @@ oidc-provider 9.x 为 ESM 包,本插件通过动态 `import()` 加载,要求
339
341
  | end_session | `GET /oidc/session/end` | RP-Initiated Logout |
340
342
  | revocation / introspection | `POST /oidc/token/revocation`、`POST /oidc/token/introspection` | 撤销、内省 |
341
343
 
344
+ ##### 前端登录配置(standalone / central)
345
+
346
+ ###### GET /api/oidc/config
347
+
348
+ 免登录,供前端登录页判断是否支持 SSO 以及当前模式(如 components-admin 的 Account 登录页)。只返回公开信息:
349
+
350
+ ```json
351
+ {
352
+ "mode": "central",
353
+ "isMain": false,
354
+ "issuer": "https://main.example.com/oidc",
355
+ "clientId": "child-spa",
356
+ "audience": "https://child.example.com/api"
357
+ }
358
+ ```
359
+
360
+ | 字段 | 说明 |
361
+ |------|------|
362
+ | mode | `standalone` / `central` |
363
+ | isMain | 是否为主系统(`isMain` 配置),central 恒为 `false` |
364
+ | issuer | IdP issuer;central 为主项目 issuer |
365
+ | clientId | 本项目前端使用的 client_id(`clientId` 配置,默认 `oidc-spa`) |
366
+ | audience | 本项目资源服务标识 |
367
+
342
368
  ##### 登录交互(standalone)
343
369
 
344
370
  前端交互页通过以下接口驱动流程,均依赖交互 cookie,需与 IdP 同源调用。成功时返回 `{ redirectTo }`,前端 `window.location` 跳转即可。
package/index.js CHANGED
@@ -23,6 +23,7 @@ module.exports = fp(
23
23
  prefix: '/api/oidc',
24
24
  dbTableNamePrefix: 't_',
25
25
  mode: env.AUTH_MODE || 'standalone',
26
+ isMain: env.OIDC_IS_MAIN === 'true',
26
27
  origin: env.ORIGIN,
27
28
  issuer: env.OIDC_ISSUER,
28
29
  mountPath: '/oidc',
@@ -0,0 +1,22 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ /**
4
+ * 前端登录方式发现:免登录,只返回公开信息,不能包含任何密钥
5
+ */
6
+ module.exports = fp(async (fastify, options) => {
7
+ const { runtime } = options;
8
+
9
+ fastify.get(
10
+ `${options.prefix}/config`,
11
+ {
12
+ schema: { tags: ['OIDC 配置'], summary: '获取前端登录配置(免登录)' }
13
+ },
14
+ async () => ({
15
+ mode: options.mode,
16
+ isMain: options.mode === 'standalone' && !!options.isMain,
17
+ issuer: runtime.issuer,
18
+ clientId: runtime.clientId,
19
+ audience: runtime.audience
20
+ })
21
+ );
22
+ });
package/libs/idp/index.js CHANGED
@@ -45,13 +45,13 @@ module.exports = ({ fastify, options, translator }) => {
45
45
  const resourceServers = [...(options.resourceServers || [])];
46
46
  const clients = [...(options.clients || [])];
47
47
  if (options.seedSelf) {
48
- resourceServers.push({ identifier: runtime.audience, name: `${options.name} API` });
48
+ resourceServers.push({ identifier: runtime.audience, name: options.name });
49
49
  resourceServers.push({ identifier: runtime.serviceAudience, name: 'OIDC 服务接口', scope: options.serviceScopes.join(' ') });
50
50
  clients.push(
51
51
  Object.assign(
52
52
  {
53
53
  clientId: runtime.clientId,
54
- clientName: `${options.name} 前端`,
54
+ clientName: options.name,
55
55
  redirect_uris: [`${runtime.origin}${options.callbackPath}`],
56
56
  post_logout_redirect_uris: [`${runtime.origin}/`],
57
57
  token_endpoint_auth_method: 'none',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-oidc",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "基于 oidc-provider 的 Fastify OIDC 认证插件,支持 standalone / central 多项目单点登录、租户与跨项目令牌",
5
5
  "main": "index.js",
6
6
  "scripts": {