oauth_callback.go 2.5 KB

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