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,130 @@
|
||||
import { SDK } from 'casdoor-nodejs-sdk'
|
||||
import { getConfig } from './config'
|
||||
|
||||
/**
|
||||
* Casdoor 统一认证封装(服务端)。
|
||||
*
|
||||
* - 登录 URL、code->token 交换、JWT 验签全部走官方 casdoor-nodejs-sdk
|
||||
* - 客户端 secret / 证书公钥只存在于服务端环境变量,绝不进入浏览器
|
||||
* - 登录成功后再通过 get-account 补齐邮箱/头像/角色等字段(拿不到也不阻塞登录)
|
||||
*/
|
||||
let _sdk: SDK | null = null
|
||||
let _sdkKey = ''
|
||||
|
||||
function getSDK(): SDK {
|
||||
const { casdoor } = getConfig()
|
||||
const key = [casdoor.serverUrl, casdoor.clientId, casdoor.clientSecret, casdoor.orgName, casdoor.appName, casdoor.certificate].join('|')
|
||||
if (!_sdk || _sdkKey !== key) {
|
||||
_sdk = new SDK({
|
||||
endpoint: casdoor.serverUrl,
|
||||
clientId: casdoor.clientId,
|
||||
clientSecret: casdoor.clientSecret,
|
||||
certificate: casdoor.certificate,
|
||||
orgName: casdoor.orgName,
|
||||
appName: casdoor.appName || undefined,
|
||||
})
|
||||
_sdkKey = key
|
||||
}
|
||||
return _sdk
|
||||
}
|
||||
|
||||
/** 构造 Casdoor 登录授权地址(redirect_uri = 本站回调) */
|
||||
export function getCasdoorSignInUrl(): string {
|
||||
const { casdoor } = getConfig()
|
||||
return getSDK().getSignInUrl(casdoor.redirectUri)
|
||||
}
|
||||
|
||||
/** 构造 Casdoor 注册页地址(在 Casdoor 侧完成注册) */
|
||||
export function getCasdoorSignUpUrl(): string {
|
||||
const { casdoor } = getConfig()
|
||||
return `${casdoor.serverUrl}/signup/${casdoor.appName}`
|
||||
}
|
||||
|
||||
/** 单点登出地址(访问后跳回 redirectUri;未配置则返回空串) */
|
||||
export function getCasdoorLogoutUrl(): string {
|
||||
const { casdoor } = getConfig()
|
||||
if (!casdoor.serverUrl || !casdoor.clientId) return ''
|
||||
return `${casdoor.serverUrl}/logout?client_id=${casdoor.clientId}&redirect_uri=${encodeURIComponent(casdoor.redirectUri)}`
|
||||
}
|
||||
|
||||
/** 登录回调校验的期望 state(casdoor SDK 以 appName 作为 state 返回) */
|
||||
export function getCasdoorState(): string {
|
||||
return getConfig().casdoor.appName
|
||||
}
|
||||
|
||||
export interface CasdoorIdentity {
|
||||
/** 唯一用户标识(Casdoor 用户 UUID,即 JWT 的 sub) */
|
||||
providerId: string
|
||||
provider: string
|
||||
username: string
|
||||
email: string
|
||||
avatar: string
|
||||
isAdmin: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 交换授权码并验签,返回可落库的身份信息。
|
||||
* @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,
|
||||
isAdmin: isAdminClaim(account) || isAdminClaim(claims),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员判定:
|
||||
* - token 字段 `IsAdmin` / `isAdmin` 为 true
|
||||
* - roles(对象数组 {name} 或字符串数组)命中 NUXT_CASDOOR_ADMIN_ROLES
|
||||
*/
|
||||
export function isAdminClaim(claims: Record<string, unknown> | undefined | null): boolean {
|
||||
if (!claims) return false
|
||||
if ((claims as any).isAdmin === true || (claims as any).IsAdmin === true) return true
|
||||
|
||||
const { casdoor } = getConfig()
|
||||
const roles = (claims as any).roles
|
||||
if (!Array.isArray(roles) || roles.length === 0) return false
|
||||
const names = roles
|
||||
.map((r: any) => (typeof r === 'string' ? r : r?.name))
|
||||
.filter(Boolean)
|
||||
.map((n: string) => n.toLowerCase())
|
||||
return names.some((n: string) => casdoor.adminRoles.includes(n))
|
||||
}
|
||||
|
||||
/** 通过 get-account 接口补齐完整账号信息(含邮箱/头像/角色);失败不阻塞登录 */
|
||||
async function getAccountProfile(accessToken: string): Promise<Record<string, unknown> | null> {
|
||||
const { casdoor } = getConfig()
|
||||
const url = `${casdoor.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
|
||||
}
|
||||
Reference in New Issue
Block a user