Skip to content

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

fix: fixed route config error when registering a new vcs and introduced new oauth event type at the frontend (#389)

Luke Zhouabdaa3f1a5c8
diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index ee030dcefe..2b43d835dd 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -33,6 +33,7 @@ import BBModalStack from "./bbkit/BBModalStack.vue";
import { NConfigProvider } from "naive-ui";
import { themeOverrides, dateLang, generalLang } from "../naive-ui.config";
+import { t } from "./plugins/i18n";
// Show at most 3 notifications to prevent excessive notification when shit hits the fan.
const MAX_NOTIFICATION_DISPLAY_COUNT = 3;
@@ -133,6 +134,18 @@ export default defineComponent({
return true;
});
+ // event listener for "bb.oauth.event.unknown"
+ // this event would be posted when an unknown state is returned by OAuth provider.
+ // Add it here so the notification is displayed on the main window. The OAuth callback window is short lived
+ // and would close before the notification has a chance to be displayed.
+ window.addEventListener("bb.oauth.unknown", (event) => {
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("oauth.unknown-event"),
+ });
+ });
+
return {
state,
dateLang,
diff --git a/frontend/src/components/RepositoryVCSProviderPanel.vue b/frontend/src/components/RepositoryVCSProviderPanel.vue
index 2967bf3eee..4e77e61126 100644
--- a/frontend/src/components/RepositoryVCSProviderPanel.vue
+++ b/frontend/src/components/RepositoryVCSProviderPanel.vue
@@ -35,12 +35,18 @@
<script lang="ts">
import { useStore } from "vuex";
-import { reactive, computed, onUnmounted, PropType, watchEffect } from "vue";
+import {
+ reactive,
+ computed,
+ PropType,
+ watchEffect,
+ onUnmounted,
+ onMounted,
+} from "vue";
import isEmpty from "lodash-es/isEmpty";
import {
OAuthConfig,
OAuthToken,
- OAuthWindowEvent,
OAuthWindowEventPayload,
openWindowForOAuth,
ProjectRepositoryConfig,
@@ -74,6 +80,17 @@ export default {
watchEffect(prepareVCSList);
+ onMounted(() => {
+ window.addEventListener(
+ "bb.oauth.link-vcs-repository",
+ eventListener,
+ false
+ );
+ });
+ onUnmounted(() => {
+ window.removeEventListener("bb.oauth.link-vcs-repository", eventListener);
+ });
+
const vcsList = computed(() => {
return store.getters["vcs/vcsList"]();
});
@@ -107,24 +124,17 @@ export default {
}
};
- onUnmounted(() => {
- window.removeEventListener(OAuthWindowEvent, eventListener);
- });
-
const isCurrentUserOwner = computed(() => {
return isOwner(currentUser.value.role);
});
const selectVCS = (vcs: VCS) => {
state.selectedVCS = vcs;
- const newWindow = openWindowForOAuth(
+ openWindowForOAuth(
`${vcs.instanceUrl}/oauth/authorize`,
vcs.applicationId,
- "register"
+ "bb.oauth.link-vcs-repository"
);
- if (newWindow) {
- window.addEventListener(OAuthWindowEvent, eventListener, false);
- }
};
return {
diff --git a/frontend/src/components/VCSSetupWizard.vue b/frontend/src/components/VCSSetupWizard.vue
index baaedcf60a..8fb2468a3e 100644
--- a/frontend/src/components/VCSSetupWizard.vue
+++ b/frontend/src/components/VCSSetupWizard.vue
@@ -1,5 +1,9 @@
<template>
- <BBAttention v-if="showAttention" :style="'WARN'" :description="attentionText" />
+ <BBAttention
+ v-if="showAttention"
+ :style="'WARN'"
+ :description="attentionText"
+ />
<BBStepTab
class="mt-4"
:step-item-list="stepList"
@@ -22,7 +26,7 @@
</template>
<script lang="ts">
-import { reactive, computed } from "vue";
+import { reactive, computed, onUnmounted, onMounted } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import isEmpty from "lodash-es/isEmpty";
@@ -37,7 +41,6 @@ import {
VCS,
openWindowForOAuth,
OAuthWindowEventPayload,
- OAuthWindowEvent,
OAuthConfig,
redirectUrl,
OAuthToken,
@@ -84,6 +87,14 @@ export default {
currentStep: 0,
});
+ onMounted(() => {
+ window.addEventListener("bb.oauth.register-vcs", eventListener, false);
+ });
+
+ onUnmounted(() => {
+ window.removeEventListener("bb.oauth.register-vcs", eventListener);
+ });
+
const eventListener = (event: Event) => {
const payload = (event as CustomEvent).detail as OAuthWindowEventPayload;
if (isEmpty(payload.error)) {
@@ -109,8 +120,6 @@ export default {
} else {
state.oAuthResultCallback!(undefined);
}
-
- window.removeEventListener(OAuthWindowEvent, eventListener);
};
const allowNext = computed((): boolean => {
@@ -151,7 +160,7 @@ export default {
const newWindow = openWindowForOAuth(
`${state.config.instanceUrl}/oauth/authorize`,
state.config.applicationId,
- "register"
+ "bb.oauth.register-vcs"
);
if (newWindow) {
state.oAuthResultCallback = (token: OAuthToken | undefined) => {
@@ -183,7 +192,6 @@ export default {
});
}
};
- window.addEventListener(OAuthWindowEvent, eventListener, false);
}
} else {
state.currentStep = newStep;
diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.yml
index 820e4a0017..bdd4bed307 100644
--- a/frontend/src/locales/en.yml
+++ b/frontend/src/locales/en.yml
@@ -1084,9 +1084,8 @@ sql-editor:
table-schema-placeholder: Select a table to see its schema
notify-empty-statement: Please input your SQL statements in the editor
notify-multiple-statements: >-
- Multiple SQL statements detected.
- SQL Editor only executes the first statement.
- You can select another statement and execute it individually.
+ Multiple SQL statements detected. SQL Editor only executes the first
+ statement. You can select another statement and execute it individually.
goto-alter-schema-hint: Please select a database connection from the top of the editor
can-not-execute-query: Can not execute query when loading data.
rows: row | rows
@@ -1105,3 +1104,5 @@ label:
select-key: Select key...
select-value: Select value...
select-values: Select values...
+oauth:
+ unknown-event: Unexpeted event type, OAuth failed.
diff --git a/frontend/src/locales/zh-CN.yml b/frontend/src/locales/zh-CN.yml
index a136df1a90..880649073c 100644
--- a/frontend/src/locales/zh-CN.yml
+++ b/frontend/src/locales/zh-CN.yml
@@ -971,3 +971,5 @@ label:
select-key: 选择 Key…
select-value: 选择值…
select-values: 选择值…
+oauth:
+ unknown-event: 未知的事件类型,认证失败
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 1a3fb7c5c2..5da91716fa 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -792,14 +792,20 @@ router.beforeEach((to, from, next) => {
store.dispatch("router/setBackPath", from.fullPath);
}
+ // OAuth callback route is a relay to receive the OAuth callback and dispatch the corresponding OAuth event. It's called in the following scenarios:
+ // - Login via OAuth
+ // - Setup VCS provider
+ // - Setup GitOps workflow in a project
+ if (to.name === "oauth-callback") {
+ next();
+ }
+
if (
to.name === SIGNIN_MODULE ||
to.name === SIGNUP_MODULE ||
to.name === ACTIVATE_MODULE ||
to.name === PASSWORD_RESET_MODULE ||
- to.name === PASSWORD_FORGOT_MODULE ||
- // this is for oauth login
- to.name === "oauth-callback"
+ to.name === PASSWORD_FORGOT_MODULE
) {
if (isLoggedIn) {
next({ name: HOME_MODULE, replace: true });
@@ -885,7 +891,6 @@ router.beforeEach((to, from, next) => {
to.name === "error.403" ||
to.name === "error.404" ||
to.name === "error.500" ||
- to.name === "oauth-callback" ||
to.name === "workspace.home" ||
to.name === "workspace.inbox" ||
to.name === "workspace.anomaly-center" ||
diff --git a/frontend/src/store/modules/principal.ts b/frontend/src/store/modules/principal.ts
index cddb078579..a2e44dc129 100644
--- a/frontend/src/store/modules/principal.ts
+++ b/frontend/src/store/modules/principal.ts
@@ -11,6 +11,7 @@ import {
EMPTY_ID,
PrincipalType,
RoleType,
+ AuthProviderType,
} from "../../types";
import { randomString } from "../../utils";
@@ -21,6 +22,7 @@ function convert(principal: ResourceObject): Principal {
createdTs: principal.attributes.createdTs as number,
updaterId: principal.attributes.updaterId as PrincipalId,
updatedTs: principal.attributes.updatedTs as number,
+ authProvider: principal.attributes.authProvider as AuthProviderType,
type: principal.attributes.type as PrincipalType,
name: principal.attributes.name as string,
email: principal.attributes.email as string,
diff --git a/frontend/src/types/oauth.ts b/frontend/src/types/oauth.ts
index a6b033de2e..8494fc727c 100644
--- a/frontend/src/types/oauth.ts
+++ b/frontend/src/types/oauth.ts
@@ -1,3 +1,5 @@
+import { t } from "../plugins/i18n";
+import { store } from "../store";
import { randomString } from "../utils";
export type OAuthConfig = {
@@ -15,7 +17,6 @@ export type OAuthToken = {
export const OAuthStateSessionKey = "oauthstate";
-export const OAuthWindowEvent = "oauthevent";
export type OAuthWindowEventPayload = {
error: string;
code: string;
@@ -25,7 +26,14 @@ export function redirectUrl(): string {
return `${window.location.origin}/oauth/callback`;
}
-type OAuthType = "login" | "register";
+// signin: users try to login via oauth
+// register-vcs: users try to bind a vcs to her workspace
+// link-vcs-repository: users try to bind a vcs repo to her project
+export type OAuthType =
+ | "bb.oauth.signin"
+ | "bb.oauth.register-vcs"
+ | "bb.oauth.link-vcs-repository"
+ | "bb.oauth.unknown";
export function openWindowForOAuth(
endpoint: string,
diff --git a/frontend/src/views/OAuthCallback.vue b/frontend/src/views/OAuthCallback.vue
index 4b41b49974..04a799b314 100644
--- a/frontend/src/views/OAuthCallback.vue
+++ b/frontend/src/views/OAuthCallback.vue
@@ -17,8 +17,8 @@ import { reactive } from "vue";
import { useRouter } from "vue-router";
import {
OAuthStateSessionKey,
- OAuthWindowEvent,
OAuthWindowEventPayload,
+ OAuthType,
} from "../types";
interface LocalState {
@@ -40,7 +40,10 @@ export default {
error: "",
code: "",
};
+
const expectedState = sessionStorage.getItem(OAuthStateSessionKey);
+ let eventType = undefined;
+
if (
!expectedState ||
expectedState != router.currentRoute.value.query.state
@@ -53,11 +56,28 @@ export default {
state.message =
"Successfully authorized. Redirecting back to the application...";
payload.code = router.currentRoute.value.query.code as string;
+
+ eventType = expectedState.slice(0, expectedState.lastIndexOf("-"));
+ }
+
+ switch (eventType as OAuthType) {
+ case "bb.oauth.signin":
+ case "bb.oauth.register-vcs":
+ case "bb.oauth.link-vcs-repository":
+ window.opener.dispatchEvent(
+ new CustomEvent(eventType as OAuthType, {
+ detail: payload,
+ })
+ );
+ break;
+ default:
+ window.opener.dispatchEvent(
+ new CustomEvent("bb.oauth.unknown", {
+ detail: payload,
+ })
+ );
}
- window.opener.dispatchEvent(
- new CustomEvent(OAuthWindowEvent, { detail: payload })
- );
window.close();
return {
diff --git a/frontend/src/views/SettingWorkspaceVCSDetail.vue b/frontend/src/views/SettingWorkspaceVCSDetail.vue
index 9ab48fb5d7..c7016a6230 100644
--- a/frontend/src/views/SettingWorkspaceVCSDetail.vue
+++ b/frontend/src/views/SettingWorkspaceVCSDetail.vue
@@ -137,7 +137,7 @@
</template>
<script lang="ts">
-import { reactive, computed, watchEffect } from "vue";
+import { reactive, computed, watchEffect, onMounted, onUnmounted } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import RepositoryTable from "../components/RepositoryTable.vue";
@@ -147,7 +147,6 @@ import {
VCS,
VCSPatch,
openWindowForOAuth,
- OAuthWindowEvent,
OAuthWindowEventPayload,
OAuthConfig,
redirectUrl,
@@ -184,6 +183,14 @@ export default {
secret: "",
});
+ onMounted(() => {
+ window.addEventListener("bb.oauth.register-vcs", eventListener, false);
+ });
+
+ onUnmounted(() => {
+ window.removeEventListener("bb.oauth.register-vcs", eventListener);
+ });
+
const eventListener = (event: Event) => {
const payload = (event as CustomEvent).detail as OAuthWindowEventPayload;
if (isEmpty(payload.error)) {
@@ -209,8 +216,6 @@ export default {
} else {
state.oAuthResultCallback!(undefined);
}
-
- window.removeEventListener(OAuthWindowEvent, eventListener);
};
const prepareRepositoryList = () => {
@@ -246,7 +251,7 @@ export default {
const newWindow = openWindowForOAuth(
`${vcs.value.instanceUrl}/oauth/authorize`,
vcs.value.applicationId,
- "register"
+ "bb.oauth.register-vcs"
);
if (newWindow) {
state.oAuthResultCallback = (token: OAuthToken | undefined) => {
@@ -290,7 +295,6 @@ export default {
});
}
};
- window.addEventListener(OAuthWindowEvent, eventListener, false);
}
} else if (state.name != vcs.value.name) {
const vcsPatch: VCSPatch = {
diff --git a/frontend/src/views/auth/Signin.vue b/frontend/src/views/auth/Signin.vue
index 5ed7888750..e2ac51944a 100644
--- a/frontend/src/views/auth/Signin.vue
+++ b/frontend/src/views/auth/Signin.vue
@@ -18,13 +18,7 @@
@click.prevent="
() => {
state.activeAuthProvider = authProvider;
- const window = openWindowForOAuth(
- `${authProvider.instanceUrl}/${
- AuthProviderConfig[authProvider.type].apiPath
- }`,
- authProvider.applicationId,
- 'login'
- );
+ trySigninWithOAuth();
}
"
>
@@ -142,7 +136,7 @@
</template>
<script lang="ts">
-import { computed, onMounted, reactive } from "vue";
+import { computed, onMounted, onUnmounted, reactive } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import {
@@ -152,7 +146,6 @@ import {
LoginInfo,
OAuthConfig,
OAuthToken,
- OAuthWindowEvent,
OAuthWindowEventPayload,
openWindowForOAuth,
redirectUrl,
@@ -191,16 +184,13 @@ export default {
}
store.dispatch("auth/fetchProviderList");
- window.addEventListener(OAuthWindowEvent, eventListener, false);
+
+ window.addEventListener("bb.oauth.signin", eventListener, false);
});
- 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,
- },
- };
+ onUnmounted(() => {
+ window.removeEventListener("bb.oauth.signin", eventListener);
+ });
const allowSignin = computed(() => {
return isValidEmail(state.email) && state.password;
@@ -258,13 +248,38 @@ export default {
});
};
+ 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,
+ },
+ };
+
+ const trySigninWithOAuth = () => {
+ const authProvider = state.activeAuthProvider;
+
+ // the following 3 lines is for a lint error
+ if (authProvider.type == "BYTEBASE") {
+ return;
+ }
+
+ openWindowForOAuth(
+ `${authProvider.instanceUrl}/${
+ AuthProviderConfig[authProvider.type].apiPath
+ }`,
+ authProvider.applicationId,
+ "bb.oauth.signin"
+ );
+ };
+
return {
state,
allowSignin,
authProviderList,
AuthProviderConfig,
trySignin,
- openWindowForOAuth,
+ trySigninWithOAuth,
};
},
};