oauth_callback.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/models"
  11. "github.com/porter-dev/porter/internal/models/integrations"
  12. "golang.org/x/oauth2"
  13. )
  14. type GithubAppOAuthCallbackHandler struct {
  15. handlers.PorterHandlerReadWriter
  16. }
  17. func NewGithubAppOAuthCallbackHandler(
  18. config *config.Config,
  19. decoderValidator shared.RequestDecoderValidator,
  20. writer shared.ResultWriter,
  21. ) *GithubAppOAuthCallbackHandler {
  22. return &GithubAppOAuthCallbackHandler{
  23. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  24. }
  25. }
  26. func (c *GithubAppOAuthCallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. user, _ := r.Context().Value(types.UserScope).(*models.User)
  28. session, err := c.Config().Store.Get(r, c.Config().ServerConf.CookieName)
  29. if err != nil {
  30. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  31. return
  32. }
  33. token, err := c.Config().GithubAppConf.Exchange(oauth2.NoContext, r.URL.Query().Get("code"))
  34. if err != nil || !token.Valid() {
  35. if session.Values["query_params"] != "" {
  36. http.Redirect(w, r, fmt.Sprintf("/dashboard?%s", session.Values["query_params"]), 302)
  37. } else {
  38. http.Redirect(w, r, "/dashboard", 302)
  39. }
  40. return
  41. }
  42. oauthInt := &integrations.GithubAppOAuthIntegration{
  43. SharedOAuthModel: integrations.SharedOAuthModel{
  44. AccessToken: []byte(token.AccessToken),
  45. RefreshToken: []byte(token.RefreshToken),
  46. Expiry: token.Expiry,
  47. },
  48. UserID: user.ID,
  49. }
  50. oauthInt, err = c.Repo().GithubAppOAuthIntegration().CreateGithubAppOAuthIntegration(oauthInt)
  51. if err != nil {
  52. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  53. return
  54. }
  55. user.GithubAppIntegrationID = oauthInt.ID
  56. user, err = c.Repo().User().UpdateUser(user)
  57. if err != nil {
  58. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  59. return
  60. }
  61. if session.Values["query_params"] != "" {
  62. http.Redirect(w, r, fmt.Sprintf("/dashboard?%s", session.Values["query_params"]), 302)
  63. } else {
  64. http.Redirect(w, r, "/dashboard", 302)
  65. }
  66. }
  67. // func (app *App) HandleGithubAppOAuthCallback(w http.ResponseWriter, r *http.Request) {
  68. // session, err := app.Store.Get(r, app.ServerConf.CookieName)
  69. // if err != nil {
  70. // app.handleErrorDataRead(err, w)
  71. // return
  72. // }
  73. // token, err := app.GithubAppConf.Exchange(oauth2.NoContext, r.URL.Query().Get("code"))
  74. // if err != nil || !token.Valid() {
  75. // if session.Values["query_params"] != "" {
  76. // http.Redirect(w, r, fmt.Sprintf("/dashboard?%s", session.Values["query_params"]), 302)
  77. // } else {
  78. // http.Redirect(w, r, "/dashboard", 302)
  79. // }
  80. // return
  81. // }
  82. // fmt.Println("exchange happaned")
  83. // fmt.Println(token.AccessToken)
  84. // fmt.Println(token.RefreshToken)
  85. // userID, err := app.getUserIDFromRequest(r)
  86. // if err != nil {
  87. // app.handleErrorInternal(err, w)
  88. // return
  89. // }
  90. // user, err := app.Repo.User().ReadUser(userID)
  91. // if err != nil {
  92. // app.handleErrorInternal(err, w)
  93. // return
  94. // }
  95. // oauthInt := &integrations.GithubAppOAuthIntegration{
  96. // SharedOAuthModel: integrations.SharedOAuthModel{
  97. // AccessToken: []byte(token.AccessToken),
  98. // RefreshToken: []byte(token.RefreshToken),
  99. // Expiry: token.Expiry,
  100. // },
  101. // UserID: user.ID,
  102. // }
  103. // oauthInt, err = app.Repo.GithubAppOAuthIntegration().CreateGithubAppOAuthIntegration(oauthInt)
  104. // if err != nil {
  105. // app.handleErrorInternal(err, w)
  106. // return
  107. // }
  108. // user.GithubAppIntegrationID = oauthInt.ID
  109. // user, err = app.Repo.User().UpdateUser(user)
  110. // if err != nil {
  111. // app.handleErrorInternal(err, w)
  112. // return
  113. // }
  114. // if session.Values["query_params"] != "" {
  115. // http.Redirect(w, r, fmt.Sprintf("/dashboard?%s", session.Values["query_params"]), 302)
  116. // } else {
  117. // http.Redirect(w, r, "/dashboard", 302)
  118. // }
  119. // }