gitlab.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package project_oauth
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "github.com/porter-dev/porter/internal/telemetry"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/commonutils"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/oauth"
  15. "golang.org/x/oauth2"
  16. "gorm.io/gorm"
  17. )
  18. type ProjectOAuthGitlabHandler struct {
  19. handlers.PorterHandlerReadWriter
  20. }
  21. func NewProjectOAuthGitlabHandler(
  22. config *config.Config,
  23. decoderValidator shared.RequestDecoderValidator,
  24. writer shared.ResultWriter,
  25. ) *ProjectOAuthGitlabHandler {
  26. return &ProjectOAuthGitlabHandler{
  27. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  28. }
  29. }
  30. func (p *ProjectOAuthGitlabHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. ctx, span := telemetry.NewSpan(r.Context(), "serve-project-gitlab")
  32. defer span.End()
  33. r = r.Clone(ctx)
  34. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  35. integrationIDStr := r.URL.Query().Get("integration_id")
  36. if len(integrationIDStr) == 0 {
  37. p.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("required query param integration_id")))
  38. return
  39. }
  40. integrationID, err := strconv.ParseUint(integrationIDStr, 10, 32)
  41. if err != nil {
  42. p.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  43. return
  44. }
  45. giIntegration, err := p.Repo().GitlabIntegration().ReadGitlabIntegration(proj.ID, uint(integrationID))
  46. if err != nil {
  47. if err == gorm.ErrRecordNotFound {
  48. p.HandleAPIError(w, r, apierrors.NewErrForbidden(
  49. fmt.Errorf("gitlab integration with id %d not found in project %d", integrationID, proj.ID),
  50. ))
  51. } else {
  52. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  53. }
  54. return
  55. }
  56. state := oauth.CreateRandomState()
  57. if err := p.PopulateOAuthSession(ctx, w, r, state, true, true, types.OAuthGitlab, uint(integrationID)); err != nil {
  58. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  59. return
  60. }
  61. gitlabConf := commonutils.GetGitlabOAuthConf(p.Config(), giIntegration)
  62. // specify access type offline to get a refresh token
  63. url := gitlabConf.AuthCodeURL(state, oauth2.AccessTypeOffline)
  64. http.Redirect(w, r, url, http.StatusFound)
  65. }