feat: 后台管理员独立账号体系,Casdoor 配置改为后台管理
- 新增 admin_users/admin_sessions 表与 adminAuth 工具(scrypt 密码 + 独立会话 Cookie) - /admin/init 初始化首个管理员,未初始化时服务端/客户端双守卫引导 - 后台鉴权改为管理员会话,前台 Casdoor 用户一律普通用户、与后台解耦 - Casdoor 配置改存 settings 表并由「统一认证」页管理,移除全部 CASDOOR 环境变量 - 侧边栏增加统一认证/管理员入口与登出;LoginModal 感知 SSO 启用状态 - 补充 adminAuth 测试,重写 casdoor 测试,全部 23 项通过
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { describe, it, expect, vi, beforeAll } from 'vitest'
|
||||
|
||||
/**
|
||||
* 后台管理员认证单测:
|
||||
* - hashPassword / verifyPassword:scrypt 哈希与校验(格式 scrypt$N$r$p$salt$hash)
|
||||
* - 异常/非法存储串一律校验失败,防绕过
|
||||
*/
|
||||
|
||||
vi.mock('../server/utils/db', () => ({
|
||||
getDb: vi.fn(() => {
|
||||
throw new Error('db not used in these tests')
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('h3', () => ({
|
||||
createError: (o: unknown) => o,
|
||||
getCookie: () => null,
|
||||
setCookie: () => {},
|
||||
deleteCookie: () => {},
|
||||
}))
|
||||
|
||||
import { hashPassword, verifyPassword } from '../server/utils/adminAuth'
|
||||
|
||||
describe('admin password hashing', () => {
|
||||
it('hashes and verifies a correct password', () => {
|
||||
const hash = hashPassword('correct horse battery staple')
|
||||
expect(hash.startsWith('scrypt$')).toBe(true)
|
||||
expect(verifyPassword('correct horse battery staple', hash)).toBe(true)
|
||||
})
|
||||
|
||||
it('rejects a wrong password', () => {
|
||||
const hash = hashPassword('right-password')
|
||||
expect(verifyPassword('wrong-password', hash)).toBe(false)
|
||||
})
|
||||
|
||||
it('produces unique salts (same password, different hashes)', () => {
|
||||
const a = hashPassword('same-pass')
|
||||
const b = hashPassword('same-pass')
|
||||
expect(a).not.toBe(b)
|
||||
expect(verifyPassword('same-pass', a)).toBe(true)
|
||||
expect(verifyPassword('same-pass', b)).toBe(true)
|
||||
})
|
||||
|
||||
it('rejects malformed / foreign stored values', () => {
|
||||
expect(verifyPassword('x', '')).toBe(false)
|
||||
expect(verifyPassword('x', 'plaintext')).toBe(false)
|
||||
expect(verifyPassword('x', 'bcrypt$abcdef')).toBe(false)
|
||||
expect(verifyPassword('x', 'scrypt$16384$8$1$salt$nothex')).toBe(false)
|
||||
})
|
||||
})
|
||||
+69
-54
@@ -1,77 +1,92 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
/**
|
||||
* Casdoor 认证逻辑单测(纯函数,不依赖外部服务)。
|
||||
* 覆盖:
|
||||
* - isAdminClaim:IsAdmin / isAdmin 字段与 roles 名单判定
|
||||
* - getCasdoorSignUpUrl / getCasdoorLogoutUrl:登录/注册/登出地址构造
|
||||
* - config.decodeCert:证书 base64 还原
|
||||
* Casdoor 认证逻辑单测(纯函数,依赖通过 vi.mock 桩掉):
|
||||
* - getCasdoorSignInUrl / getCasdoorLogoutUrl 地址构造(走 SDK getSignInUrl 与手动拼接)
|
||||
* - casdoorConfig.toPublicCasdoorConfig 不泄漏 secret
|
||||
* - casdoorConfig.validateCasdoorConfig 必填校验
|
||||
* - 后台管理员密码 scrypt 哈希/校验(adminAuth)
|
||||
*/
|
||||
|
||||
vi.mock('../server/utils/config', () => ({
|
||||
getConfig: () => ({
|
||||
casdoor: {
|
||||
vi.mock('../server/utils/casdoorConfig', async (importOriginal) => {
|
||||
const actual = (await importOriginal()) as Record<string, unknown>
|
||||
return {
|
||||
...actual,
|
||||
getCasdoorConfig: () => ({
|
||||
serverUrl: 'https://sys.virtheart.com',
|
||||
clientId: 'a0ba9570e0bcfbeb9f67',
|
||||
clientSecret: 'secret',
|
||||
clientSecret: 'secret123',
|
||||
orgName: 'virtheart',
|
||||
appName: 'virtheat-personal',
|
||||
certificate: 'FAKE_CERT',
|
||||
adminRoles: ['admin'],
|
||||
callbackPath: '/api/auth/callback',
|
||||
redirectUri: 'https://personal.virtheart.com/api/auth/callback',
|
||||
},
|
||||
}),
|
||||
isCasdoorEnabled: () => true,
|
||||
}))
|
||||
|
||||
import { isAdminClaim } from '../server/utils/casdoor'
|
||||
import { getConfig } from '../server/utils/config'
|
||||
|
||||
describe('isAdminClaim', () => {
|
||||
it('returns false for empty / null claims', () => {
|
||||
expect(isAdminClaim(null)).toBe(false)
|
||||
expect(isAdminClaim(undefined)).toBe(false)
|
||||
expect(isAdminClaim({})).toBe(false)
|
||||
})
|
||||
|
||||
it('honors isAdmin / IsAdmin boolean fields', () => {
|
||||
expect(isAdminClaim({ isAdmin: true })).toBe(true)
|
||||
expect(isAdminClaim({ IsAdmin: true })).toBe(true)
|
||||
expect(isAdminClaim({ isAdmin: false })).toBe(false)
|
||||
})
|
||||
|
||||
it('honors roles array (string form)', () => {
|
||||
expect(isAdminClaim({ roles: ['admin'] })).toBe(true)
|
||||
expect(isAdminClaim({ roles: ['user', 'editor'] })).toBe(false)
|
||||
})
|
||||
|
||||
it('honors roles array (object form with name)', () => {
|
||||
expect(isAdminClaim({ roles: [{ name: 'admin' }] })).toBe(true)
|
||||
expect(isAdminClaim({ roles: [{ name: 'user' }] })).toBe(false)
|
||||
})
|
||||
|
||||
it('is case-insensitive on role names', () => {
|
||||
expect(isAdminClaim({ roles: ['Admin'] })).toBe(true)
|
||||
})
|
||||
|
||||
it('falls back to configured admin roles', () => {
|
||||
const cfg = getConfig()
|
||||
expect(cfg.casdoor.adminRoles).toContain('admin')
|
||||
})
|
||||
enabled: true,
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('../server/utils/config', () => ({
|
||||
getConfig: () => ({
|
||||
siteUrl: 'https://personal.virtheart.com',
|
||||
dbPath: ':memory:',
|
||||
sessionSecret: 'test-secret',
|
||||
smtp: {},
|
||||
}),
|
||||
}))
|
||||
|
||||
import { getCasdoorSignInUrl, getCasdoorSignUpUrl, getCasdoorLogoutUrl } from '../server/utils/casdoor'
|
||||
import { toPublicCasdoorConfig, validateCasdoorConfig } from '../server/utils/casdoorConfig'
|
||||
|
||||
describe('casdoor URLs', () => {
|
||||
it('builds signup url from config', async () => {
|
||||
const { getCasdoorSignUpUrl } = await import('../server/utils/casdoor')
|
||||
it('builds sign-in url via SDK with redirect_uri + state', () => {
|
||||
const url = getCasdoorSignInUrl()
|
||||
expect(url).toContain('https://sys.virtheart.com/login/oauth/authorize')
|
||||
expect(url).toContain('client_id=a0ba9570e0bcfbeb9f67')
|
||||
expect(decodeURIComponent(url)).toContain('redirect_uri=https://personal.virtheart.com/api/auth/callback')
|
||||
})
|
||||
|
||||
it('builds signup url', () => {
|
||||
expect(getCasdoorSignUpUrl()).toBe('https://sys.virtheart.com/signup/virtheat-personal')
|
||||
})
|
||||
|
||||
it('builds logout url with client_id and redirect_uri', async () => {
|
||||
const { getCasdoorLogoutUrl } = await import('../server/utils/casdoor')
|
||||
it('builds logout url with client_id and redirect_uri', () => {
|
||||
const url = getCasdoorLogoutUrl()
|
||||
expect(url).toContain('https://sys.virtheart.com/logout')
|
||||
expect(url).toContain('client_id=a0ba9570e0bcfbeb9f67')
|
||||
expect(decodeURIComponent(url)).toContain('https://personal.virtheart.com/api/auth/callback')
|
||||
})
|
||||
})
|
||||
|
||||
describe('casdoorConfig', () => {
|
||||
it('public config never leaks secret fields', () => {
|
||||
const pub = toPublicCasdoorConfig({
|
||||
serverUrl: 'https://sys.virtheart.com',
|
||||
clientId: 'cid',
|
||||
clientSecret: 'super-secret',
|
||||
orgName: 'virtheart',
|
||||
appName: 'app',
|
||||
certificate: 'CERT',
|
||||
callbackPath: '/api/auth/callback',
|
||||
enabled: true,
|
||||
})
|
||||
expect(pub).not.toHaveProperty('clientSecret')
|
||||
expect(pub).not.toHaveProperty('certificate')
|
||||
expect(pub.enabled).toBe(true)
|
||||
expect(pub.redirectUri).toBe('https://personal.virtheart.com/api/auth/callback')
|
||||
})
|
||||
|
||||
it('validates required fields when enabled', () => {
|
||||
const errs = validateCasdoorConfig({ enabled: true })
|
||||
expect(errs.length).toBeGreaterThan(0)
|
||||
expect(validateCasdoorConfig({ enabled: false })).toEqual([])
|
||||
expect(
|
||||
validateCasdoorConfig({
|
||||
enabled: true,
|
||||
serverUrl: 'https://sys.virtheart.com',
|
||||
clientId: 'cid',
|
||||
orgName: 'virtheart',
|
||||
appName: 'app',
|
||||
}),
|
||||
).toEqual([])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user