slack.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package project_oauth
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/internal/telemetry"
  5. "golang.org/x/oauth2"
  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/internal/oauth"
  11. )
  12. type ProjectOAuthSlackHandler struct {
  13. handlers.PorterHandlerReadWriter
  14. }
  15. func NewProjectOAuthSlackHandler(
  16. config *config.Config,
  17. decoderValidator shared.RequestDecoderValidator,
  18. writer shared.ResultWriter,
  19. ) *ProjectOAuthSlackHandler {
  20. return &ProjectOAuthSlackHandler{
  21. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  22. }
  23. }
  24. func (p *ProjectOAuthSlackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  25. ctx, span := telemetry.NewSpan(r.Context(), "serve-project-oauth-slack")
  26. defer span.End()
  27. r = r.Clone(ctx)
  28. state := oauth.CreateRandomState()
  29. if err := p.PopulateOAuthSession(ctx, w, r, state, true, false, "", 0); err != nil {
  30. err = telemetry.Error(ctx, span, err, "population oauth session failed")
  31. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  32. return
  33. }
  34. // specify access type offline to get a refresh token
  35. url := p.Config().SlackConf.AuthCodeURL(state, oauth2.AccessTypeOffline)
  36. http.Redirect(w, r, url, 302)
  37. }