feat(auth): 重构统一认证并支持平级服务无感登录
This commit is contained in:
1 parent
d79def5519
commit
ca312ea751
64 files changed
+3713
-449
No files matched your search
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* features/sso/utils/continuation.ts
|
||||
* 用途:在平级服务跳转到 HXOPS 登录页期间保存并恢复 SSO 请求。
|
||||
* 依赖:浏览器 sessionStorage、Vue Router 查询参数类型。
|
||||
* 逻辑:只保存 app key 和签名 state,不接收或持久化 callback URL、JWT 或用户资料。
|
||||
*/
|
||||
import type { LocationQuery } from 'vue-router';
|
||||
|
||||
export interface SsoContinuation {
|
||||
app: string;
|
||||
state: string;
|
||||
createdAt: number;
|
||||
}
|
||||
|
||||
const STORAGE_KEY = 'hxops_sso_continuation';
|
||||
const MAX_AGE_MS = 10 * 60 * 1000;
|
||||
const APP_KEY_PATTERN = /^[a-z0-9][a-z0-9_-]{1,63}$/;
|
||||
|
||||
/** 从授权路由查询参数构造安全的续跳请求。 */
|
||||
export function parseSsoContinuation(query: LocationQuery): SsoContinuation | null {
|
||||
const app = typeof query.app === 'string' ? query.app.trim().toLowerCase() : '';
|
||||
const state = typeof query.state === 'string' ? query.state.trim() : '';
|
||||
if (!APP_KEY_PATTERN.test(app) || !state || state.length > 4096) return null;
|
||||
return { app, state, createdAt: Date.now() };
|
||||
}
|
||||
|
||||
/** 保存未登录用户的 SSO 请求,登录成功后在同一标签页恢复。 */
|
||||
export function saveSsoContinuation(continuation: SsoContinuation): void {
|
||||
sessionStorage.setItem(STORAGE_KEY, JSON.stringify(continuation));
|
||||
}
|
||||
|
||||
/** 读取仍在有效期内的 SSO 请求;坏数据或超时数据会立即清理。 */
|
||||
export function readSsoContinuation(): SsoContinuation | null {
|
||||
try {
|
||||
const raw = sessionStorage.getItem(STORAGE_KEY);
|
||||
if (!raw) return null;
|
||||
const data = JSON.parse(raw) as Partial<SsoContinuation>;
|
||||
if (
|
||||
!APP_KEY_PATTERN.test(String(data.app || ''))
|
||||
|| !String(data.state || '')
|
||||
|| String(data.state || '').length > 4096
|
||||
|| Date.now() - Number(data.createdAt || 0) > MAX_AGE_MS
|
||||
) {
|
||||
clearSsoContinuation();
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
app: String(data.app),
|
||||
state: String(data.state),
|
||||
createdAt: Number(data.createdAt),
|
||||
};
|
||||
} catch {
|
||||
clearSsoContinuation();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** 清理已完成或无效的登录续跳请求。 */
|
||||
export function clearSsoContinuation(): void {
|
||||
sessionStorage.removeItem(STORAGE_KEY);
|
||||
}
|
||||
|
||||
/** 生成只包含服务端应用 key 的 HXOPS 授权路由。 */
|
||||
export function continuationLocation(continuation: SsoContinuation) {
|
||||
return {
|
||||
path: '/sso/authorize',
|
||||
query: {
|
||||
app: continuation.app,
|
||||
state: continuation.state,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* features/sso/utils/postForm.ts
|
||||
* 用途:用浏览器表单 POST 将短时 SSO assertion 交给已登记的平级服务回调。
|
||||
* 依赖:DOM API。
|
||||
* 逻辑:assertion 只进入请求体,不拼接 URL;提交后浏览器离开 HXOPS 授权页。
|
||||
*/
|
||||
|
||||
/**
|
||||
* POST assertion 和 state 到后端返回的已登记回调地址。
|
||||
* @param action - HXOPS 服务端从应用注册表返回的绝对 HTTPS callback URL。
|
||||
* @param fields - 仅包含 assertion 与平级服务原始 state。
|
||||
*/
|
||||
export function submitSsoForm(action: string, fields: Record<string, string>): void {
|
||||
const form = document.createElement('form');
|
||||
form.method = 'POST';
|
||||
form.action = action;
|
||||
form.style.display = 'none';
|
||||
for (const [name, value] of Object.entries(fields)) {
|
||||
const input = document.createElement('input');
|
||||
input.type = 'hidden';
|
||||
input.name = name;
|
||||
input.value = value;
|
||||
form.appendChild(input);
|
||||
}
|
||||
document.body.appendChild(form);
|
||||
form.submit();
|
||||
}
|
||||
Reference in new issue
Block a user