Skip to content

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

fix: fix redirectUri of oauth (#1236)

h3n4l368e93656f33
diff --git a/server/auth.go b/server/auth.go
index af88f390b5..f888acf7ea 100644
--- a/server/auth.go
+++ b/server/auth.go
@@ -87,6 +87,16 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("vcs do not exist, name: %v, ID: %v", gitlabLogin.Name, gitlabLogin.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{Logger: s.l}).ExchangeOAuthToken(
ctx,
@@ -95,7 +105,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
ClientID: vcsFound.ApplicationID,
ClientSecret: vcsFound.Secret,
Code: gitlabLogin.Code,
- RedirectURL: fmt.Sprintf("%s:%d/oauth/callback", s.profile.FrontendHost, s.profile.FrontendPort),
+ RedirectURL: redirectURL,
},
)
if err != nil {
diff --git a/server/oauth.go b/server/oauth.go
index 76fbb242a1..2c3844c53d 100644
--- a/server/oauth.go
+++ b/server/oauth.go
@@ -57,7 +57,16 @@ func (s *Server) registerOAuthRoutes(g *echo.Group) {
}
}
- oauthExchange.RedirectURL = fmt.Sprintf("%s:%d/oauth/callback", s.profile.FrontendHost, s.profile.FrontendPort)
+ // 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.
+ if s.profile.FrontendPort == 80 {
+ oauthExchange.RedirectURL = fmt.Sprintf("%s/oauth/callback", s.profile.FrontendHost)
+ } else {
+ oauthExchange.RedirectURL = fmt.Sprintf("%s:%d/oauth/callback", s.profile.FrontendHost, s.profile.FrontendPort)
+ }
+
oauthToken, err := vcsPlugin.Get(vcsType, vcsPlugin.ProviderConfig{Logger: s.l}).
ExchangeOAuthToken(
c.Request().Context(),