Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: add 2fa required page (#5459)

boojack5f4f8cbd4166
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.go
index 33e69cbde4..693a3140ed 100644
--- a/backend/api/v1/auth_service.go
+++ b/backend/api/v1/auth_service.go
@@ -318,11 +318,13 @@ func (s *AuthService) UpdateUser(ctx context.Context, request *v1pb.UpdateUserRe
return nil, status.Errorf(codes.Internal, "failed to generate recovery codes, error: %v", err)
}
patch.MFAConfig = &storepb.MFAConfig{
- OtpSecret: user.MFAConfig.OtpSecret,
TempOtpSecret: tempSecret,
- RecoveryCodes: user.MFAConfig.RecoveryCodes,
TempRecoveryCodes: tempRecoveryCodes,
}
+ if user.MFAConfig != nil {
+ patch.MFAConfig.OtpSecret = user.MFAConfig.OtpSecret
+ patch.MFAConfig.RecoveryCodes = user.MFAConfig.RecoveryCodes
+ }
}
// This flag will update user's recovery codes with temp recovery codes.
// It will be used when user regenerate recovery codes after two phase commit.
diff --git a/frontend/src/bbkit/BBAttention.vue b/frontend/src/bbkit/BBAttention.vue
index c02f24147d..8f8f7eaf5c 100644
--- a/frontend/src/bbkit/BBAttention.vue
+++ b/frontend/src/bbkit/BBAttention.vue
@@ -1,7 +1,7 @@
<template>
<div
- class="rounded-md p-4 flex flex-col md:flex-row justify-between"
- :class="`bg-${color}-50`"
+ class="rounded-md p-4 flex flex-col md:flex-row md:items-center"
+ :class="`bg-${color}-50 ${additionalSideClasses}`"
>
<div class="flex">
<div class="flex-shrink-0">
@@ -52,7 +52,7 @@
<script lang="ts" setup>
import { computed, withDefaults } from "vue";
-import { BBAttentionStyle } from "./types";
+import { BBAttentionSide, BBAttentionStyle } from "./types";
const props = withDefaults(
defineProps<{
@@ -60,12 +60,14 @@ const props = withDefaults(
title?: string;
description?: string;
actionText?: string;
+ side?: BBAttentionSide;
}>(),
{
style: "INFO",
title: "bbkit.attention.default",
description: "",
actionText: "",
+ side: "BETWEEN",
}
);
@@ -84,4 +86,15 @@ const color = computed(() => {
return "red";
}
});
+
+const additionalSideClasses = computed(() => {
+ switch (props.side) {
+ case "BETWEEN":
+ return "justify-between";
+ case "CENTER":
+ return "justify-center";
+ default:
+ return "";
+ }
+});
</script>
diff --git a/frontend/src/bbkit/types.ts b/frontend/src/bbkit/types.ts
index 698accb6eb..23c6e7b183 100644
--- a/frontend/src/bbkit/types.ts
+++ b/frontend/src/bbkit/types.ts
@@ -95,4 +95,6 @@ export type BBAlertStyle = "INFO" | "SUCCESS" | "WARN" | "CRITICAL";
export type BBAttentionStyle = "INFO" | "WARN" | "CRITICAL";
+export type BBAttentionSide = "BETWEEN" | "CENTER";
+
export type BBAvatarSizeType = "SMALL" | "NORMAL" | "LARGE" | "HUGE";
diff --git a/frontend/src/components/GeneralSetting/SecuritySetting.vue b/frontend/src/components/GeneralSetting/SecuritySetting.vue
index 92e04d73e5..0d26059bce 100644
--- a/frontend/src/components/GeneralSetting/SecuritySetting.vue
+++ b/frontend/src/components/GeneralSetting/SecuritySetting.vue
@@ -61,8 +61,7 @@
{{ $t("settings.general.workspace.disallow-signup.description") }}
</div>
</div>
- <!-- TODO(steven): remove release guard later -->
- <div v-if="isDev" class="mb-7 mt-5 lg:mt-0">
+ <div class="mb-7 mt-5 lg:mt-0">
<label
class="flex items-center gap-x-2 tooltip-wrapper"
:class="[allowEdit ? 'cursor-pointer' : 'cursor-not-allowed']"
diff --git a/frontend/src/components/MemberTable.vue b/frontend/src/components/MemberTable.vue
index 793c83483c..3b07bf7cbd 100644
--- a/frontend/src/components/MemberTable.vue
+++ b/frontend/src/components/MemberTable.vue
@@ -114,7 +114,11 @@
</div>
</BBTableCell>
<BBTableCell class="whitespace-nowrap tooltip-wrapper w-auto">
- <div class="flex flex-row items-center">
+ <!-- Only show 2FA flag for users -->
+ <div
+ v-if="member.principal.type === 'END_USER'"
+ class="flex flex-row items-center"
+ >
2FA
<heroicons-solid:check
v-if="is2FAEnabled(member)"
diff --git a/frontend/src/layouts/DashboardLayout.vue b/frontend/src/layouts/DashboardLayout.vue
index 46ec32c44c..57cba921dc 100644
--- a/frontend/src/layouts/DashboardLayout.vue
+++ b/frontend/src/layouts/DashboardLayout.vue
@@ -25,10 +25,10 @@
<script lang="ts">
import { defineComponent } from "vue";
-import { ServerInfo } from "@/types";
import { pushNotification, useActuatorStore } from "@/store";
import ProvideDashboardContext from "@/components/ProvideDashboardContext.vue";
import BannersWrapper from "@/components/BannersWrapper.vue";
+import { ActuatorInfo } from "@/types/proto/v1/actuator_service";
export default defineComponent({
name: "DashboardLayout",
@@ -40,7 +40,7 @@ export default defineComponent({
const actuatorStore = useActuatorStore();
const ping = () => {
- actuatorStore.fetchServerInfo().then((info: ServerInfo) => {
+ actuatorStore.fetchServerInfo().then((info: ActuatorInfo) => {
pushNotification({
module: "bytebase",
style: "SUCCESS",
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index caef2d2ed5..9ffa90c666 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2076,7 +2076,9 @@
"messages": {
"2fa-enabled": "Two-factor authentication is enabled.",
"2fa-disabled": "Two-factor authentication is disabled.",
- "recovery-codes-regenerated": "Recovery codes are regenerated."
+ "recovery-codes-regenerated": "Recovery codes are regenerated.",
+ "2fa-required": "Two-factor authentication is required in your workspace. Please enable it before you can continue.",
+ "cannot-disable": "You cannot disable two-factor authentication because your admin requires it in the workspace."
}
},
"plugin": {
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 52ab0b7776..3c3bc8fd1f 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -2075,7 +2075,9 @@
"messages": {
"2fa-enabled": "双重认证开启成功",
"2fa-disabled": "双重认证禁用成功",
- "recovery-codes-regenerated": "恢复码已经重新生成"
+ "recovery-codes-regenerated": "恢复码已经重新生成",
+ "2fa-required": "您的管理员要求所有用户配置双重认证。请先配置双重认证,才能继续使用。",
+ "cannot-disable": "您的管理员要求所有用户配置双重认证。您无法禁用双重认证。"
}
},
"plugin": {
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 2e891259ef..eb8b222dd0 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -42,6 +42,7 @@ import {
useProjectStore,
useSheetStore,
useAuthStore,
+ useActuatorStore,
useDatabaseStore,
useEnvironmentStore,
useInstanceStore,
@@ -54,6 +55,7 @@ import {
useIdentityProviderStore,
useCurrentUser,
useSubscriptionStore,
+ useUserStore,
} from "@/store";
import { useConversationStore } from "@/plugins/ai/store";
import { PlanType } from "@/types/proto/v1/subscription_service";
@@ -130,6 +132,15 @@ const routes: Array<RouteRecordRaw> = [
name: "oidc-callback",
component: () => import("../views/OAuthCallback.vue"),
},
+ {
+ path: "/2fa/setup",
+ name: "2fa.setup",
+ meta: {
+ title: () => t("two-factor.self"),
+ },
+ component: () => import("../views/TwoFactorRequired.vue"),
+ props: true,
+ },
{
path: "/",
component: DashboardLayout,
@@ -1116,6 +1127,27 @@ router.beforeEach((to, from, next) => {
return;
}
+ if (to.name === "2fa.setup") {
+ next();
+ return;
+ }
+
+ const userStore = useUserStore();
+ const currentUser = useCurrentUser();
+ const serverInfo = useActuatorStore().serverInfo;
+
+ // If 2FA is required, redirect to MFA setup page if the user has not enabled 2FA.
+ if (serverInfo?.require2fa && currentUser.value) {
+ const user = userStore.getUserById(currentUser.value.id as number);
+ if (user && !user.mfaEnabled) {
+ next({
+ name: "2fa.setup",
+ replace: true,
+ });
+ return;
+ }
+ }
+
if (to.name === SQL_EDITOR_HOME_MODULE) {
const onboardingStateStore = useOnboardingStateStore();
if (onboardingStateStore.getStateByKey("sql-editor")) {
@@ -1129,8 +1161,6 @@ router.beforeEach((to, from, next) => {
}
}
- const currentUser = useCurrentUser();
-
if (to.name?.toString().startsWith("setting.workspace.im-integration")) {
if (
!hasWorkspacePermission(
diff --git a/frontend/src/views/ProfileDashboard.vue b/frontend/src/views/ProfileDashboard.vue
index 255446c0dc..ee11d38757 100644
--- a/frontend/src/views/ProfileDashboard.vue
+++ b/frontend/src/views/ProfileDashboard.vue
@@ -182,10 +182,18 @@
{{ $t("two-factor.self") }}
<FeatureBadge :feature="'bb.feature.2fa'" class="ml-2 text-accent" />
</span>
- <BBSwitch
- :value="isMFAEnabled"
- @toggle="handle2FAEnableStatusChanged"
- />
+ <div class="space-x-2">
+ <button class="btn btn-normal" @click="enable2FA">
+ {{ isMFAEnabled ? $t("common.edit") : $t("common.enable") }}
+ </button>
+ <button
+ v-if="isMFAEnabled"
+ class="btn btn-normal"
+ @click="disable2FA"
+ >
+ {{ $t("common.disable") }}
+ </button>
+ </div>
</div>
<p class="mt-4 text-sm text-gray-500">
{{ $t("two-factor.description") }}
@@ -258,12 +266,12 @@ import { hasWorkspacePermission } from "../utils";
import {
featureToRef,
pushNotification,
+ useActuatorStore,
useAuthStore,
useCurrentUser,
usePrincipalStore,
useUserStore,
} from "@/store";
-import { BBSwitch } from "@/bbkit";
import PrincipalAvatar from "../components/PrincipalAvatar.vue";
import LearnMoreLink from "@/components/LearnMoreLink.vue";
import { useRouter } from "vue-router";
@@ -286,6 +294,7 @@ const props = defineProps<{
const { t } = useI18n();
const router = useRouter();
+const actuatorStore = useActuatorStore();
const authStore = useAuthStore();
const currentUser = useCurrentUser();
const userStore = useUserStore();
@@ -396,13 +405,21 @@ const saveEdit = async () => {
state.editing = false;
};
-const handle2FAEnableStatusChanged = (enabled: boolean) => {
+const enable2FA = () => {
if (!has2FAFeature.value) {
state.showFeatureModal = true;
return;
}
- if (enabled) {
- router.push({ name: "setting.profile.two-factor" });
+ router.push({ name: "setting.profile.two-factor" });
+};
+
+const disable2FA = () => {
+ if (actuatorStore.serverInfo?.require2fa) {
+ pushNotification({
+ module: "bytebase",
+ style: "WARN",
+ title: t("two-factor.messages.cannot-disable"),
+ });
} else {
state.showDisable2FAConfirmModal = true;
}
diff --git a/frontend/src/views/TwoFactorRequired.vue b/frontend/src/views/TwoFactorRequired.vue
new file mode 100644
index 0000000000..e0e952ec7d
--- /dev/null
+++ b/frontend/src/views/TwoFactorRequired.vue
@@ -0,0 +1,27 @@
+<template>
+ <div class="w-full">
+ <BBAttention
+ :style="'WARN'"
+ :title="$t('two-factor.messages.2fa-required')"
+ side="CENTER"
+ />
+ <div class="w-full p-2 sm:p-8 sm:px-16">
+ <TwoFactorSetup :cancel-action="handleSetupCancel" />
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { useRouter } from "vue-router";
+import { useAuthStore } from "@/store";
+import TwoFactorSetup from "./TwoFactorSetup.vue";
+
+const router = useRouter();
+const authStore = useAuthStore();
+
+const handleSetupCancel = () => {
+ authStore.logout().then(() => {
+ router.push({ name: "auth.signin" });
+ });
+};
+</script>
diff --git a/frontend/src/views/TwoFactorSetup.vue b/frontend/src/views/TwoFactorSetup.vue
index b20f383dd8..c398d53a38 100644
--- a/frontend/src/views/TwoFactorSetup.vue
+++ b/frontend/src/views/TwoFactorSetup.vue
@@ -103,6 +103,13 @@ interface LocalState {
recoveryCodesDownloaded: boolean;
}
+const props = withDefaults(
+ defineProps<{
+ cancelAction?: () => void;
+ }>(),
+ {}
+);
+
const { t } = useI18n();
const router = useRouter();
const authStore = useAuthStore();
@@ -170,9 +177,13 @@ const verifyTOPCode = async () => {
};
const cancelSetup = () => {
- router.replace({
- name: "setting.profile",
- });
+ if (props.cancelAction) {
+ props.cancelAction();
+ } else {
+ router.replace({
+ name: "setting.profile",
+ });
+ }
};
const tryChangeStep = async (