第二版:casdoor登录,保留用户密码登录
This commit is contained in:
1 parent
a5f0ec645d
commit
61fa96d686
13 files changed
+701
-39
No files matched your search
@@ -20,7 +20,7 @@ import {
|
||||
type WecomLoginDialogContent,
|
||||
type WecomMatchField,
|
||||
} from '../utils/wecom';
|
||||
import { getAuthHeaders, getStoredAuthUser } from '../utils/auth';
|
||||
import { getAuthHeaders, getStoredAuthUser, saveAuthUser } from '../utils/auth';
|
||||
import {
|
||||
Settings,
|
||||
LayoutGrid,
|
||||
@@ -382,18 +382,7 @@ const loadAuthUser = () => {
|
||||
};
|
||||
|
||||
const persistLoginUser = (user: any) => {
|
||||
const nextUser = { ...user };
|
||||
if (user?.access_token && !user?.token) {
|
||||
nextUser.token = user.access_token;
|
||||
}
|
||||
if (rememberMe.value) {
|
||||
const expiry = new Date().getTime() + 30 * 24 * 60 * 60 * 1000;
|
||||
localStorage.setItem('hxops_user', JSON.stringify({ user: nextUser, expiry }));
|
||||
sessionStorage.removeItem('hxops_user');
|
||||
} else {
|
||||
sessionStorage.setItem('hxops_user', JSON.stringify({ user: nextUser }));
|
||||
localStorage.removeItem('hxops_user');
|
||||
}
|
||||
saveAuthUser({ ...(user || {}) }, rememberMe.value);
|
||||
};
|
||||
|
||||
const handleLogin = async () => {
|
||||
@@ -447,6 +436,86 @@ const handleLogin = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleCasdoorLogin = async () => {
|
||||
if (!config.auth?.casdoor?.enabled) return;
|
||||
try {
|
||||
// Ask backend to build a server-side-tracked authorize URL (includes state).
|
||||
const api = config.auth.casdoor.authorizeApi || '/api/user/casdoor/authorize-url';
|
||||
const callbackPath = config.auth.casdoor.callbackPath || '/casdoor-callback';
|
||||
const redirectUri = `${window.location.origin}${callbackPath}`;
|
||||
const res = await fetch(`${api}?redirect_uri=${encodeURIComponent(redirectUri)}`);
|
||||
const data = await res.json();
|
||||
if (!res.ok || !data.url) {
|
||||
showMessageDialog('error', data.error || t('login.requestFailed'));
|
||||
return;
|
||||
}
|
||||
if (data.state) {
|
||||
sessionStorage.setItem('hxops_casdoor_state', data.state);
|
||||
}
|
||||
window.location.href = data.url;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
showMessageDialog('error', t('login.requestFailed'));
|
||||
}
|
||||
};
|
||||
|
||||
const tryCasdoorCallbackLogin = async () => {
|
||||
if (!config.auth?.casdoor?.enabled) return false;
|
||||
const casdoorPath = config.auth.casdoor.callbackPath || '/casdoor-callback';
|
||||
const path = window.location.pathname;
|
||||
// Compatibility: allow temporary callback landing on /workspace during migration.
|
||||
if (path !== casdoorPath && path !== '/workspace') return false;
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const code = params.get('code');
|
||||
const state = params.get('state');
|
||||
if (!code) return false;
|
||||
|
||||
const expectedState = sessionStorage.getItem('hxops_casdoor_state');
|
||||
// Backward compatibility: old backend emitted random in-memory state
|
||||
// (hxops_casdoor_*), while new backend emits signed stateless state.
|
||||
// For new signed state, rely on backend signature verification instead of
|
||||
// strict frontend equality check to avoid false negatives during rollout.
|
||||
const shouldStrictCheck = !!expectedState && !!state && !String(state).startsWith('hxops_casdoor_v1.');
|
||||
if (shouldStrictCheck && state !== expectedState) {
|
||||
// Prevent CSRF/state mismatch replay.
|
||||
showMessageDialog('error', 'Invalid or expired Casdoor state');
|
||||
clearWecomCallbackQuery();
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
const api = config.auth.casdoor.loginApi || '/api/user/casdoor/login';
|
||||
const res = await fetch(api, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ code, state }),
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) {
|
||||
const code = String(data.error_code || '');
|
||||
let msg = data.error || 'Casdoor login failed';
|
||||
if (code === 'CASDOOR_USER_NOT_FOUND') {
|
||||
msg = 'Casdoor 登录成功,但未匹配到本地账号。请让管理员在本地用户表创建同名 username 或相同 email 的账号。';
|
||||
} else if (code === 'ACCOUNT_DISABLED') {
|
||||
msg = '该本地账号已被禁用,请联系管理员启用后再登录。';
|
||||
}
|
||||
showMessageDialog('error', msg);
|
||||
clearWecomCallbackQuery();
|
||||
return true;
|
||||
}
|
||||
// Persist access+refresh tokens for API auth and silent refresh.
|
||||
persistLoginUser(data);
|
||||
sessionStorage.removeItem('hxops_casdoor_state');
|
||||
window.history.replaceState({}, '', '/workspace');
|
||||
await fetchHome();
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
showMessageDialog('error', 'Casdoor login failed');
|
||||
clearWecomCallbackQuery();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
const handleWecomLogin = async () => {
|
||||
if (!config.auth?.wecom?.enabled) return;
|
||||
try {
|
||||
@@ -549,6 +618,8 @@ const submitWecomCompleteBind = async () => {
|
||||
|
||||
const tryWecomCallbackLogin = async () => {
|
||||
if (!config.auth?.wecom?.enabled) return false;
|
||||
const wecomPath = config.auth.wecom.callbackPath || '/workspace';
|
||||
if (window.location.pathname !== wecomPath) return false;
|
||||
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const code = params.get('code');
|
||||
@@ -862,6 +933,10 @@ onMounted(async () => {
|
||||
document.addEventListener('touchstart', handleDocumentPointerDown, { passive: true });
|
||||
loadLanguagePreference();
|
||||
fetchAnnouncements();
|
||||
const handledCasdoor = await tryCasdoorCallbackLogin();
|
||||
if (handledCasdoor && currentUser.value && currentUser.value.role !== 'guest') {
|
||||
return;
|
||||
}
|
||||
const handledWecom = await tryWecomCallbackLogin();
|
||||
if (handledWecom && currentUser.value && currentUser.value.role !== 'guest') {
|
||||
return;
|
||||
@@ -1069,6 +1144,14 @@ onBeforeUnmount(() => {
|
||||
{{ $t('login.submit') }}
|
||||
</button>
|
||||
|
||||
<button
|
||||
v-if="config.auth?.casdoor?.enabled"
|
||||
@click="handleCasdoorLogin"
|
||||
class="btn-secondary w-full py-3 text-base mt-3"
|
||||
>
|
||||
{{ $t('login.casdoorLogin') }}
|
||||
</button>
|
||||
|
||||
<button
|
||||
v-if="config.auth?.wecom?.enabled"
|
||||
@click="handleWecomLogin"
|
||||
|
||||
Reference in new issue
Block a user