feat: 登录改为 Casdoor 统一身份认证(SSO),重建用户体系

- 移除内置密码注册/登录/改密/删号/头像,统一走 Casdoor OIDC 授权码
- 服务端 casdoor-nodejs-sdk:code 换 token + 证书公钥验签 + get-account 补齐资料
- 用户表重建为 provider+provider_id(Casdoor sub 为准),邮箱不再唯一
- 管理员由 Casdoor token 的 isAdmin/IsAdmin 字段或 admin 角色判定,删除角色切换接口
- 前端登录弹窗改为申统认证跳转,注册去 Casdoor;后台用户列表只读角色
- 会话沿用 an_session httpOnly Cookie,requireUser/requireAdmin 守卫保留
- 配置注入 deploy.sh/.env.example;新增 casdoor 单测,删除 password 单测
This commit is contained in:
2026-08-09 00:18:47 +08:00
parent bdafac2857
commit 337eb4f05a
51 changed files with 1395 additions and 1504 deletions
+77
View File
@@ -0,0 +1,77 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
/**
* Casdoor 认证逻辑单测(纯函数,不依赖外部服务)。
* 覆盖:
* - isAdminClaim:IsAdmin / isAdmin 字段与 roles 名单判定
* - getCasdoorSignUpUrl / getCasdoorLogoutUrl:登录/注册/登出地址构造
* - config.decodeCert:证书 base64 还原
*/
vi.mock('../server/utils/config', () => ({
getConfig: () => ({
casdoor: {
serverUrl: 'https://sys.virtheart.com',
clientId: 'a0ba9570e0bcfbeb9f67',
clientSecret: 'secret',
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')
})
})
describe('casdoor URLs', () => {
it('builds signup url from config', async () => {
const { getCasdoorSignUpUrl } = await import('../server/utils/casdoor')
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')
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')
})
})
-26
View File
@@ -1,26 +0,0 @@
import { describe, it, expect } from 'vitest'
import { hashPassword, verifyPassword } from '../server/utils/password'
describe('password hashing (scrypt)', () => {
it('hashes and verifies the correct password', async () => {
const hash = await hashPassword('secret123')
expect(hash).toContain(':')
expect(await verifyPassword('secret123', hash)).toBe(true)
})
it('rejects a wrong password', async () => {
const hash = await hashPassword('secret123')
expect(await verifyPassword('wrong-pass', hash)).toBe(false)
})
it('rejects a malformed stored hash', async () => {
expect(await verifyPassword('secret123', 'no-separator')).toBe(false)
expect(await verifyPassword('secret123', '')).toBe(false)
})
it('produces a unique salt per call', async () => {
const a = await hashPassword('secret123')
const b = await hashPassword('secret123')
expect(a).not.toBe(b)
})
})
+5 -2
View File
@@ -23,7 +23,8 @@ describe('seed data integrity', () => {
expect(a.excerptEn).toBeTruthy()
expect(a.contentZh.length).toBeGreaterThan(0)
expect(a.contentEn.length).toBeGreaterThan(0)
expect(a.image).toBeTruthy()
// 图片留空由后台上传配置,种子不携带演示图
expect(a.image).toBeNull()
expect(a.published).toBe(1)
}
})
@@ -37,8 +38,10 @@ describe('seed data integrity', () => {
expect(w.descriptionEn).toBeTruthy()
expect(w.overviewZh).toBeTruthy()
expect(w.overviewEn).toBeTruthy()
expect(w.gallery.length).toBeGreaterThan(0)
expect(Array.isArray(w.gallery)).toBe(true)
expect(w.gallery.length).toBe(0)
expect(w.tags.length).toBeGreaterThan(0)
expect(w.image).toBeNull()
expect(w.published).toBe(1)
}
})
+9 -2
View File
@@ -14,13 +14,20 @@ describe('default site settings', () => {
expect(defaultSettings.contact.address.en).toBeTruthy()
})
it('has non-empty socials and tools', () => {
it('has non-empty socials (domestic-first, url left blank) and tools', () => {
expect(defaultSettings.socials.length).toBeGreaterThan(0)
expect(defaultSettings.socials.every((s) => s.name && s.icon && s.url)).toBe(true)
expect(defaultSettings.socials.every((s) => s.name && s.icon)).toBe(true)
expect(defaultSettings.tools.length).toBeGreaterThan(0)
expect(defaultSettings.tools.every((t) => t.id && t.url && t.descZh && t.descEn)).toBe(true)
})
it('home page content defaults are empty (no demo data, configured via admin)', () => {
expect(defaultSettings.hero.gallery).toEqual([])
expect(defaultSettings.achievements).toEqual([])
expect(defaultSettings.testimonials).toEqual([])
expect(defaultSettings.partners).toEqual([])
})
it('has bilingual footer texts', () => {
for (const k of ['title', 'newsletterTitle', 'newsletterNote', 'copyright'] as const) {
expect(defaultSettings.footer[k].zh).toBeTruthy()