Skip to content

Commit ffcb514

Browse files
author
zhangtao
committed
Merge branch 'dev' into master
2 parents 9bf1170 + 0d84b09 commit ffcb514

4 files changed

Lines changed: 40 additions & 21 deletions

File tree

‎backend/app/modules/system/auth/oauth_service.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def _frontend_bound_redirect(frontend_base: str, provider: str) -> str:
5757
return f"{frontend_base}{sep}oauth_bound={quote(provider, safe='')}"
5858

5959

60+
def _frontend_session_redirect(frontend_base: str) -> str:
61+
"""OAuth 成功后只回跳会话标记;access 由前端用 HttpOnly Cookie 静默刷新。"""
62+
sep = "&" if "?" in frontend_base else "?"
63+
return f"{frontend_base}{sep}oauth=1"
64+
65+
6066
def _frontend_success_redirect(frontend_base: str, access_token: str, refresh_token: str | None, token_type: str) -> str:
6167
payload = {"access_token": access_token, "token_type": token_type}
6268
if refresh_token:
@@ -615,10 +621,11 @@ async def _frontend() -> str:
615621
return _frontend_bound_redirect(frontend, provider)
616622
if on_token:
617623
on_token(token.refresh_token)
624+
# Web:token 不进 URL,前端靠 HttpOnly Cookie 静默恢复
625+
return _frontend_session_redirect(frontend)
618626
return _frontend_success_redirect(
619627
frontend,
620628
token.access_token,
621-
# Web 的 refresh token 已写入 HttpOnly Cookie,不再进入 URL
622-
None if on_token else token.refresh_token,
629+
token.refresh_token,
623630
token.token_type,
624631
)

‎backend/tests/test_oauth_provider.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ class T:
161161
access_token="a"; refresh_token="r"; token_type="bearer"
162162
async def ok(**kw): return T(), "https://f"
163163
monkeypatch.setattr(o,"complete_oauth_login",ok)
164-
out = await o.finish_oauth_login(request=object(),redis=object(),db=object(),provider="github",code="c",state="s")
165-
assert "access_token=a" in out
164+
out = await o.finish_oauth_login(request=object(),redis=object(),db=object(),provider="github",code="c",state="s",on_token=lambda _: None)
165+
assert "oauth=1" in out and "access_token" not in out
166166

167167

168168
async def test_start_oauth_login_success(monkeypatch: pytest.MonkeyPatch) -> None:

‎frontend/web/src/utils/auth/index.ts‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,12 @@ export async function redirectToLogin(message: string = "请重新登录"): Prom
109109
// ── OAuth 第三方登录 ──
110110

111111
/**
112-
* 打开小弹窗完成 OAuth;回调页会把结果 postMessage 给当前窗口并自动关闭。
112+
* 整页跳转至后端 OAuth 入口;回调仅带 oauth=1,前端用 HttpOnly Cookie 恢复会话。
113113
*/
114114
export function startOAuthLogin(provider: OAuthProvider): void {
115115
const base = (import.meta.env.VITE_APP_BASE_API || "/api/v1").replace(/\/$/, "");
116116
const basePath = (import.meta.env.BASE_URL || "/").replace(/\/$/, "");
117-
const redirectUri = `${window.location.origin}${basePath}/#/login?oauth_popup=1`;
117+
const redirectUri = `${window.location.origin}${basePath}/#/login`;
118118
const url = `${base}/system/auth/oauth/${provider}/login?redirect_uri=${encodeURIComponent(redirectUri)}`;
119-
const width = 520;
120-
const height = 680;
121-
const left = window.screenX + Math.max(0, (window.outerWidth - width) / 2);
122-
const top = window.screenY + Math.max(0, (window.outerHeight - height) / 2);
123-
const popup = window.open(
124-
"about:blank",
125-
"fastapiadmin-oauth",
126-
`popup=yes,width=${width},height=${height},left=${left},top=${top},resizable=yes,scrollbars=yes`
127-
);
128-
if (!popup) {
129-
ElMessage.warning("浏览器阻止了授权弹窗,请允许本站弹出窗口");
130-
return;
131-
}
132-
popup.location.href = url;
119+
window.location.href = url;
133120
}

‎frontend/web/src/views/module_system/auth/login/index.vue‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,9 @@ async function tryConsumeOAuthCallback() {
332332
const q = route.query;
333333
const oauthError = q.oauth_error as string | undefined;
334334
const access = q.access_token as string | undefined;
335+
const oauthSession = q.oauth === "1";
335336
336-
if (!oauthError && !access) return;
337+
if (!oauthError && !access && !oauthSession) return;
337338
338339
// OAuth 弹窗:跨域授权后 opener 可能被 COOP 断开,用同源 localStorage 事件回传。
339340
if (window.name === "fastapiadmin-oauth" || q.oauth_popup === "1") {
@@ -355,6 +356,7 @@ async function tryConsumeOAuthCallback() {
355356
delete rest.access_token;
356357
delete rest.refresh_token;
357358
delete rest.token_type;
359+
delete rest.oauth;
358360
delete rest.oauth_popup;
359361
360362
if (oauthError) {
@@ -363,6 +365,29 @@ async function tryConsumeOAuthCallback() {
363365
return;
364366
}
365367
368+
if (oauthSession) {
369+
try {
370+
await userStore.refreshTokenFn();
371+
await userStore.getUserInfo();
372+
await configStore.getConfig(true);
373+
userStore.setLoginStatus(true);
374+
ElNotification({
375+
title: t("login.oauthNoticeTitle"),
376+
message: t("login.oauthLoginSuccess"),
377+
type: "success",
378+
});
379+
await router.replace(oauthRedirectTarget(rest as Record<string, unknown>));
380+
if (settingStore.showGuide) {
381+
appStore.showGuide(true);
382+
}
383+
} catch (error) {
384+
console.error("[Login] OAuth session:", error);
385+
ElMessage.error(t("login.oauthLoginFailed"));
386+
await router.replace({ path: route.path, query: rest as LocationQuery });
387+
}
388+
return;
389+
}
390+
366391
if (access) {
367392
try {
368393
// refresh token 由后端写入 HttpOnly Cookie,URL 不再携带

0 commit comments

Comments
 (0)