@allbluecn/admin-app 0.6.15 → 0.6.17

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/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@allbluecn/admin-app",
3
- "version": "0.6.15",
3
+ "version": "0.6.17",
4
4
  "private": false,
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "type": "module",
7
7
  "dependencies": {
8
- "@allbluecn/database": "0.14.11",
8
+ "@allbluecn/database": "0.14.13",
9
9
  "@allbluecn/kernel": "0.12.4",
10
10
  "@auth/core": "^0.41.2",
11
11
  "@base-ui/react": "^1.8.0",
@@ -0,0 +1,57 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // This program is free software: you can redistribute it and/or modify
6
+ // it under the terms of the GNU Affero General Public License as published by
7
+ // the Free Software Foundation, either version 3 of the License, or
8
+ // (at your option) any later version.
9
+ //
10
+ // This program is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+ // admin 端同构副本(对齐 apps/web/src/lib/crypto/base64.ts)。admin 不可直接
19
+ // 引用 apps/web/src,故 api-key-cipher(sso-001 IdP secret 加密)依赖的 base64
20
+ // 在此维护同构副本。改动需双端同步。
21
+
22
+ // ⚠ 仅服务端 import。依赖 node:crypto,禁止在客户端组件/路由文件顶层引用。
23
+ import { Buffer } from "node:buffer"
24
+
25
+ const BASE64_RE = /^[A-Za-z0-9+/]*={0,2}$/
26
+ const BASE64URL_RE = /^[A-Za-z0-9_-]*$/
27
+
28
+ function isStrictBase64(input: string, strict: RegExp): boolean {
29
+ if (input.length % 4 !== 0) return false
30
+ return strict.test(input)
31
+ }
32
+
33
+ export function encodeBase64(bytes: Uint8Array): string {
34
+ return Buffer.from(bytes).toString("base64")
35
+ }
36
+
37
+ export function decodeBase64(input: string): Uint8Array {
38
+ return new Uint8Array(Buffer.from(input, "base64"))
39
+ }
40
+
41
+ export function decodeBase64Strict(input: string): Uint8Array {
42
+ if (!isStrictBase64(input, BASE64_RE)) {
43
+ throw new TypeError(`非法 base64 载荷:${JSON.stringify(input.slice(0, 32))}`)
44
+ }
45
+ return decodeBase64(input)
46
+ }
47
+
48
+ export function encodeBase64Url(bytes: Uint8Array): string {
49
+ return Buffer.from(bytes).toString("base64url")
50
+ }
51
+
52
+ export function decodeBase64Url(input: string): Uint8Array {
53
+ if (!BASE64URL_RE.test(input)) {
54
+ throw new TypeError(`非法 base64url 载荷:${JSON.stringify(input.slice(0, 32))}`)
55
+ }
56
+ return new Uint8Array(Buffer.from(input, "base64url"))
57
+ }
@@ -0,0 +1,58 @@
1
+ // admin 端同构副本(对齐 apps/web/src/server/lib/api-key-cipher.ts)。admin 不可
2
+ // 直接引用 apps/web/src,故 sso-001(IdP secret 加密落库)在此维护同构副本。
3
+ // ⚠ 仅服务端 import。依赖 node:crypto,禁止在客户端组件/路由文件顶层引用。
4
+ import { createCipheriv, createDecipheriv, randomBytes, createHash } from "node:crypto"
5
+
6
+ import { decodeBase64Strict, encodeBase64 } from "@/lib/crypto/base64"
7
+
8
+ const PREFIX = "enc:v1:"
9
+ const ALGO = "aes-256-gcm"
10
+ const IV_LEN = 12 // GCM 推荐 96 位
11
+
12
+ // SEC-16 密钥分离决策(对齐 web 端):优先 API_KEY_ENCRYPTION_SECRET,缺失回退
13
+ // AUTH_SECRET(双用途绑定,无法独立轮换,生产应显式配置专用密钥)。
14
+ let warnedFallbackKey = false
15
+
16
+ function getKey(): Buffer {
17
+ const dedicated = process.env.API_KEY_ENCRYPTION_SECRET
18
+ const secret = dedicated || process.env.AUTH_SECRET
19
+ if (!secret) {
20
+ throw new Error("缺少 API_KEY_ENCRYPTION_SECRET 或 AUTH_SECRET 环境变量")
21
+ }
22
+ if (!dedicated && !warnedFallbackKey) {
23
+ warnedFallbackKey = true
24
+ console.warn(
25
+ "[api-key-cipher] 未设置 API_KEY_ENCRYPTION_SECRET,回退使用 AUTH_SECRET 加密 IdP secret:" +
26
+ "双用途密钥无法独立轮换,建议生产环境配置专用密钥。",
27
+ )
28
+ }
29
+ return createHash("sha256").update(secret).digest() // 32 字节
30
+ }
31
+
32
+ export function encryptApiKey(plain: string): string {
33
+ if (!plain) return plain
34
+ const iv = randomBytes(IV_LEN)
35
+ const cipher = createCipheriv(ALGO, getKey(), iv)
36
+ const enc = Buffer.concat([cipher.update(plain, "utf8"), cipher.final()])
37
+ const tag = cipher.getAuthTag()
38
+ return `${PREFIX}${encodeBase64(iv)}.${encodeBase64(tag)}.${encodeBase64(enc)}`
39
+ }
40
+
41
+ export function decryptApiKey(stored: string | null | undefined): string | null {
42
+ if (!stored) return null
43
+ if (!stored.startsWith(PREFIX)) return stored // 存量明文兼容
44
+ try {
45
+ const parts = stored.slice(PREFIX.length).split(".")
46
+ if (parts.length !== 3) return null
47
+ const [ivB64, tagB64, encB64] = parts
48
+ const iv = decodeBase64Strict(ivB64)
49
+ const tag = decodeBase64Strict(tagB64)
50
+ const enc = decodeBase64Strict(encB64)
51
+ const decipher = createDecipheriv(ALGO, getKey(), iv)
52
+ decipher.setAuthTag(tag)
53
+ const dec = Buffer.concat([decipher.update(enc), decipher.final()])
54
+ return dec.toString("utf8")
55
+ } catch {
56
+ return null
57
+ }
58
+ }
@@ -21,6 +21,23 @@ import { getPrisma } from '@allbluecn/database/get-prisma'
21
21
  import type { Prisma } from '@allbluecn/database'
22
22
  import { adminAuthContextMiddleware } from '@/server/middlewares/auth-context'
23
23
  import { requireFeature } from '@/server/entitlements'
24
+ import { encryptApiKey } from '@/server/lib/api-key-cipher'
25
+
26
+ // sso-001:IdP secret 明文落库收敛——config 中键名匹配 /secret|password|key/i 的
27
+ // 标量值逐个 AES-256-GCM 加密(enc:v1: 信封,复用 api-key-cipher)。存量明文行
28
+ // 回填迁移后续跟进。
29
+ const SECRET_KEY_RE = /secret|password|key/i
30
+ function encryptSecretConfig(
31
+ config: Record<string, string | number | boolean | null>,
32
+ ): Record<string, string | number | boolean | null> {
33
+ const out: Record<string, string | number | boolean | null> = {}
34
+ for (const [k, v] of Object.entries(config)) {
35
+ out[k] = SECRET_KEY_RE.test(k) && typeof v === 'string' && v !== '' && !v.startsWith('enc:v1:')
36
+ ? encryptApiKey(v)
37
+ : v
38
+ }
39
+ return out
40
+ }
24
41
 
25
42
  export const listSsoProvidersFn = createServerFn({ method: 'GET' })
26
43
  .middleware([adminAuthContextMiddleware])
@@ -73,7 +90,9 @@ export const createSsoProviderFn = createServerFn({ method: 'POST' })
73
90
  workspaceId: data.workspaceId,
74
91
  type: data.type,
75
92
  name: data.name,
76
- config: (data.config ?? {}) as Prisma.InputJsonValue,
93
+ // sso-001:IdP client secret 等凭据加密落库(键名匹配 /secret|password|key/i
94
+ // 的标量值逐个 encryptApiKey)
95
+ config: encryptSecretConfig(data.config ?? {}) as Prisma.InputJsonValue,
77
96
  domain: data.domain,
78
97
  },
79
98
  })
@@ -28,7 +28,9 @@ export const updateUserRoleFn = createServerFn({ method: 'POST' })
28
28
  return updateUserGuarded(context.admin.role, data.userId, { role: data.role }, (tx) =>
29
29
  tx.user.update({
30
30
  where: { id: data.userId },
31
- data: { role: data.role },
31
+ // 降级/升级均吊销既有会话(web 侧 reset-password 同款 tokenVersion 吊销机制):
32
+ // 否则被降级的 ADMIN 已签发 JWT 的 role 永不刷新,保留 admin 全权直到超时。
33
+ data: { role: data.role, tokenVersion: { increment: 1 } },
32
34
  // 不回显 password 哈希
33
35
  select: { id: true, email: true, username: true, role: true, status: true },
34
36
  }),
@@ -30,7 +30,9 @@ export const updateUserStatusFn = createServerFn({ method: 'POST' })
30
30
  return updateUserGuarded(context.admin.role, data.userId, { status: data.status }, (tx) =>
31
31
  tx.user.update({
32
32
  where: { id: data.userId },
33
- data: { status: data.status },
33
+ // 停用/封禁均吊销既有会话(同 update-role tokenVersion 吊销机制):被停用的
34
+ // ADMIN 已签发 JWT 若不吊销则保留 admin 全权直到超时。
35
+ data: { status: data.status, tokenVersion: { increment: 1 } },
34
36
  // 不回显 password 哈希
35
37
  select: { id: true, email: true, username: true, role: true, status: true },
36
38
  }),