gitlab.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package oauth_callback
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/apierrors"
  11. "github.com/porter-dev/porter/api/server/shared/commonutils"
  12. "github.com/porter-dev/porter/api/server/shared/config"
  13. "github.com/porter-dev/porter/api/types"
  14. "github.com/porter-dev/porter/internal/models/integrations"
  15. "gorm.io/gorm"
  16. )
  17. type OAuthCallbackGitlabHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. }
  20. func NewOAuthCallbackGitlabHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *OAuthCallbackGitlabHandler {
  25. return &OAuthCallbackGitlabHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. func (p *OAuthCallbackGitlabHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  30. session, err := p.Config().Store.Get(r, p.Config().ServerConf.CookieName)
  31. if err != nil {
  32. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  33. return
  34. }
  35. if _, ok := session.Values["state"]; !ok {
  36. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  37. return
  38. }
  39. if r.URL.Query().Get("state") != session.Values["state"] {
  40. p.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  41. return
  42. }
  43. userID, _ := session.Values["user_id"].(uint)
  44. projID, _ := session.Values["project_id"].(uint)
  45. integrationID := session.Values["integration_id"].(uint)
  46. giIntegration, err := p.Repo().GitlabIntegration().ReadGitlabIntegration(projID, integrationID)
  47. if err != nil {
  48. if errors.Is(err, gorm.ErrRecordNotFound) {
  49. p.HandleAPIError(w, r, apierrors.NewErrForbidden(
  50. fmt.Errorf("gitlab integration with id %d not found in project %d",
  51. integrationID, projID),
  52. ))
  53. return
  54. }
  55. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  56. return
  57. }
  58. token, err := commonutils.GetGitlabOAuthConf(p.Config(), giIntegration).
  59. Exchange(context.Background(), r.URL.Query().Get("code"))
  60. if err != nil {
  61. p.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  62. return
  63. }
  64. if !token.Valid() {
  65. p.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("invalid token")))
  66. return
  67. }
  68. oauthInt := &integrations.OAuthIntegration{
  69. SharedOAuthModel: integrations.SharedOAuthModel{
  70. AccessToken: []byte(token.AccessToken),
  71. RefreshToken: []byte(token.RefreshToken),
  72. Expiry: token.Expiry,
  73. },
  74. Client: types.OAuthGitlab,
  75. UserID: userID,
  76. ProjectID: projID,
  77. }
  78. oauthInt, err = p.Repo().OAuthIntegration().CreateOAuthIntegration(oauthInt)
  79. if err != nil {
  80. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  81. return
  82. }
  83. if oauthInt.ID == 0 {
  84. p.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error creating oauth integration for gitlab")))
  85. return
  86. }
  87. giOAuthInt := &integrations.GitlabAppOAuthIntegration{
  88. OAuthIntegrationID: oauthInt.ID,
  89. GitlabIntegrationID: integrationID,
  90. }
  91. // create the oauth integration first
  92. _, err = p.Repo().GitlabAppOAuthIntegration().CreateGitlabAppOAuthIntegration(giOAuthInt)
  93. if err != nil {
  94. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  95. return
  96. }
  97. if redirectStr, ok := session.Values["redirect_uri"].(string); ok && redirectStr != "" {
  98. // attempt to parse the redirect uri, if it fails just redirect to dashboard
  99. redirectURI, err := url.Parse(redirectStr)
  100. if err != nil {
  101. http.Redirect(w, r, "/dashboard", http.StatusFound)
  102. }
  103. http.Redirect(w, r, fmt.Sprintf("%s?%s", redirectURI.Path, redirectURI.RawQuery), http.StatusFound)
  104. } else {
  105. http.Redirect(w, r, "/dashboard", http.StatusFound)
  106. }
  107. }