541e24c6f4
- 新增 admin_users/admin_sessions 表与 adminAuth 工具(scrypt 密码 + 独立会话 Cookie) - /admin/init 初始化首个管理员,未初始化时服务端/客户端双守卫引导 - 后台鉴权改为管理员会话,前台 Casdoor 用户一律普通用户、与后台解耦 - Casdoor 配置改存 settings 表并由「统一认证」页管理,移除全部 CASDOOR 环境变量 - 侧边栏增加统一认证/管理员入口与登出;LoginModal 感知 SSO 启用状态 - 补充 adminAuth 测试,重写 casdoor 测试,全部 23 项通过
113 lines
3.9 KiB
TypeScript
113 lines
3.9 KiB
TypeScript
import { SDK } from 'casdoor-nodejs-sdk'
|
||
import { getCasdoorConfig } from './casdoorConfig'
|
||
import { getConfig } from './config'
|
||
|
||
/**
|
||
* Casdoor 统一认证封装(服务端)。
|
||
*
|
||
* - 登录 URL、code->token 交换、JWT 验签全部走官方 casdoor-nodejs-sdk
|
||
* - 配置(serverUrl/clientId/clientSecret/证书)由后台写入 settings 表,不依赖环境变量
|
||
* - 登录成功后再通过 get-account 补齐邮箱/头像等字段(拿不到也不阻塞登录)
|
||
* - 前台用户一律视为普通用户;后台权限由独立 admin_users 表管理
|
||
*/
|
||
let _sdk: SDK | null = null
|
||
let _sdkKey = ''
|
||
|
||
function getSDK(): SDK {
|
||
const cfg = getCasdoorConfig()
|
||
const key = [cfg.serverUrl, cfg.clientId, cfg.clientSecret, cfg.orgName, cfg.appName, cfg.certificate].join('|')
|
||
if (!_sdk || _sdkKey !== key) {
|
||
_sdk = new SDK({
|
||
endpoint: cfg.serverUrl,
|
||
clientId: cfg.clientId,
|
||
clientSecret: cfg.clientSecret,
|
||
certificate: cfg.certificate,
|
||
orgName: cfg.orgName,
|
||
appName: cfg.appName || undefined,
|
||
})
|
||
_sdkKey = key
|
||
}
|
||
return _sdk
|
||
}
|
||
|
||
/** 构造 Casdoor 登录授权地址(redirect_uri = 本站回调) */
|
||
export function getCasdoorSignInUrl(): string {
|
||
const cfg = getCasdoorConfig()
|
||
const redirectUri = getConfig().siteUrl + cfg.callbackPath
|
||
return getSDK().getSignInUrl(redirectUri)
|
||
}
|
||
|
||
/** 构造 Casdoor 注册页地址(在 Casdoor 侧完成注册) */
|
||
export function getCasdoorSignUpUrl(): string {
|
||
const cfg = getCasdoorConfig()
|
||
return `${cfg.serverUrl}/signup/${cfg.appName}`
|
||
}
|
||
|
||
/** 单点登出地址(访问后跳回 redirectUri;未配置则返回空串) */
|
||
export function getCasdoorLogoutUrl(): string {
|
||
const cfg = getCasdoorConfig()
|
||
if (!cfg.serverUrl || !cfg.clientId) return ''
|
||
const redirectUri = getConfig().siteUrl + cfg.callbackPath
|
||
return `${cfg.serverUrl}/logout?client_id=${cfg.clientId}&redirect_uri=${encodeURIComponent(redirectUri)}`
|
||
}
|
||
|
||
/** 登录回调校验的期望 state(casdoor SDK 以 appName 作为 state 返回) */
|
||
export function getCasdoorState(): string {
|
||
return getCasdoorConfig().appName
|
||
}
|
||
|
||
export interface CasdoorIdentity {
|
||
/** 唯一用户标识(Casdoor 用户 UUID,即 JWT 的 sub) */
|
||
providerId: string
|
||
provider: string
|
||
username: string
|
||
email: string
|
||
avatar: string
|
||
}
|
||
|
||
/**
|
||
* 交换授权码并验签,返回可落库的身份信息。
|
||
* @throws 交换失败 / 验签失败 / 缺少 sub
|
||
*/
|
||
export async function exchangeCodeForIdentity(code: string): Promise<CasdoorIdentity> {
|
||
if (!code) {
|
||
throw new Error('缺少授权码')
|
||
}
|
||
const { access_token } = await getSDK().getAuthToken(code)
|
||
if (!access_token) {
|
||
throw new Error('Casdoor 未返回 access_token')
|
||
}
|
||
|
||
const claims = getSDK().parseJwtToken(access_token)
|
||
const account = await getAccountProfile(access_token).catch(() => null)
|
||
|
||
const providerId = String(account?.sub || (claims as any).sub || (claims as any).id || '')
|
||
if (!providerId) {
|
||
throw new Error('Casdoor token 缺少用户标识(sub)')
|
||
}
|
||
|
||
const username =
|
||
String(account?.name || (claims as any).name || (claims as any).preferred_username || 'user').trim() ||
|
||
'user'
|
||
const email = String(account?.email || (claims as any).email || '').trim().toLowerCase()
|
||
const avatar = String(account?.avatar || (claims as any).avatar || (claims as any).picture || '').trim()
|
||
|
||
return {
|
||
providerId,
|
||
provider: 'casdoor',
|
||
username,
|
||
email,
|
||
avatar,
|
||
}
|
||
}
|
||
|
||
/** 通过 get-account 接口补齐完整账号信息(含邮箱/头像);失败不阻塞登录 */
|
||
async function getAccountProfile(accessToken: string): Promise<Record<string, unknown> | null> {
|
||
const cfg = getCasdoorConfig()
|
||
const url = `${cfg.serverUrl}/api/get-account?access_token=${encodeURIComponent(accessToken)}`
|
||
const res = await fetch(url)
|
||
if (!res.ok) return null
|
||
const body = (await res.json()) as { status?: string; data?: Record<string, unknown> }
|
||
if (body?.status !== 'ok') return null
|
||
return body.data ?? null
|
||
} |