feat(auth): 用 Casdoor 校验地址令牌并完成 M02

HXOPS 打开带 token 的地址时,前端只把令牌交给后端,后端向 Casdoor userinfo 校验。D20 接受该实例上任意仍然有效的访问令牌。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
unknownandCursor committed 2026-11-17 19:41:31 +08:00
1 parent ab31d828d4
commit f8e1c77bd3
17 files changed
+322 -114

No files matched your search

+45
View File
@@ -66,6 +66,51 @@ test.describe("login shell", () => {
await expect(page.getByTestId("shell-welcome")).toHaveCount(0);
});
test("return from HXOPS with a one-time assertion signs in", async ({ page }) => {
await page.route("**/auth/me", async (route) => {
await route.fulfill({
status: 401,
contentType: "application/json",
body: JSON.stringify({
success: false,
error: { code: 41000, message: "Unauthorized", details: {}, retryable: false },
request_id: "pw-anon",
}),
});
});
let postedAssertion = "";
await page.route("**/auth/token-login", async (route) => {
postedAssertion = route.request().postData() ?? "";
await route.fulfill({
status: 200,
contentType: "application/json",
body: envelope({
subject: "casdoor-subject-1",
display_name: "高昊宇",
roles: ["watchman-admin"],
access_token: "session-token",
token_type: "Bearer",
expires_in: 28800,
}),
});
});
await page.route("**/health", async (route) => {
await route.fulfill({
status: 200,
contentType: "application/json",
body: envelope({ status: "ok", app: "watchman", version: "0.1.0" }),
});
});
await page.goto("/?token=one-time-token");
await expect(page.getByTestId("hxops-entry")).toHaveCount(0);
await expect(page.getByTestId("shell-welcome")).toContainText("已登录:高昊宇");
await expect(page.getByTestId("health-success")).toBeVisible();
expect(postedAssertion).toContain("one-time-token");
await expect(page).not.toHaveURL(/token=/);
await page.screenshot({ path: path.join(evidenceDir, "hxops-return.png"), fullPage: true });
});
test("failed sign-in stays on the form", async ({ page }) => {
await page.route("**/auth/me", async (route) => {
await route.fulfill({
+23
View File
@@ -82,6 +82,29 @@ async function signOut(): Promise<void> {
}
onMounted(async () => {
const params = new URLSearchParams(window.location.search);
const urlToken = params.get("token");
if (urlToken) {
params.delete("token");
const query = params.toString();
window.history.replaceState(null, "", query ? `/?${query}` : "/");
try {
applyIdentity(
await api<SessionUser>("/auth/token-login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token: urlToken }),
}),
);
return;
} catch (error) {
if (error instanceof BusinessError && error.status !== 401) {
detail.value = String(error.code);
}
screen.value = "login";
return;
}
}
try {
applyIdentity(await api<SessionUser>("/auth/me"));
} catch (error) {
+8
View File
@@ -6,6 +6,14 @@ declare module "*.vue" {
export default component;
}
interface ImportMetaEnv {
readonly VITE_AUTH_MODE?: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}
declare module "*.json" {
const value: Record<string, unknown>;
export default value;