Skip to content

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

chore: display login with VCS (#4545)

boojack0c2fb685b174
diff --git a/frontend/src/types/oauth.ts b/frontend/src/types/oauth.ts
index 3e0268003b..79f23f23f2 100644
--- a/frontend/src/types/oauth.ts
+++ b/frontend/src/types/oauth.ts
@@ -1,5 +1,4 @@
import { VCSType } from ".";
-import { randomString } from "../utils";
export type OAuthConfig = {
endpoint: string;
@@ -41,7 +40,7 @@ export function openWindowForOAuth(
vcsType: VCSType
): Window | null {
// we use type to determine oauth type when receiving the callback
- const stateQueryParameter = `${type}.${vcsType}-${randomString(20)}`;
+ const stateQueryParameter = `${type}.${vcsType}-${applicationId}`;
sessionStorage.setItem(OAuthStateSessionKey, stateQueryParameter);
if (vcsType == "GITHUB_COM") {
diff --git a/frontend/src/views/auth/Signin.vue b/frontend/src/views/auth/Signin.vue
index 51b833e4e5..e0e84069b4 100644
--- a/frontend/src/views/auth/Signin.vue
+++ b/frontend/src/views/auth/Signin.vue
@@ -6,6 +6,56 @@
alt="Bytebase"
/>
+ <div class="mt-8 mb-3">
+ <template
+ v-for="authProvider in authProviderList"
+ :key="authProvider.type"
+ >
+ <button
+ type="button"
+ class="btn-normal flex justify-center w-full h-10 mb-2 tooltip-wrapper"
+ :disabled="!has3rdPartyLoginFeature"
+ @click="trySigninWithOAuth(authProvider)"
+ >
+ <img
+ class="w-5 mr-1"
+ :src="AuthProviderConfig[authProvider.type].iconPath"
+ />
+ <span class="text-center font-semibold align-middle">
+ {{
+ authProviderList.length == 1
+ ? authProvider.type.includes("GITHUB")
+ ? $t("auth.sign-in.github")
+ : $t("auth.sign-in.gitlab")
+ : authProvider.name
+ }}
+ </span>
+ <span v-if="isDemo" class="tooltip">{{
+ $t("auth.sign-in.3rd-party-auth-demo")
+ }}</span>
+ <span v-else-if="!has3rdPartyLoginFeature" class="tooltip">{{
+ $t("subscription.features.bb-feature-3rd-party-auth.login")
+ }}</span>
+ </button>
+ </template>
+
+ <template v-if="authProviderList.length == 0">
+ <button
+ disabled
+ type="button"
+ class="btn-normal flex justify-center w-full h-10 mb-2"
+ >
+ <img
+ class="w-5 mr-1"
+ :src="AuthProviderConfig['GITLAB_SELF_HOST'].iconPath"
+ />
+ <span class="text-center font-semibold align-middle">
+ {{ $t("auth.sign-in.gitlab-oauth") }}
+ </span>
+ </button>
+ </template>
+ </div>
+
<div class="mt-4">
<form class="space-y-6" @submit.prevent="trySignin">
<div>
@@ -151,7 +201,11 @@
import { computed, onMounted, onUnmounted, reactive, watch } from "vue";
import { useRouter } from "vue-router";
import { storeToRefs } from "pinia";
-import { OAuthWindowEventPayload } from "@/types";
+import {
+ AuthProvider,
+ OAuthWindowEventPayload,
+ openWindowForOAuth,
+} from "@/types";
import { isValidEmail, openWindowForSSO } from "@/utils";
import {
featureToRef,
@@ -165,10 +219,24 @@ import {
} from "@/types/proto/v1/idp_service";
import AuthFooter from "./AuthFooter.vue";
+const AuthProviderConfig = {
+ GITLAB_SELF_HOST: {
+ apiPath: "oauth/authorize",
+ // see https://vitejs.cn/guide/assets.html#the-public-directory for static resource import during run time
+ iconPath: new URL("../../assets/gitlab-logo.svg", import.meta.url).href,
+ },
+ GITHUB_COM: {
+ apiPath: "login/oauth/authorize",
+ // see https://vitejs.cn/guide/assets.html#the-public-directory for static resource import during run time
+ iconPath: new URL("../../assets/github-logo.svg", import.meta.url).href,
+ },
+};
+
interface LocalState {
email: string;
password: string;
showPassword: boolean;
+ activeAuthProvider?: AuthProvider;
activeIdentityProvider?: IdentityProvider;
}
@@ -177,6 +245,8 @@ const authStore = useAuthStore();
const identityProviderStore = useIdentityProviderStore();
const router = useRouter();
+const { authProviderList } = storeToRefs(authStore);
+
const state = reactive<LocalState>({
email: "",
password: "",
@@ -202,10 +272,18 @@ onMounted(async () => {
state.showPassword = true;
}
+ await authStore.fetchProviderList();
await identityProviderStore.fetchIdentityProviderList();
});
onUnmounted(() => {
+ if (state.activeAuthProvider) {
+ window.removeEventListener(
+ `bb.oauth.signin.${state.activeAuthProvider?.type}-${state.activeAuthProvider?.applicationId}`,
+ loginWithVCSEventListener,
+ false
+ );
+ }
if (state.activeIdentityProvider) {
window.removeEventListener(
`bb.oauth.signin.${state.activeIdentityProvider.name}`,
@@ -231,6 +309,42 @@ watch(
}
);
+watch(
+ () => state.activeAuthProvider,
+ (newValue, oldValue) => {
+ window.removeEventListener(
+ `bb.oauth.signin.${oldValue?.type}-${oldValue?.applicationId}`,
+ loginWithVCSEventListener,
+ false
+ );
+ window.addEventListener(
+ `bb.oauth.signin.${newValue?.type}-${newValue?.applicationId}`,
+ loginWithVCSEventListener,
+ false
+ );
+ }
+);
+
+const loginWithVCSEventListener = (event: Event) => {
+ if (!state.activeAuthProvider) {
+ return;
+ }
+
+ const payload = (event as CustomEvent).detail as OAuthWindowEventPayload;
+ if (payload.error) {
+ return;
+ }
+ authStore
+ .vcsLogin(state.activeAuthProvider.type, {
+ vcsId: state.activeAuthProvider.id,
+ name: state.activeAuthProvider.name,
+ code: payload.code,
+ })
+ .then(() => {
+ router.push("/");
+ });
+};
+
const loginWithIdentityProviderEventListener = async (event: Event) => {
if (!state.activeIdentityProvider) {
return;
@@ -271,6 +385,22 @@ const trySignin = async () => {
router.push("/");
};
+const trySigninWithOAuth = (authProvider: AuthProvider) => {
+ if (authProvider.type == "BYTEBASE") {
+ return;
+ }
+
+ state.activeAuthProvider = authProvider;
+ openWindowForOAuth(
+ `${authProvider.instanceUrl}/${
+ AuthProviderConfig[authProvider.type].apiPath
+ }`,
+ authProvider.applicationId,
+ "bb.oauth.signin",
+ authProvider.type
+ );
+};
+
const trySigninWithIdentityProvider = (identityProvider: IdentityProvider) => {
state.activeIdentityProvider = identityProvider;
openWindowForSSO(identityProvider);