2
0

oauth_callback.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package gitinstallation
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  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/config"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/analytics"
  12. "github.com/porter-dev/porter/internal/models"
  13. "github.com/porter-dev/porter/internal/models/integrations"
  14. "golang.org/x/oauth2"
  15. )
  16. type GithubAppOAuthCallbackHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. func NewGithubAppOAuthCallbackHandler(
  20. config *config.Config,
  21. decoderValidator shared.RequestDecoderValidator,
  22. writer shared.ResultWriter,
  23. ) *GithubAppOAuthCallbackHandler {
  24. return &GithubAppOAuthCallbackHandler{
  25. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  26. }
  27. }
  28. func (c *GithubAppOAuthCallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  29. user, _ := r.Context().Value(types.UserScope).(*models.User)
  30. session, err := c.Config().Store.Get(r, c.Config().ServerConf.CookieName)
  31. if err != nil {
  32. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  33. return
  34. }
  35. token, err := c.Config().GithubAppConf.Exchange(oauth2.NoContext, r.URL.Query().Get("code"))
  36. if err != nil || !token.Valid() {
  37. if redirectStr, ok := session.Values["redirect_uri"].(string); ok && redirectStr != "" {
  38. // attempt to parse the redirect uri, if it fails just redirect to dashboard
  39. redirectURI, err := url.Parse(redirectStr)
  40. if err != nil {
  41. http.Redirect(w, r, "/dashboard", 302)
  42. }
  43. http.Redirect(w, r, fmt.Sprintf("%s?%s", redirectURI.Path, redirectURI.RawQuery), 302)
  44. } else {
  45. http.Redirect(w, r, "/dashboard", 302)
  46. }
  47. return
  48. }
  49. oauthInt := &integrations.GithubAppOAuthIntegration{
  50. SharedOAuthModel: integrations.SharedOAuthModel{
  51. AccessToken: []byte(token.AccessToken),
  52. RefreshToken: []byte(token.RefreshToken),
  53. Expiry: token.Expiry,
  54. },
  55. UserID: user.ID,
  56. }
  57. oauthInt, err = c.Repo().GithubAppOAuthIntegration().CreateGithubAppOAuthIntegration(oauthInt)
  58. if err != nil {
  59. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  60. return
  61. }
  62. user.GithubAppIntegrationID = oauthInt.ID
  63. user, err = c.Repo().User().UpdateUser(user)
  64. if err != nil {
  65. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  66. return
  67. }
  68. c.Config().AnalyticsClient.Track(analytics.GithubConnectionSuccessTrack(
  69. &analytics.GithubConnectionSuccessTrackOpts{
  70. UserScopedTrackOpts: analytics.GetUserScopedTrackOpts(user.ID),
  71. },
  72. ))
  73. if redirectStr, ok := session.Values["redirect_uri"].(string); ok && redirectStr != "" {
  74. // attempt to parse the redirect uri, if it fails just redirect to dashboard
  75. redirectURI, err := url.Parse(redirectStr)
  76. if err != nil {
  77. http.Redirect(w, r, "/dashboard", 302)
  78. }
  79. http.Redirect(w, r, fmt.Sprintf("%s?%s", redirectURI.Path, redirectURI.RawQuery), 302)
  80. } else {
  81. http.Redirect(w, r, "/dashboard", 302)
  82. }
  83. }