gitlab.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/internal/models/integrations"
  14. "gorm.io/gorm"
  15. )
  16. type OAuthCallbackGitlabHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. func NewOAuthCallbackGitlabHandler(
  20. config *config.Config,
  21. decoderValidator shared.RequestDecoderValidator,
  22. writer shared.ResultWriter,
  23. ) *OAuthCallbackGitlabHandler {
  24. return &OAuthCallbackGitlabHandler{
  25. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  26. }
  27. }
  28. func (p *OAuthCallbackGitlabHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  29. session, err := p.Config().Store.Get(r, p.Config().ServerConf.CookieName)
  30. if err != nil {
  31. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  32. return
  33. }
  34. if _, ok := session.Values["state"]; !ok {
  35. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  36. return
  37. }
  38. if r.URL.Query().Get("state") != session.Values["state"] {
  39. p.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  40. return
  41. }
  42. userID, _ := session.Values["user_id"].(uint)
  43. projID, _ := session.Values["project_id"].(uint)
  44. integrationID := session.Values["integration_id"].(uint)
  45. giIntegration, err := p.Repo().GitlabIntegration().ReadGitlabIntegration(projID, integrationID)
  46. if err != nil {
  47. if errors.Is(err, gorm.ErrRecordNotFound) {
  48. p.HandleAPIError(w, r, apierrors.NewErrForbidden(
  49. fmt.Errorf("gitlab integration with id %d not found in project %d",
  50. integrationID, projID),
  51. ))
  52. return
  53. }
  54. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  55. return
  56. }
  57. token, err := commonutils.GetGitlabOAuthConf(p.Config(), giIntegration).
  58. Exchange(context.Background(), r.URL.Query().Get("code"))
  59. if err != nil {
  60. p.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  61. return
  62. }
  63. if !token.Valid() {
  64. p.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("invalid token")))
  65. return
  66. }
  67. oauthInt := &integrations.GitlabAppOAuthIntegration{
  68. SharedOAuthModel: integrations.SharedOAuthModel{
  69. AccessToken: []byte(token.AccessToken),
  70. RefreshToken: []byte(token.RefreshToken),
  71. },
  72. UserID: userID,
  73. ProjectID: projID,
  74. IntegrationID: integrationID,
  75. }
  76. // create the oauth integration first
  77. _, err = p.Repo().GitlabAppOAuthIntegration().CreateGitlabAppOAuthIntegration(oauthInt)
  78. if err != nil {
  79. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  80. return
  81. }
  82. if redirectStr, ok := session.Values["redirect_uri"].(string); ok && redirectStr != "" {
  83. // attempt to parse the redirect uri, if it fails just redirect to dashboard
  84. redirectURI, err := url.Parse(redirectStr)
  85. if err != nil {
  86. http.Redirect(w, r, "/dashboard", http.StatusFound)
  87. }
  88. http.Redirect(w, r, fmt.Sprintf("%s?%s", redirectURI.Path, redirectURI.RawQuery), http.StatusFound)
  89. } else {
  90. http.Redirect(w, r, "/dashboard", http.StatusFound)
  91. }
  92. }