feat(auth): 重构统一认证并支持平级服务无感登录

This commit is contained in:
sophie committed 2026-07-28 09:15:41 +08:00
1 parent d79def5519
commit ca312ea751
64 files changed
+3713 -449

No files matched your search

@@ -0,0 +1,73 @@
<!--
features/sso/views/SsoAuthorizeView.vue
用途:承接平级服务登录请求;未登录时转 HXOPS 登录页,已登录时签发 assertion 并 POST 回平级服务。
依赖:auth store、SSO API、continuation 与 postForm 工具。
逻辑:平级服务只提供 app/state;callback 与 JWT audience 始终由 HXOPS 服务端注册表决定。
-->
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { ArrowLeft, Loader2 } from 'lucide-vue-next';
import { useI18n } from 'vue-i18n';
import { useRoute, useRouter } from 'vue-router';
import { useAuthStore } from '@/features/auth/stores/auth.store';
import { createSsoAssertion } from '../api/sso.api';
import {
clearSsoContinuation,
parseSsoContinuation,
saveSsoContinuation,
} from '../utils/continuation';
import { submitSsoForm } from '../utils/postForm';
const { t } = useI18n();
const route = useRoute();
const router = useRouter();
const authStore = useAuthStore();
const errorMessage = ref('');
/** 校验授权请求,并按当前登录状态执行保存续跳或签发短时 assertion。 */
async function authorize(): Promise<void> {
const continuation = parseSsoContinuation(route.query);
if (!continuation) {
errorMessage.value = t('common.sso.invalidRequest');
return;
}
authStore.syncFromStorage();
if (!authStore.isLoggedIn) {
saveSsoContinuation(continuation);
await router.replace('/login');
return;
}
try {
const result = await createSsoAssertion(continuation.app, continuation.state);
clearSsoContinuation();
submitSsoForm(result.post_url, { assertion: result.assertion, state: result.state });
} catch (error) {
errorMessage.value = (error as Error)?.message || t('common.sso.failed');
}
}
onMounted(() => {
void authorize();
});
</script>
<template>
<main class="page-shell flex min-h-screen items-center justify-center px-6">
<section class="w-full max-w-md rounded-2xl border border-default bg-surface p-8 text-center shadow-xl">
<template v-if="!errorMessage">
<Loader2 class="mx-auto h-10 w-10 animate-spin text-brand-600" />
<h1 class="mt-5 text-xl font-semibold text-primary">{{ t('common.sso.authorizing') }}</h1>
<p class="mt-2 text-sm text-muted">{{ t('common.sso.authorizingHint') }}</p>
</template>
<template v-else>
<h1 class="text-xl font-semibold text-primary">{{ t('common.sso.failed') }}</h1>
<p class="mt-3 text-sm text-red-600">{{ errorMessage }}</p>
<button type="button" class="btn-secondary mt-6 inline-flex items-center gap-2" @click="router.replace('/workspace/applications')">
<ArrowLeft class="h-4 w-4" /> {{ t('common.sso.backToHxops') }}
</button>
</template>
</section>
</main>
</template>
@@ -0,0 +1,63 @@
<!--
features/sso/views/SsoLaunchView.vue
用途:在新标签先继承 HXOPS sessionStorage,再安全跳转到平级服务登录起点。
依赖:SSO 应用目录 API、Vue Router。
逻辑:页面只接收 app key,加载后立即切断 opener,目标 URL 由 HXOPS 服务端注册表提供。
-->
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { ArrowLeft, Loader2 } from 'lucide-vue-next';
import { useI18n } from 'vue-i18n';
import { useRoute, useRouter } from 'vue-router';
import { listSsoApplications } from '../api/sso.api';
const { t } = useI18n();
const route = useRoute();
const router = useRouter();
const errorMessage = ref('');
// 新标签已完成同源 sessionStorage 克隆,跳跨域前切断 opener 防止反向标签劫持。
window.opener = null;
/** 按服务端应用目录解析 app key,并跳转到平级服务登录起点。 */
async function launch(): Promise<void> {
const appKey = typeof route.query.app === 'string' ? route.query.app.trim().toLowerCase() : '';
if (!appKey) {
errorMessage.value = t('common.sso.invalidRequest');
return;
}
try {
const applications = await listSsoApplications();
const application = applications.find((item) => item.key === appKey);
if (!application) {
errorMessage.value = t('errors.codes.AUTH_SSO_APP_NOT_FOUND');
return;
}
window.location.replace(application.login_url);
} catch (error) {
errorMessage.value = (error as Error)?.message || t('common.sso.failed');
}
}
onMounted(() => {
void launch();
});
</script>
<template>
<main class="page-shell flex min-h-screen items-center justify-center px-6">
<section class="w-full max-w-md rounded-2xl border border-default bg-surface p-8 text-center shadow-xl">
<template v-if="!errorMessage">
<Loader2 class="mx-auto h-10 w-10 animate-spin text-brand-600" />
<h1 class="mt-5 text-xl font-semibold text-primary">{{ t('common.sso.authorizing') }}</h1>
</template>
<template v-else>
<h1 class="text-xl font-semibold text-primary">{{ t('common.sso.failed') }}</h1>
<p class="mt-3 text-sm text-red-600">{{ errorMessage }}</p>
<button type="button" class="btn-secondary mt-6 inline-flex items-center gap-2" @click="router.replace('/workspace/applications')">
<ArrowLeft class="h-4 w-4" /> {{ t('common.sso.backToHxops') }}
</button>
</template>
</section>
</main>
</template>