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:
@@ -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')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user