oauth_slack_handler.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "github.com/go-chi/chi"
  8. "github.com/porter-dev/porter/internal/integrations/slack"
  9. "github.com/porter-dev/porter/internal/models/integrations"
  10. "github.com/porter-dev/porter/internal/oauth"
  11. "golang.org/x/oauth2"
  12. )
  13. // HandleSlackOAuthStartProject starts the oauth2 flow for a project slack request.
  14. // In this handler, the project id gets written to the session (along with the oauth
  15. // state param), so that the correct project id can be identified in the callback.
  16. func (app *App) HandleSlackOAuthStartProject(w http.ResponseWriter, r *http.Request) {
  17. state := oauth.CreateRandomState()
  18. err := app.populateOAuthSession(w, r, state, true)
  19. if err != nil {
  20. app.handleErrorDataRead(err, w)
  21. return
  22. }
  23. // specify access type offline to get a refresh token
  24. url := app.SlackConf.AuthCodeURL(state, oauth2.AccessTypeOffline)
  25. http.Redirect(w, r, url, 302)
  26. }
  27. // HandleSlackOAuthCallback verifies the callback request by checking that the
  28. // state parameter has not been modified, and validates the token.
  29. func (app *App) HandleSlackOAuthCallback(w http.ResponseWriter, r *http.Request) {
  30. session, err := app.Store.Get(r, app.ServerConf.CookieName)
  31. if err != nil {
  32. app.handleErrorDataRead(err, w)
  33. return
  34. }
  35. if _, ok := session.Values["state"]; !ok {
  36. app.sendExternalError(
  37. err,
  38. http.StatusForbidden,
  39. HTTPError{
  40. Code: http.StatusForbidden,
  41. Errors: []string{
  42. "Could not read cookie: are cookies enabled?",
  43. },
  44. },
  45. w,
  46. )
  47. return
  48. }
  49. if r.URL.Query().Get("state") != session.Values["state"] {
  50. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  51. return
  52. }
  53. token, err := app.SlackConf.Exchange(oauth2.NoContext, r.URL.Query().Get("code"))
  54. if err != nil {
  55. fmt.Println("ERR IS", err)
  56. return
  57. }
  58. userID, _ := session.Values["user_id"].(uint)
  59. projID, _ := session.Values["project_id"].(uint)
  60. slackInt, err := slack.TokenToSlackIntegration(token)
  61. if err != nil {
  62. app.handleErrorDataRead(err, w)
  63. return
  64. }
  65. slackInt.UserID = userID
  66. slackInt.ProjectID = projID
  67. // save to repository
  68. slackInt, err = app.Repo.SlackIntegration().CreateSlackIntegration(slackInt)
  69. if err != nil {
  70. app.handleErrorDataWrite(err, w)
  71. return
  72. }
  73. if session.Values["query_params"] != "" {
  74. http.Redirect(w, r, fmt.Sprintf("/dashboard?%s", session.Values["query_params"]), 302)
  75. } else {
  76. http.Redirect(w, r, "/dashboard", 302)
  77. }
  78. }
  79. // HandleListSlackIntegrations lists all slack integrations belonging to a certain project
  80. // ID
  81. func (app *App) HandleListSlackIntegrations(w http.ResponseWriter, r *http.Request) {
  82. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  83. if err != nil || projID == 0 {
  84. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  85. return
  86. }
  87. slackInts, err := app.Repo.SlackIntegration().ListSlackIntegrationsByProjectID(uint(projID))
  88. if err != nil {
  89. app.handleErrorRead(err, ErrProjectDataRead, w)
  90. return
  91. }
  92. extSlackInts := make([]*integrations.SlackIntegrationExternal, 0)
  93. for _, slackInt := range slackInts {
  94. extSlackInts = append(extSlackInts, slackInt.Externalize())
  95. }
  96. w.WriteHeader(http.StatusOK)
  97. if err := json.NewEncoder(w).Encode(extSlackInts); err != nil {
  98. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  99. return
  100. }
  101. }
  102. // HandleSlackIntegrationExists does 200 if at least one slack integration exists and 404 otherwise
  103. func (app *App) HandleSlackIntegrationExists(w http.ResponseWriter, r *http.Request) {
  104. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  105. if err != nil || projID == 0 {
  106. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  107. return
  108. }
  109. slackInts, err := app.Repo.SlackIntegration().ListSlackIntegrationsByProjectID(uint(projID))
  110. if err != nil {
  111. app.handleErrorRead(err, ErrProjectDataRead, w)
  112. return
  113. }
  114. if len(slackInts) != 0 {
  115. w.WriteHeader(http.StatusOK)
  116. } else {
  117. w.WriteHeader(http.StatusNotFound)
  118. }
  119. }
  120. // HandleDeleteSlackIntegration deletes a slack integration for a project by ID
  121. func (app *App) HandleDeleteSlackIntegration(w http.ResponseWriter, r *http.Request) {
  122. // check that slack integration belongs to given project
  123. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  124. if err != nil || projID == 0 {
  125. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  126. return
  127. }
  128. integrationID, err := strconv.ParseUint(chi.URLParam(r, "slack_integration_id"), 0, 64)
  129. if err != nil || projID == 0 {
  130. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  131. return
  132. }
  133. slackInts, err := app.Repo.SlackIntegration().ListSlackIntegrationsByProjectID(uint(projID))
  134. if err != nil {
  135. app.handleErrorRead(err, ErrProjectDataRead, w)
  136. return
  137. }
  138. for _, slackInt := range slackInts {
  139. if slackInt.ID == uint(integrationID) {
  140. err = app.Repo.SlackIntegration().DeleteSlackIntegration(slackInt.ID)
  141. if err != nil {
  142. app.handleErrorInternal(err, w)
  143. return
  144. }
  145. w.WriteHeader(http.StatusOK)
  146. }
  147. }
  148. w.WriteHeader(http.StatusNotFound)
  149. }