Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(github): support sign-in via GitHub.com (#1996)
Joe Chenaa8782b2b74a
diff --git a/api/auth.go b/api/auth.goindex 2dff82f1e8..3c022d3b83 100644--- a/api/auth.go+++ b/api/auth.go@@ -14,6 +14,7 @@ type AuthProvider struct {}// GitlabLogin is the API message for logins via Gitlab.+// TODO(jc): Rename to VCSLogin, https://linear.app/bbteam/issue/BYT-993type GitlabLogin struct {// VCSID is the ID of the chosen VCS role provider.VCSID int `jsonapi:"attr,vcsId"`diff --git a/api/principal.go b/api/principal.goindex 64cf51d82e..fceb19ecc1 100644--- a/api/principal.go+++ b/api/principal.go@@ -25,6 +25,8 @@ const (PrincipalAuthProviderBytebase PrincipalAuthProvider = "BYTEBASE"// PrincipalAuthProviderGitlabSelfHost is the self-hosted GitLab authentication provider.PrincipalAuthProviderGitlabSelfHost PrincipalAuthProvider = "GITLAB_SELF_HOST"+ // PrincipalAuthProviderGitHubCom is the GitHub.com authentication provider.+ PrincipalAuthProviderGitHubCom PrincipalAuthProvider = "GITHUB_COM")// Principal is the API message for principals.diff --git a/frontend/src/types/auth.ts b/frontend/src/types/auth.tsindex 5b5503bc26..3be1e80547 100644--- a/frontend/src/types/auth.ts+++ b/frontend/src/types/auth.ts@@ -2,8 +2,8 @@import { VCSId } from "./id";-// For now, a single user's auth provider should either belong to GITLAB_SELF_HOST or BYTEBASE-export type AuthProviderType = "GITLAB_SELF_HOST" | "BYTEBASE";+// For now, a single user's auth provider should either belong to GITLAB_SELF_HOST, GITHUB_COM or BYTEBASE+export type AuthProviderType = "GITLAB_SELF_HOST" | "GITHUB_COM" | "BYTEBASE";export type LoginInfo = {authProvider: AuthProviderType;diff --git a/frontend/src/types/project.ts b/frontend/src/types/project.tsindex 38cd1f1ddd..876455a56b 100644--- a/frontend/src/types/project.ts+++ b/frontend/src/types/project.ts@@ -13,7 +13,10 @@ export type ProjectVisibility = "PUBLIC" | "PRIVATE";export type ProjectTenantMode = "DISABLED" | "TENANT";-export type ProjectRoleProvider = "GITLAB_SELF_HOST" | "BYTEBASE";+export type ProjectRoleProvider =+ | "GITLAB_SELF_HOST"+ | "GITHUB_COM"+ | "BYTEBASE";export type ProjectRoleProviderPayload = {vcsRole: string;diff --git a/frontend/src/views/auth/Signin.vue b/frontend/src/views/auth/Signin.vueindex f455b16df4..01aa60cb92 100644--- a/frontend/src/views/auth/Signin.vue+++ b/frontend/src/views/auth/Signin.vue@@ -237,7 +237,7 @@ export default defineComponent({};authStore.login({- authProvider: "GITLAB_SELF_HOST",+ authProvider: state.activeAuthProvider.type,payload: gitlabLoginInfo,}).then(() => {@@ -264,6 +264,11 @@ export default defineComponent({// see https://vitejs.cn/guide/assets.html#the-public-directory for static resource import during run timeiconPath: 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,+ },};const trySigninWithOAuth = () => {diff --git a/plugin/vcs/github/github.go b/plugin/vcs/github/github.goindex 21c5097433..298963b118 100644--- a/plugin/vcs/github/github.go+++ b/plugin/vcs/github/github.go@@ -224,6 +224,7 @@ func (p *Provider) fetchUserInfo(ctx context.Context, oauthCtx common.OauthContereturn &vcs.UserInfo{PublicEmail: user.Email,Name: user.Name,+ State: vcs.StateActive,}, err}diff --git a/plugin/vcs/github/github_test.go b/plugin/vcs/github/github_test.goindex 642c3f9794..1d11f1d56a 100644--- a/plugin/vcs/github/github_test.go+++ b/plugin/vcs/github/github_test.go@@ -88,6 +88,7 @@ func TestProvider_FetchUserInfo(t *testing.T) {want := &vcs.UserInfo{PublicEmail: "[email protected]",Name: "monalisa octocat",+ State: vcs.StateActive,}assert.Equal(t, want, got)}diff --git a/server/auth.go b/server/auth.goindex 6990ec3b61..941622e2b6 100644--- a/server/auth.go+++ b/server/auth.go@@ -157,6 +157,93 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {}}}+ case api.PrincipalAuthProviderGitHubCom:+ {+ login := &api.GitlabLogin{}+ if err := jsonapi.UnmarshalPayload(c.Request().Body, login); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, "Malformed login request").SetInternal(err)+ }+ vcsFound, err := s.store.GetVCSByID(ctx, login.VCSID)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch vcs, name: %v, ID: %v", login.Name, login.Name)).SetInternal(err)+ }+ if vcsFound == nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("vcs do not exist, name: %v, ID: %v", login.Name, login.Name)).SetInternal(err)+ }++ // We need to attach the RedirectURL in the get token process of OAuth, and the+ // RedirectURL needs to be consistent with the RedirectURL in the get code+ // process. The frontend get it through window.location.origin in the get code+ // process, so port 80 needs to be cropped when the backend splices the+ // RedirectURL.+ var redirectURL string+ if s.profile.FrontendPort == 80 {+ redirectURL = fmt.Sprintf("%s/oauth/callback", s.profile.FrontendHost)+ } else {+ redirectURL = fmt.Sprintf("%s:%d/oauth/callback", s.profile.FrontendHost, s.profile.FrontendPort)+ }+ // Exchange OAuth Token+ oauthToken, err := vcs.Get(vcsFound.Type, vcs.ProviderConfig{}).ExchangeOAuthToken(+ ctx,+ vcsFound.InstanceURL,+ &common.OAuthExchange{+ ClientID: vcsFound.ApplicationID,+ ClientSecret: vcsFound.Secret,+ Code: login.Code,+ RedirectURL: redirectURL,+ },+ )+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to exchange OAuth token").SetInternal(err)+ }++ githubUserInfo, err := vcs.Get(vcs.GitHubCom, vcs.ProviderConfig{}).TryLogin(ctx,+ common.OauthContext{+ ClientID: vcsFound.ApplicationID,+ ClientSecret: vcsFound.Secret,+ AccessToken: oauthToken.AccessToken,+ RefreshToken: "",+ Refresher: nil,+ },+ vcsFound.InstanceURL,+ )+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, "Fail to fetch user info from GitHub").SetInternal(err)+ }++ // We only allow active user to login via GitHub+ if githubUserInfo.State != vcs.StateActive {+ return echo.NewHTTPError(http.StatusUnauthorized, "Fail to login via GitHub, user is Archived")+ }++ user, err = s.store.GetPrincipalByEmail(ctx, githubUserInfo.PublicEmail)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to authenticate user").SetInternal(err)+ }++ // Create a new user if not exist+ if user == nil {+ if githubUserInfo.PublicEmail == "" {+ return echo.NewHTTPError(http.StatusNotFound, "Please configure your public email first, https://docs.github.com/en/account-and-profile")+ }+ // If the user logins via GitHub for the first time, we will generate a random+ // password. The random password is supposed to be not guessable. If user wants+ // to login via password, they need to set the new password from the profile+ // page.+ signUp := &api.SignUp{+ Email: githubUserInfo.PublicEmail,+ Password: common.RandomString(20),+ Name: githubUserInfo.Name,+ }+ var httpError *echo.HTTPError+ user, httpError = trySignUp(ctx, s, signUp, api.SystemBotID)+ if httpError != nil {+ return httpError+ }+ }+ }+ default:+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unsupported auth provider: %s", authProvider))}// test the status of this user