gitlab.go 2.2 KB

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