Mohammed Nafees 4 lat temu
rodzic
commit
e02f1af163

+ 1 - 1
api/server/handlers/handler.go

@@ -132,7 +132,7 @@ func (d *DefaultPorterHandler) PopulateOAuthSession(
 
 	if integrationID != 0 && len(integrationClient) > 0 {
 		session.Values["integration_id"] = integrationID
-		session.Values["integration_client"] = integrationClient
+		session.Values["integration_client"] = string(integrationClient)
 	}
 
 	if err := session.Save(r, w); err != nil {

+ 15 - 13
api/server/handlers/oauth_callback/gitlab.go

@@ -10,6 +10,7 @@ import (
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
+	"github.com/porter-dev/porter/api/server/shared/commonutils"
 	"github.com/porter-dev/porter/api/server/shared/config"
 	"github.com/porter-dev/porter/internal/models/integrations"
 	"gorm.io/gorm"
@@ -47,23 +48,11 @@ func (p *OAuthCallbackGitlabHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
 		return
 	}
 
-	token, err := p.Config().DOConf.Exchange(context.Background(), r.URL.Query().Get("code"))
-
-	if err != nil {
-		p.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
-		return
-	}
-
-	if !token.Valid() {
-		p.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("invalid token")))
-		return
-	}
-
 	userID, _ := session.Values["user_id"].(uint)
 	projID, _ := session.Values["project_id"].(uint)
 	integrationID := session.Values["integration_id"].(uint)
 
-	_, err = p.Repo().GitlabIntegration().ReadGitlabIntegration(projID, integrationID)
+	giIntegration, err := p.Repo().GitlabIntegration().ReadGitlabIntegration(projID, integrationID)
 
 	if err != nil {
 		if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -78,6 +67,19 @@ func (p *OAuthCallbackGitlabHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
 		return
 	}
 
+	token, err := commonutils.GetGitlabOAuthConf(p.Config(), giIntegration).
+		Exchange(context.Background(), r.URL.Query().Get("code"))
+
+	if err != nil {
+		p.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
+		return
+	}
+
+	if !token.Valid() {
+		p.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("invalid token")))
+		return
+	}
+
 	oauthInt := &integrations.GitlabAppOAuthIntegration{
 		SharedOAuthModel: integrations.SharedOAuthModel{
 			AccessToken:  []byte(token.AccessToken),

+ 13 - 6
api/server/handlers/project_oauth/gitlab.go

@@ -3,13 +3,13 @@ package project_oauth
 import (
 	"fmt"
 	"net/http"
+	"strconv"
 
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
 	"github.com/porter-dev/porter/api/server/shared/commonutils"
 	"github.com/porter-dev/porter/api/server/shared/config"
-	"github.com/porter-dev/porter/api/server/shared/requestutils"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/internal/oauth"
@@ -34,14 +34,21 @@ func NewProjectOAuthGitlabHandler(
 func (p *ProjectOAuthGitlabHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
 
-	integrationID, reqErr := requestutils.GetURLParamUint(r, "integration_id")
+	integrationIDStr := r.URL.Query().Get("integration_id")
 
-	if reqErr != nil {
-		p.HandleAPIError(w, r, reqErr)
+	if len(integrationIDStr) == 0 {
+		p.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("required query param integration_id")))
 		return
 	}
 
-	giIntegration, err := p.Repo().GitlabIntegration().ReadGitlabIntegration(proj.ID, integrationID)
+	integrationID, err := strconv.ParseUint(integrationIDStr, 10, 32)
+
+	if err != nil {
+		p.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
+		return
+	}
+
+	giIntegration, err := p.Repo().GitlabIntegration().ReadGitlabIntegration(proj.ID, uint(integrationID))
 
 	if err != nil {
 		if err == gorm.ErrRecordNotFound {
@@ -57,7 +64,7 @@ func (p *ProjectOAuthGitlabHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 
 	state := oauth.CreateRandomState()
 
-	if err := p.PopulateOAuthSession(w, r, state, true, true, types.OAuthGitlab, integrationID); err != nil {
+	if err := p.PopulateOAuthSession(w, r, state, true, true, types.OAuthGitlab, uint(integrationID)); err != nil {
 		p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		return
 	}

+ 1 - 0
internal/repository/gorm/migrate.go

@@ -57,6 +57,7 @@ func AutoMigrate(db *gorm.DB, debug bool) error {
 		&ints.AWSIntegration{},
 		&ints.AzureIntegration{},
 		&ints.GitlabIntegration{},
+		&ints.GitlabAppOAuthIntegration{},
 		&ints.TokenCache{},
 		&ints.ClusterTokenCache{},
 		&ints.RegTokenCache{},