JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(web): prevent enterprise auth redirect loop
whq16f8deafb4a6
diff --git a/deploy/docs/feature/2026-08-enterprise-user-web-login-auth-pluggable.md b/deploy/docs/feature/2026-08-enterprise-user-web-login-auth-pluggable.mdindex b4b0010f6e..67f24d5685 100644--- a/deploy/docs/feature/2026-08-enterprise-user-web-login-auth-pluggable.md+++ b/deploy/docs/feature/2026-08-enterprise-user-web-login-auth-pluggable.md@@ -48,7 +48,7 @@1. User Web 读取 `USER_WEB_MODE=enterprise`、`LOGIN_AUTH_SIMULATE=false`。2. Provider 注册器选择 Manager 正式认证 Provider。-3. 未发现本地 access token 时清理登录状态并跳转 `/auth`。+3. 未发现本地 access token 时清理登录状态并跳转 `/auth`。该路径由 Manager Web 或客户统一入口提供,User Web 自身不包含登录页;若 User Web 的 SPA 回退已经落到 `/auth`,则停止重复跳转并提示用户改从统一入口访问。4. 登录后依次通过同源代理获取:- `/idp/v1/auth/me`:当前用户;- `/idp/v1/auth/me/orgs`:授权组织;@@ -105,7 +105,7 @@## 验证-- 前端认证单测:`npm run test:user-web-entry`,**8/8 通过**。覆盖 personal 跳过认证、enterprise 未登录跳转、正式上下文加载、模拟默认值、URL 三元组覆盖、布尔配置严格解析、候选排序和 Agent 回退。+- 前端认证单测:`npm run test:user-web-entry`,**10/10 通过**。覆盖 personal 跳过认证、enterprise 未登录跳转、正式上下文加载、模拟默认值、URL 三元组覆盖、布尔配置严格解析、`/auth` 自跳转保护、候选排序、Agent 回退和调试上下文。- Python User Web 单测:`JIUWENSWARM_DATA_DIR=/private/tmp/jiuwenswarm-codex-auth-test jiuwenswarm/bin/python -m pytest -q tests/unit_tests/test_app_web_gateway_api_proxy.py`,**5/5 通过**。覆盖代理路由、运行配置注入及严格布尔解析。- 内部制品:`npm run build` 构建成功,Vite 转换 **4559** 个模块。- 客户制品:`npm run build:customer` 构建成功,Vite 转换 **4558** 个模块,比内部制品少模拟插件模块。diff --git a/jiuwenswarm/channels/web/frontend/src/EnterpriseEntry.tsx b/jiuwenswarm/channels/web/frontend/src/EnterpriseEntry.tsxindex 403574c689..e86a12cd58 100644--- a/jiuwenswarm/channels/web/frontend/src/EnterpriseEntry.tsx+++ b/jiuwenswarm/channels/web/frontend/src/EnterpriseEntry.tsx@@ -13,7 +13,7 @@ import {} from './services/enterpriseContext';import { parseRuntimeScope, setRuntimeScope } from './services/runtimeScope';-type EntryPhase = 'loading' | 'ready' | 'empty' | 'error' | 'redirecting';+type EntryPhase = 'loading' | 'ready' | 'empty' | 'error' | 'redirecting' | 'login-required';interface ContextCandidate {gateway: EnterpriseGateway;@@ -154,6 +154,7 @@ function errorText(error: unknown): string {function EntryStatus({ phase, error, onLogout }: { phase: EntryPhase; error: string; onLogout: () => void }) {const empty = phase === 'empty';const failed = phase === 'error';+ const loginRequired = phase === 'login-required';return (<div className="enterprise-entry"><div className="enterprise-entry__glow" />@@ -162,8 +163,26 @@ function EntryStatus({ phase, error, onLogout }: { phase: EntryPhase; error: strJIUWEN<span>CLAW</span></div><div className="enterprise-entry__eyebrow">ENTERPRISE WORKSPACE</div>- <h1>{empty ? '暂无可用 Agent' : failed ? '加载失败' : phase === 'redirecting' ? '正在前往登录页' : '正在加载工作空间'}</h1>- <p>{empty ? '当前账号没有可用的组织、组网和 Agent 组合,请联系管理员完成授权。' : failed ? error : '正在校验账号权限并选择一个可用 Agent。'}</p>+ <h1>+ {empty+ ? '暂无可用 Agent'+ : failed+ ? '加载失败'+ : loginRequired+ ? '请从统一登录入口访问'+ : phase === 'redirecting'+ ? '正在前往登录页'+ : '正在加载工作空间'}+ </h1>+ <p>+ {empty+ ? '当前账号没有可用的组织、组网和 Agent 组合,请联系管理员完成授权。'+ : failed+ ? error+ : loginRequired+ ? '当前 User Web 地址不提供登录页面,请访问正确的登录入口。'+ : '正在校验账号权限并选择一个可用 Agent。'}+ </p>{(empty || failed) && (<button type="button" className="enterprise-entry__button" onClick={onLogout}>返回登录页@@ -195,7 +214,7 @@ export function EnterpriseEntry({ children }: { children: ReactNode }) {if (!enterprise || !provider) return;console.info(provider.startupMessage);if (!provider.isAuthenticated()) {- provider.redirectToLogin();+ if (!provider.redirectToLogin()) setPhase('login-required');return;}@@ -234,7 +253,7 @@ export function EnterpriseEntry({ children }: { children: ReactNode }) {} catch (bootstrapError) {if (cancelled) return;if (bootstrapError instanceof EnterpriseAuthError && bootstrapError.status === 401) {- provider.redirectToLogin();+ if (!provider.redirectToLogin()) setPhase('login-required');return;}setError(errorText(bootstrapError));@@ -261,7 +280,7 @@ export function EnterpriseEntry({ children }: { children: ReactNode }) {activateContext(context.user.user_id, resolved, true);} catch (switchError) {if (switchError instanceof EnterpriseAuthError && switchError.status === 401) {- provider.redirectToLogin();+ if (!provider.redirectToLogin()) setPhase('login-required');return;}setContextError(errorText(switchError));diff --git a/jiuwenswarm/channels/web/frontend/src/auth/config.ts b/jiuwenswarm/channels/web/frontend/src/auth/config.tsindex 9d9ea9808c..a4aeafa378 100644--- a/jiuwenswarm/channels/web/frontend/src/auth/config.ts+++ b/jiuwenswarm/channels/web/frontend/src/auth/config.ts@@ -15,3 +15,7 @@ export function isLoginAuthSimulateEnabled(): boolean {}return parseLoginAuthSimulate(import.meta.env.VITE_LOGIN_AUTH_SIMULATE);}++export function isAuthEntryPath(pathname: string): boolean {+ return pathname === '/auth' || pathname === '/auth/';+}diff --git a/jiuwenswarm/channels/web/frontend/src/auth/manager/ManagerAuthProvider.ts b/jiuwenswarm/channels/web/frontend/src/auth/manager/ManagerAuthProvider.tsindex fab7d50fea..f6fc2a1634 100644--- a/jiuwenswarm/channels/web/frontend/src/auth/manager/ManagerAuthProvider.ts+++ b/jiuwenswarm/channels/web/frontend/src/auth/manager/ManagerAuthProvider.ts@@ -5,6 +5,7 @@ import type {EnterpriseUser,} from '../../services/enterpriseContext';import { EnterpriseAuthError, type EnterpriseAuthProvider } from '../types';+import { isAuthEntryPath } from '../config';const ACCESS_KEY = 'openjiuwen_access_token';const REFRESH_KEY = 'openjiuwen_refresh_token';@@ -66,7 +67,9 @@ export const managerAuthProvider: EnterpriseAuthProvider = {isAuthenticated: () => Boolean(accessToken()),redirectToLogin() {clearLogin();+ if (isAuthEntryPath(window.location.pathname)) return false;window.location.replace('/auth');+ return true;},getCurrentUser: () => requestJson<EnterpriseUser>('/idp/v1/auth/me'),async listOrganizations() {diff --git a/jiuwenswarm/channels/web/frontend/src/auth/simulate/SimulatedAuthProvider.ts b/jiuwenswarm/channels/web/frontend/src/auth/simulate/SimulatedAuthProvider.tsindex 9e5858c018..ef65d50cff 100644--- a/jiuwenswarm/channels/web/frontend/src/auth/simulate/SimulatedAuthProvider.ts+++ b/jiuwenswarm/channels/web/frontend/src/auth/simulate/SimulatedAuthProvider.ts@@ -38,7 +38,10 @@ export const simulatedAuthProvider: EnterpriseAuthProvider = {id: 'simulate',startupMessage: '【登录认证模拟调试模式已开启】使用默认用户、组织、组网和 Agent 候选值',isAuthenticated: () => true,- redirectToLogin: () => window.location.replace(entryPath()),+ redirectToLogin: () => {+ window.location.replace(entryPath());+ return true;+ },getCurrentUser: async () => buildSimulatedEnterpriseContext(window.location.search).user,listOrganizations: async () => buildSimulatedEnterpriseContext(window.location.search).orgs,listGateways: async () => buildSimulatedEnterpriseContext(window.location.search).gateways,diff --git a/jiuwenswarm/channels/web/frontend/src/auth/types.ts b/jiuwenswarm/channels/web/frontend/src/auth/types.tsindex dd979be547..599858e63f 100644--- a/jiuwenswarm/channels/web/frontend/src/auth/types.ts+++ b/jiuwenswarm/channels/web/frontend/src/auth/types.ts@@ -9,7 +9,8 @@ export interface EnterpriseAuthProvider {readonly id: 'manager' | 'simulate';readonly startupMessage: string;isAuthenticated(): boolean;- redirectToLogin(): void;+ /** Return false when the User Web itself has already fallen back to /auth. */+ redirectToLogin(): boolean;getCurrentUser(): Promise<EnterpriseUser>;listOrganizations(): Promise<EnterpriseOrg[]>;listGateways(): Promise<EnterpriseGateway[]>;diff --git a/jiuwenswarm/channels/web/frontend/tests/userWebEntry.test.mjs b/jiuwenswarm/channels/web/frontend/tests/userWebEntry.test.mjsindex c181c6eb5a..ce83a35e60 100644--- a/jiuwenswarm/channels/web/frontend/tests/userWebEntry.test.mjs+++ b/jiuwenswarm/channels/web/frontend/tests/userWebEntry.test.mjs@@ -28,7 +28,10 @@ const {isDebugContext,orderedContextCandidates,} = await import('../node_modules/.cache/user-web-entry/EnterpriseEntry.mjs');-const { parseLoginAuthSimulate } = await import('../node_modules/.cache/user-web-entry/auth/config.js');+const {+ isAuthEntryPath,+ parseLoginAuthSimulate,+} = await import('../node_modules/.cache/user-web-entry/auth/config.js');const { buildSimulatedEnterpriseContext } = await import('../node_modules/.cache/user-web-entry/auth/simulate/SimulatedAuthProvider.js');function renderEntry(edition, simulate = false) {@@ -39,6 +42,7 @@ function renderEntry(edition, simulate = false) {function resetBrowserState() {values.clear();+ window.location.pathname = '/';window.location.search = '';}@@ -97,6 +101,12 @@ test('LOGIN_AUTH_SIMULATE accepts only booleans and defaults to true', () => {assert.throws(() => parseLoginAuthSimulate('yes'), /期望 true 或 false/);});+test('auth entry path guard stops User Web from redirecting /auth to itself', () => {+ assert.equal(isAuthEntryPath('/auth'), true);+ assert.equal(isAuthEntryPath('/auth/'), true);+ assert.equal(isAuthEntryPath('/chat/'), false);+});+test('context candidates prefer URL values but retain every authorized combination', () => {const gateways = [{ jiuwenclaw_id: 'gateway-1', jiuwenclaw_name: 'Gateway 1', gateway_endpoint: null },