From fbf12f16b5eaa7676fd41923587bf6bd2595adba Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Mon, 1 Dec 2025 00:40:46 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=AF=E6=8C=81OIDC=E5=8D=95?= =?UTF-8?q?=E7=82=B9=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/locales/langs/en-US/authentication.ts | 2 + .../src/locales/langs/en-US/certd.ts | 10 ++ .../src/locales/langs/zh-CN/authentication.ts | 3 + .../src/locales/langs/zh-CN/certd.ts | 9 ++ .../src/store/settings/api.basic.ts | 7 + .../certd-client/src/store/user/api.user.ts | 7 + .../ui/certd-client/src/store/user/index.ts | 30 +++- .../src/views/certd/mine/user-profile.vue | 4 +- .../src/views/framework/login/index.vue | 137 ++++++++-------- .../views/framework/oauth/oauth-callback.vue | 9 +- .../views/framework/oauth/oauth-footer.vue | 23 ++- .../src/views/sys/settings/index.vue | 8 +- .../src/views/sys/settings/tabs/oauth.vue | 150 ++++++++++++++++++ .../src/views/sys/settings/tabs/register.vue | 66 -------- .../basic/login/oauth-controller.ts | 34 ++-- .../src/plugins/plugin-oauth/api.ts | 8 + .../plugins/plugin-oauth/oidc/plugin-oidc.ts | 10 +- .../src/plugins/plugin-oauth/wx/plugin-wx.ts | 7 +- 18 files changed, 371 insertions(+), 153 deletions(-) create mode 100644 packages/ui/certd-client/src/views/sys/settings/tabs/oauth.vue diff --git a/packages/ui/certd-client/src/locales/langs/en-US/authentication.ts b/packages/ui/certd-client/src/locales/langs/en-US/authentication.ts index 68bd1cb2a..4bd0bbb91 100644 --- a/packages/ui/certd-client/src/locales/langs/en-US/authentication.ts +++ b/packages/ui/certd-client/src/locales/langs/en-US/authentication.ts @@ -84,4 +84,6 @@ export default { phoneNumber: "Phone Number", changePassword: "Change Password", updateProfile: "Update Profile", + oauthLoginTitle: "Other ways of login", + oauthOnlyLoginTitle: "Login", }; diff --git a/packages/ui/certd-client/src/locales/langs/en-US/certd.ts b/packages/ui/certd-client/src/locales/langs/en-US/certd.ts index 00019b232..33769a8be 100644 --- a/packages/ui/certd-client/src/locales/langs/en-US/certd.ts +++ b/packages/ui/certd-client/src/locales/langs/en-US/certd.ts @@ -743,6 +743,8 @@ export default { paymentSetting: "Payment Settings", captchaSetting: "Captcha Setting", pipelineSetting: "Pipeline Settings", + oauthSetting: "OAuth2 Settings", + showRunStrategy: "Show RunStrategy", showRunStrategyHelper: "Allow modify the run strategy of the task", @@ -770,6 +772,14 @@ export default { oauthCallback: "Callback URL", oauthCallbackHelper: "Copy this URL to the callback address of the OAuth2 login provider", oauthCallbackCopy: "Copy Callback URL", + oauthAutoRegister: "Auto Register User", + oauthAutoRegisterCheckedText: "Auto Register", + oauthAutoRegisterUnCheckedText: "User Select", + oauthAutoRegisterHelper: "Whether to auto register user when login", + oauthAutoRedirect: "Auto Redirect to OAuth2 Login", + oauthAutoRedirectHelper: "Whether to auto redirect to OAuth2 login when login (using the first enabled OAuth2 login type)", + oauthOnly: "OAuth2 Login Only", + oauthOnlyHelper: "Whether to only allow OAuth2 login, disable password login", }, }, modal: { diff --git a/packages/ui/certd-client/src/locales/langs/zh-CN/authentication.ts b/packages/ui/certd-client/src/locales/langs/zh-CN/authentication.ts index 35a325db4..9f0594eb9 100644 --- a/packages/ui/certd-client/src/locales/langs/zh-CN/authentication.ts +++ b/packages/ui/certd-client/src/locales/langs/zh-CN/authentication.ts @@ -85,4 +85,7 @@ export default { phoneNumber: "手机号", changePassword: "修改密码", updateProfile: "修改个人信息", + + oauthLoginTitle: "其他登录方式", + oauthOnlyLoginTitle: "登录", }; diff --git a/packages/ui/certd-client/src/locales/langs/zh-CN/certd.ts b/packages/ui/certd-client/src/locales/langs/zh-CN/certd.ts index 3e368a21d..5fe7bc2f7 100644 --- a/packages/ui/certd-client/src/locales/langs/zh-CN/certd.ts +++ b/packages/ui/certd-client/src/locales/langs/zh-CN/certd.ts @@ -743,6 +743,7 @@ export default { paymentSetting: "支付设置", captchaSetting: "验证码设置", pipelineSetting: "流水线设置", + oauthSetting: "第三方登录", showRunStrategy: "显示运行策略选择", showRunStrategyHelper: "任务设置中是否允许选择运行策略", @@ -771,6 +772,14 @@ export default { oauthCallback: "回调地址", oauthCallbackHelper: "复制回调地址,配置到对应提供商的回调地址中", oauthCallbackCopy: "复制回调地址", + oauthAutoRegister: "自动注册用户", + oauthAutoRegisterHelper: "当第三方账户未绑定本站账号时,是否自动注册用户,默认由用户选择", + oauthAutoRegisterCheckedText: "自动注册", + oauthAutoRegisterUnCheckedText: "用户选择", + oauthAutoRedirect: "自动跳转第三方登录", + oauthAutoRedirectHelper: "是否自动跳转第三方登录(使用第一个已启用的第三方登录类型)", + oauthOnly: "仅使用第三方登录", + oauthOnlyHelper: "是否仅使用第三方登录,关闭密码登录(注意:请务必在测试第三方登录功能正常后再开启)", }, }, modal: { diff --git a/packages/ui/certd-client/src/store/settings/api.basic.ts b/packages/ui/certd-client/src/store/settings/api.basic.ts index c041db411..b22a70bfa 100644 --- a/packages/ui/certd-client/src/store/settings/api.basic.ts +++ b/packages/ui/certd-client/src/store/settings/api.basic.ts @@ -62,6 +62,13 @@ export type SysPublicSetting = { // 第三方OAuth配置 oauthEnabled?: boolean; + // 是否自动注册用户 + oauthAutoRegister?: boolean; + // 是否自动跳转第三方登录 + oauthAutoRedirect?: boolean; + // 是否仅允许使用第三方登录 + oauthOnly?: boolean; + // 第三方OAuth登录提供者配置 oauthProviders?: Record< string, { diff --git a/packages/ui/certd-client/src/store/user/api.user.ts b/packages/ui/certd-client/src/store/user/api.user.ts index c37202a5e..c4ce1fb9d 100644 --- a/packages/ui/certd-client/src/store/user/api.user.ts +++ b/packages/ui/certd-client/src/store/user/api.user.ts @@ -100,3 +100,10 @@ export async function loginByTwoFactor(data: any) { data, }); } + +export async function OauthProviders() { + return await request({ + url: "/oauth/providers", + method: "post", + }); +} diff --git a/packages/ui/certd-client/src/store/user/index.ts b/packages/ui/certd-client/src/store/user/index.ts index cca8e8487..2cd47a0d5 100644 --- a/packages/ui/certd-client/src/store/user/index.ts +++ b/packages/ui/certd-client/src/store/user/index.ts @@ -14,6 +14,7 @@ import { mitter } from "/src/utils/util.mitt"; import { resetAllStores, useAccessStore } from "/@/vben/stores"; import { useUserStore as vbenUserStore } from "/@/vben/stores/modules/user"; +import { request } from "/@/api/service"; interface UserState { userInfo: Nullable; @@ -116,15 +117,38 @@ export const useUserStore = defineStore({ * @description: logout */ async logout(goLogin = true, from401 = false) { + if (!from401 && this.getToken) { + try { + await UserApi.logout(); //主要是清空cookie + } catch (e) { + console.error("注销登录请求失败:", e); + } + } + this.resetState(); resetAllStores(); - if (!from401) { - await UserApi.logout(); //主要是清空cookie - } + // 第三方登录注销 + await this.oauthLogout(); goLogin && router.push("/login"); mitter.emit("app.logout"); }, + async oauthLogout() { + const providers = await UserApi.OauthProviders(); + for (const provider of providers) { + if (provider.logoutUrl) { + try { + await request({ + url: provider.logoutUrl, + method: "get", + withCredentials: true, + }); + } catch (e) { + console.error("注销第三方登录失败:", e); + } + } + } + }, /** * @description: Confirm before logging out */ diff --git a/packages/ui/certd-client/src/views/certd/mine/user-profile.vue b/packages/ui/certd-client/src/views/certd/mine/user-profile.vue index 5f00852c7..5f5a7bc77 100644 --- a/packages/ui/certd-client/src/views/certd/mine/user-profile.vue +++ b/packages/ui/certd-client/src/views/certd/mine/user-profile.vue @@ -16,10 +16,10 @@ {{ userInfo.email }} {{ userInfo.phoneCode }}{{ userInfo.mobile }} -
+
{{ item.title }} - 解绑 + 解绑 绑定
diff --git a/packages/ui/certd-client/src/views/framework/login/index.vue b/packages/ui/certd-client/src/views/framework/login/index.vue index c513ea5b8..9d0b44449 100644 --- a/packages/ui/certd-client/src/views/framework/login/index.vue +++ b/packages/ui/certd-client/src/views/framework/login/index.vue @@ -2,71 +2,74 @@ + diff --git a/packages/ui/certd-client/src/views/sys/settings/tabs/register.vue b/packages/ui/certd-client/src/views/sys/settings/tabs/register.vue index ab8c1c420..5912d7868 100644 --- a/packages/ui/certd-client/src/views/sys/settings/tabs/register.vue +++ b/packages/ui/certd-client/src/views/sys/settings/tabs/register.vue @@ -55,45 +55,6 @@ - - -
- - -
-
- -
- - - - - - - - - - - - - - - -
{{ t("certd.sys.setting.oauthType") }}{{ t("certd.sys.setting.oauthCallback") }}{{ t("certd.sys.setting.oauthConfig") }}
-
- - {{ item.title }} -
-
- - {{ t("certd.sys.setting.oauthCallbackCopy") }} - - - -
-
-
- {{ t("certd.saveButton") }} @@ -105,7 +66,6 @@ import { notification } from "ant-design-vue"; import { merge } from "lodash-es"; import { reactive, ref, Ref } from "vue"; -import AddonSelector from "../../../certd/addon/addon-selector/index.vue"; import { useSettingStore } from "/@/store/settings"; import * as api from "/@/views/sys/settings/api"; import { SysSettings } from "/@/views/sys/settings/api"; @@ -196,26 +156,6 @@ async function loadTypeDefine(type: string) { smsTypeDefineInputs.value = inputs; } -const oauthProviders = ref([]); -async function loadOauthProviders() { - oauthProviders.value = await api.GetOauthProviders(); -} - -function fillOauthProviders(form: any) { - const providers: any = {}; - for (const item of oauthProviders.value) { - const type = item.name; - providers[type] = { - type: type, - title: item.title, - icon: item.icon, - addonId: item.addonId || null, - }; - } - form.public.oauthProviders = providers; - return providers; -} - async function loadSysSettings() { const data: any = await api.SysSettingsGet(); merge(formState, data); @@ -230,7 +170,6 @@ async function loadSysSettings() { if (!settingsStore.isComm) { formState.public.smsLoginEnabled = false; } - await loadOauthProviders(); } const saveLoading = ref(false); @@ -239,7 +178,6 @@ const settingsStore = useSettingStore(); const onFinish = async (form: any) => { try { saveLoading.value = true; - fillOauthProviders(form); await api.SysSettingsSave(form); await settingsStore.loadSysSettings(); notification.success({ @@ -249,10 +187,6 @@ const onFinish = async (form: any) => { saveLoading.value = false; } }; - -function buildCallbackUrl(type: string) { - return `${window.location.origin}/api/oauth/callback/${type}`; -}