Skip to content

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

Still stores the access_token, expires_in, refresh_token for the connected VCS

天舟f81f42cfd9ca
diff --git a/backend/api/vcs.go b/backend/api/vcs.go
index f4a2006798..43d22f9353 100644
--- a/backend/api/vcs.go
+++ b/backend/api/vcs.go
@@ -37,6 +37,10 @@ type VCS struct {
ApiURL string `jsonapi:"attr,apiURL"`
ApplicationId string `jsonapi:"attr,applicationId"`
Secret string `jsonapi:"attr,secret"`
+ // These will be exclusively used on the server side (e.g. clean up orphaned webhooks) and we don't return it to the client.
+ AccessToken string
+ ExpiresTs int64
+ RefreshToken string
}
type VCSCreate struct {
@@ -52,6 +56,9 @@ type VCSCreate struct {
ApiURL string
ApplicationId string `jsonapi:"attr,applicationId"`
Secret string `jsonapi:"attr,secret"`
+ AccessToken string `jsonapi:"attr,accessToken"`
+ ExpiresTs int64 `jsonapi:"attr,expiresTs"`
+ RefreshToken string `jsonapi:"attr,refreshToken"`
}
type VCSFind struct {
@@ -77,6 +84,9 @@ type VCSPatch struct {
Name *string `jsonapi:"attr,name"`
ApplicationId *string `jsonapi:"attr,applicationId"`
Secret *string `jsonapi:"attr,secret"`
+ AccessToken *string `jsonapi:"attr,accessToken"`
+ ExpiresTs *int64 `jsonapi:"attr,expiresTs"`
+ RefreshToken *string `jsonapi:"attr,refreshToken"`
}
type VCSDelete struct {
diff --git a/backend/store/migration/0001_init_schema.up.sql b/backend/store/migration/0001_init_schema.up.sql
index 7b9cf46775..8f3c5e07b6 100644
--- a/backend/store/migration/0001_init_schema.up.sql
+++ b/backend/store/migration/0001_init_schema.up.sql
@@ -656,7 +656,10 @@ CREATE TABLE vcs (
OR api_url LIKE 'https:/%'
),
application_id TEXT NOT NULL,
- secret TEXT NOT NULL
+ secret TEXT NOT NULL,
+ access_token TEXT NOT NULL,
+ expires_ts BIGINT NOT NULL,
+ refresh_token TEXT NOT NULL
);
INSERT INTO
diff --git a/backend/store/seed/0160_vcs.sql b/backend/store/seed/0160_vcs.sql
index 28c205ae09..a88e13def9 100644
--- a/backend/store/seed/0160_vcs.sql
+++ b/backend/store/seed/0160_vcs.sql
@@ -8,7 +8,10 @@ INSERT INTO
instance_url,
api_url,
application_id,
- secret
+ secret,
+ access_token,
+ expires_ts,
+ refresh_token
)
VALUES
(
@@ -20,7 +23,10 @@ VALUES
'http://gitlab.bytebase.com',
'http://gitlab.bytebase.com/api/v4',
'0d1f706e68e3bf22be6712752726a3eec2a9684ec098a9695015f484165ed922',
- '77deb508304dd73fa9b6868d319e1832889c6fc8ea087f400e6efe35d7c6dd87'
+ '77deb508304dd73fa9b6868d319e1832889c6fc8ea087f400e6efe35d7c6dd87',
+ 'a6e1d552205a64c41afd377dedff745fd38a41733b2c780a506ad593ea42d27b',
+ 0,
+ 'e4b22027c3ef1847a898e2f8ef1e28056d286bc23bdd11a235711e434e447ab8'
);
INSERT INTO
@@ -33,7 +39,10 @@ INSERT INTO
instance_url,
api_url,
application_id,
- secret
+ secret,
+ access_token,
+ expires_ts,
+ refresh_token
)
VALUES
(
@@ -45,5 +54,8 @@ VALUES
'http://gitlab.bytebase.com',
'http://gitlab.bytebase.com/api/v4',
'0d1f706e68e3bf22be6712752726a3eec2a9684ec098a9695015f484165ed922',
- '77deb508304dd73fa9b6868d319e1832889c6fc8ea087f400e6efe35d7c6dd87'
+ '77deb508304dd73fa9b6868d319e1832889c6fc8ea087f400e6efe35d7c6dd87',
+ 'a6e1d552205a64c41afd377dedff745fd38a41733b2c780a506ad593ea42d27b',
+ 0,
+ 'e4b22027c3ef1847a898e2f8ef1e28056d286bc23bdd11a235711e434e447ab8'
);
\ No newline at end of file
diff --git a/backend/store/vcs.go b/backend/store/vcs.go
index ac39228de7..4733735330 100644
--- a/backend/store/vcs.go
+++ b/backend/store/vcs.go
@@ -136,10 +136,13 @@ func createVCS(ctx context.Context, tx *Tx, create *api.VCSCreate) (*api.VCS, er
instance_url,
api_url,
application_id,
- secret
+ secret,
+ access_token,
+ expires_ts,
+ refresh_token
)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, name, `+"`type`, instance_url, api_url, application_id, secret"+`
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, name, `+"`type`, instance_url, api_url, application_id, secret, access_token, expires_ts, refresh_token"+`
`,
create.CreatorId,
create.CreatorId,
@@ -149,6 +152,9 @@ func createVCS(ctx context.Context, tx *Tx, create *api.VCSCreate) (*api.VCS, er
create.ApiURL,
create.ApplicationId,
create.Secret,
+ create.AccessToken,
+ create.ExpiresTs,
+ create.RefreshToken,
)
if err != nil {
@@ -170,6 +176,9 @@ func createVCS(ctx context.Context, tx *Tx, create *api.VCSCreate) (*api.VCS, er
&vcs.ApiURL,
&vcs.ApplicationId,
&vcs.Secret,
+ &vcs.AccessToken,
+ &vcs.ExpiresTs,
+ &vcs.RefreshToken,
); err != nil {
return nil, FormatError(err)
}
@@ -196,7 +205,10 @@ func findVCSList(ctx context.Context, tx *Tx, find *api.VCSFind) (_ []*api.VCS,
instance_url,
api_url,
application_id,
- secret
+ secret,
+ access_token,
+ expires_ts,
+ refresh_token
FROM vcs
WHERE `+strings.Join(where, " AND "),
args...,
@@ -222,6 +234,9 @@ func findVCSList(ctx context.Context, tx *Tx, find *api.VCSFind) (_ []*api.VCS,
&vcs.ApiURL,
&vcs.ApplicationId,
&vcs.Secret,
+ &vcs.AccessToken,
+ &vcs.ExpiresTs,
+ &vcs.RefreshToken,
); err != nil {
return nil, FormatError(err)
}
@@ -248,6 +263,15 @@ func patchVCS(ctx context.Context, tx *Tx, patch *api.VCSPatch) (*api.VCS, error
if v := patch.Secret; v != nil {
set, args = append(set, "secret = ?"), append(args, *v)
}
+ if v := patch.AccessToken; v != nil {
+ set, args = append(set, "access_token = ?"), append(args, *v)
+ }
+ if v := patch.ExpiresTs; v != nil {
+ set, args = append(set, "expires_ts = ?"), append(args, *v)
+ }
+ if v := patch.RefreshToken; v != nil {
+ set, args = append(set, "refresh_token = ?"), append(args, *v)
+ }
args = append(args, patch.ID)
// Execute update query with RETURNING.
@@ -255,7 +279,7 @@ func patchVCS(ctx context.Context, tx *Tx, patch *api.VCSPatch) (*api.VCS, error
UPDATE vcs
SET `+strings.Join(set, ", ")+`
WHERE id = ?
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, name, `+"`type`, instance_url, api_url, application_id, secret"+`
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, name, `+"`type`, instance_url, api_url, application_id, secret, access_token, expires_ts, refresh_token"+`
`,
args...,
)
@@ -278,6 +302,9 @@ func patchVCS(ctx context.Context, tx *Tx, patch *api.VCSPatch) (*api.VCS, error
&vcs.ApiURL,
&vcs.ApplicationId,
&vcs.Secret,
+ &vcs.AccessToken,
+ &vcs.ExpiresTs,
+ &vcs.RefreshToken,
); err != nil {
return nil, FormatError(err)
}
diff --git a/frontend/src/components/RepositoryVCSPanel.vue b/frontend/src/components/RepositoryVCSPanel.vue
index f61437d452..dbe0ba28cb 100644
--- a/frontend/src/components/RepositoryVCSPanel.vue
+++ b/frontend/src/components/RepositoryVCSPanel.vue
@@ -46,6 +46,7 @@ import {
import isEmpty from "lodash-es/isEmpty";
import {
OAuthConfig,
+ OAuthToken,
OAuthWindowEvent,
OAuthWindowEventPayload,
openWindowForOAuth,
@@ -100,9 +101,9 @@ export default {
oAuthConfig,
code: payload.code,
})
- .then((token: string) => {
+ .then((token: OAuthToken) => {
props.config.vcs = state.selectedVCS!;
- props.config.accessToken = token;
+ props.config.token = token;
emit("next");
});
} else {
diff --git a/frontend/src/components/VCSSetupWizard.vue b/frontend/src/components/VCSSetupWizard.vue
index 474a0579de..ab58fbbf6d 100644
--- a/frontend/src/components/VCSSetupWizard.vue
+++ b/frontend/src/components/VCSSetupWizard.vue
@@ -41,6 +41,7 @@ import {
OAuthWindowEvent,
OAuthConfig,
redirectURL,
+ OAuthToken,
} from "../types";
import { isURL } from "../utils";
@@ -57,7 +58,7 @@ const stepList: BBStepTabItem[] = [
interface LocalState {
config: VCSConfig;
currentStep: number;
- oAuthResultCallback?: (success: boolean) => void;
+ oAuthResultCallback?: (token: OAuthToken | undefined) => void;
}
export default {
@@ -81,6 +82,9 @@ export default {
instanceURL: "",
applicationId: "",
secret: "",
+ accessToken: "",
+ expiresTs: 0,
+ refreshToken: "",
},
currentStep: 0,
});
@@ -100,15 +104,15 @@ export default {
oAuthConfig,
code: payload.code,
})
- .then((token: string) => {
- state.oAuthResultCallback!(true);
+ .then((token: OAuthToken) => {
+ state.oAuthResultCallback!(token);
})
.catch((error) => {
- state.oAuthResultCallback!(false);
+ state.oAuthResultCallback!(undefined);
});
}
} else {
- state.oAuthResultCallback!(false);
+ state.oAuthResultCallback!(undefined);
}
window.removeEventListener(OAuthWindowEvent, eventListener);
@@ -152,9 +156,12 @@ export default {
state.config.applicationId
);
if (newWindow) {
- state.oAuthResultCallback = (success: boolean) => {
- if (success) {
+ state.oAuthResultCallback = (token: OAuthToken | undefined) => {
+ if (token) {
state.currentStep = newStep;
+ state.config.accessToken = token.accessToken;
+ state.config.expiresTs = token.expiresTs;
+ state.config.refreshToken = token.refreshToken;
allowChangeCallback();
store.dispatch("notification/pushNotification", {
module: "bytebase",
diff --git a/frontend/src/store/modules/gitlab.ts b/frontend/src/store/modules/gitlab.ts
index f845646563..dd2281300a 100644
--- a/frontend/src/store/modules/gitlab.ts
+++ b/frontend/src/store/modules/gitlab.ts
@@ -1,5 +1,10 @@
import axios from "axios";
-import { ExternalRepositoryInfo, VCS, OAuthConfig } from "../../types";
+import {
+ ExternalRepositoryInfo,
+ VCS,
+ OAuthConfig,
+ OAuthToken,
+} from "../../types";
const GITLAB_API_PATH = "api/v4";
const GITLAB_WEBHOOK_PATH = "hook/gitlab";
@@ -26,7 +31,7 @@ const actions = {
oAuthConfig: OAuthConfig;
code: string;
}
- ): Promise<string> {
+ ): Promise<OAuthToken> {
console.log(
"req",
`${oAuthConfig.endpoint}?client_id=${oAuthConfig.applicationId}&client_secret=${oAuthConfig.secret}&code=${code}&redirect_uri=${oAuthConfig.redirectURL}&grant_type=authorization_code`
@@ -36,7 +41,15 @@ const actions = {
`${oAuthConfig.endpoint}?client_id=${oAuthConfig.applicationId}&client_secret=${oAuthConfig.secret}&code=${code}&redirect_uri=${oAuthConfig.redirectURL}&grant_type=authorization_code`
)
).data;
- return data.access_token;
+
+ const oAuthToken: OAuthToken = {
+ accessToken: data.access_token,
+ refreshToken: data.refresh_token,
+ // For GitLab, as of 13.12, the default config won't expire the access token, thus this field is 0.
+ // see https://gitlab.com/gitlab-org/gitlab/-/issues/21745.
+ expiresTs: data.expires_in == 0 ? 0 : data.created_at + data.expires_in,
+ };
+ return oAuthToken;
},
async fetchProjectList(
diff --git a/frontend/src/types/oauth.ts b/frontend/src/types/oauth.ts
index 08bb9131db..021865f33d 100644
--- a/frontend/src/types/oauth.ts
+++ b/frontend/src/types/oauth.ts
@@ -7,6 +7,12 @@ export type OAuthConfig = {
redirectURL: string;
};
+export type OAuthToken = {
+ accessToken: string;
+ expiresTs: number;
+ refreshToken: string;
+};
+
export const OAuthStateSessionKey = "oauthstate";
export const OAuthWindowEvent = "oauthevent";
diff --git a/frontend/src/types/project.ts b/frontend/src/types/project.ts
index caff7187b4..7002b477d9 100644
--- a/frontend/src/types/project.ts
+++ b/frontend/src/types/project.ts
@@ -1,5 +1,6 @@
import { RowStatus } from "./common";
import { MemberId, PrincipalId, ProjectId } from "./id";
+import { OAuthToken } from "./oauth";
import { Principal } from "./principal";
import { ExternalRepositoryInfo, RepositoryConfig } from "./repository";
import { VCS } from "./vcs";
@@ -85,7 +86,7 @@ export type ProjectMemberPatch = {
export type ProjectRepositoryConfig = {
vcs: VCS;
code: string;
- accessToken: string;
+ token: OAuthToken;
repositoryInfo: ExternalRepositoryInfo;
repositoryConfig: RepositoryConfig;
};
diff --git a/frontend/src/types/vcs.ts b/frontend/src/types/vcs.ts
index b5866390ec..c4b45af500 100644
--- a/frontend/src/types/vcs.ts
+++ b/frontend/src/types/vcs.ts
@@ -1,4 +1,5 @@
import { PrincipalId, VCSId } from "./id";
+import { OAuthToken } from "./oauth";
import { Principal } from "./principal";
export type VCSType = "GITLAB_SELF_HOST";
@@ -9,6 +10,9 @@ export interface VCSConfig {
instanceURL: string;
applicationId: string;
secret: string;
+ accessToken: string;
+ expiresTs: number;
+ refreshToken: string;
}
export type VCS = {
@@ -39,6 +43,9 @@ export type VCSCreate = {
instanceURL: string;
applicationId: string;
secret: string;
+ accessToken: string;
+ expiresTs: number;
+ refreshToken: string;
};
export type VCSPatch = {
@@ -46,6 +53,9 @@ export type VCSPatch = {
name?: string;
applicationId?: string;
secret?: string;
+ accessToken?: string;
+ expiresTs?: number;
+ refreshToken?: string;
};
export type VCSTokenCreate = {
diff --git a/frontend/src/views/SettingWorkspaceVCSDetail.vue b/frontend/src/views/SettingWorkspaceVCSDetail.vue
index 49165b20cd..a222dbd78c 100644
--- a/frontend/src/views/SettingWorkspaceVCSDetail.vue
+++ b/frontend/src/views/SettingWorkspaceVCSDetail.vue
@@ -141,13 +141,14 @@ import {
OAuthWindowEventPayload,
OAuthConfig,
redirectURL,
+ OAuthToken,
} from "../types";
interface LocalState {
name: string;
applicationId: string;
secret: string;
- oAuthResultCallback?: (success: boolean) => void;
+ oAuthResultCallback?: (token: OAuthToken | undefined) => void;
}
export default {
@@ -188,15 +189,15 @@ export default {
oAuthConfig,
code: payload.code,
})
- .then((token: string) => {
- state.oAuthResultCallback!(true);
+ .then((token: OAuthToken) => {
+ state.oAuthResultCallback!(token);
})
.catch((error) => {
- state.oAuthResultCallback!(false);
+ state.oAuthResultCallback!(undefined);
});
}
} else {
- state.oAuthResultCallback!(false);
+ state.oAuthResultCallback!(undefined);
}
window.removeEventListener(OAuthWindowEvent, eventListener);
@@ -237,8 +238,8 @@ export default {
vcs.value.applicationId
);
if (newWindow) {
- state.oAuthResultCallback = (success: boolean) => {
- if (success) {
+ state.oAuthResultCallback = (token: OAuthToken | undefined) => {
+ if (token) {
const vcsPatch: VCSPatch = {};
if (state.name != vcs.value.name) {
vcsPatch.name = state.name;
@@ -249,6 +250,9 @@ export default {
if (!isEmpty(state.secret)) {
vcsPatch.secret = state.secret;
}
+ vcsPatch.accessToken = token.accessToken;
+ vcsPatch.expiresTs = token.expiresTs;
+ vcsPatch.refreshToken = token.refreshToken;
store
.dispatch("vcs/patchVCS", {
vcsId: vcs.value.id,