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,36 @@
|
||||
import { getQuery, getCookie, deleteCookie, createError, sendRedirect } from 'h3'
|
||||
import { exchangeCodeForIdentity, getCasdoorState } from '../../utils/casdoor'
|
||||
import { upsertCasdoorUser, createSession, setSessionCookie } from '../../utils/auth'
|
||||
import { isCasdoorEnabled } from '../../utils/config'
|
||||
|
||||
/**
|
||||
* Casdoor OAuth 回调:
|
||||
* code -> 交换 access_token -> JWT 验签 -> upsert 本地用户 -> 建立会话 -> 回跳。
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
if (!isCasdoorEnabled()) {
|
||||
throw createError({ statusCode: 503, message: '未配置 Casdoor 统一认证', data: { code: 'CASDOOR_NOT_CONFIGURED' } })
|
||||
}
|
||||
|
||||
const query = getQuery(event)
|
||||
const code = String(query?.code ?? '')
|
||||
const state = String(query?.state ?? '')
|
||||
|
||||
// state 校验(SDK 以 appName 为 state;登录端点已写入同值 Cookie)
|
||||
const expectedState = getCasdoorState()
|
||||
if (expectedState && state && state !== expectedState) {
|
||||
throw createError({ statusCode: 400, message: 'state 校验失败', data: { code: 'OAUTH_STATE_MISMATCH' } })
|
||||
}
|
||||
|
||||
const identity = await exchangeCodeForIdentity(code)
|
||||
const user = upsertCasdoorUser(identity)
|
||||
const token = createSession(user.id)
|
||||
setSessionCookie(event, token)
|
||||
|
||||
const cookieNext = getCookie(event, 'an_oauth_next') || '/'
|
||||
const safeNext = cookieNext.startsWith('/') && !cookieNext.startsWith('//') ? cookieNext : '/'
|
||||
deleteCookie(event, 'an_oauth_state', { path: '/' })
|
||||
deleteCookie(event, 'an_oauth_next', { path: '/' })
|
||||
|
||||
return sendRedirect(event, safeNext)
|
||||
})
|
||||
Reference in New Issue
Block a user