slack.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package oauth_callback
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "github.com/porter-dev/porter/internal/telemetry"
  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/integrations/slack"
  13. )
  14. type OAuthCallbackSlackHandler struct {
  15. handlers.PorterHandlerReadWriter
  16. }
  17. func NewOAuthCallbackSlackHandler(
  18. config *config.Config,
  19. decoderValidator shared.RequestDecoderValidator,
  20. writer shared.ResultWriter,
  21. ) *OAuthCallbackSlackHandler {
  22. return &OAuthCallbackSlackHandler{
  23. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  24. }
  25. }
  26. func (p *OAuthCallbackSlackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. ctx, span := telemetry.NewSpan(r.Context(), "serve-oauth-callback-slack")
  28. defer span.End()
  29. r = r.Clone(ctx)
  30. session, err := p.Config().Store.Get(r, p.Config().ServerConf.CookieName)
  31. if err != nil {
  32. err = telemetry.Error(ctx, span, err, "session could not be retrieved")
  33. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  34. return
  35. }
  36. if _, ok := session.Values["state"]; !ok {
  37. err = telemetry.Error(ctx, span, nil, "state not found in session")
  38. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  39. return
  40. }
  41. if r.URL.Query().Get("state") != session.Values["state"] {
  42. err = telemetry.Error(ctx, span, nil, "state does not match")
  43. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  44. return
  45. }
  46. token, err := p.Config().SlackConf.Exchange(context.TODO(), r.URL.Query().Get("code"))
  47. if err != nil {
  48. err = telemetry.Error(ctx, span, err, "exchange failed")
  49. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  50. return
  51. }
  52. slackInt, err := slack.TokenToSlackIntegration(token)
  53. if err != nil {
  54. err = telemetry.Error(ctx, span, err, "token to slack integration failed")
  55. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  56. return
  57. }
  58. userID, _ := session.Values["user_id"].(uint)
  59. projID, _ := session.Values["project_id"].(uint)
  60. slackInt.UserID = userID
  61. slackInt.ProjectID = projID
  62. if _, err = p.Repo().SlackIntegration().CreateSlackIntegration(slackInt); err != nil {
  63. err = telemetry.Error(ctx, span, err, "create slack integration failed")
  64. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  65. return
  66. }
  67. if redirectStr, ok := session.Values["redirect_uri"].(string); ok && redirectStr != "" {
  68. // attempt to parse the redirect uri, if it fails just redirect to dashboard
  69. redirectURI, err := url.Parse(redirectStr)
  70. if err != nil {
  71. http.Redirect(w, r, "/dashboard", 302)
  72. }
  73. http.Redirect(w, r, fmt.Sprintf("%s?%s", redirectURI.Path, redirectURI.RawQuery), 302)
  74. } else {
  75. http.Redirect(w, r, "/dashboard", 302)
  76. }
  77. }