gitlab.go 3.0 KB

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