oauth_do_handler.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package api
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/porter-dev/porter/internal/oauth"
  6. "golang.org/x/oauth2"
  7. "github.com/porter-dev/porter/internal/models/integrations"
  8. )
  9. // HandleDOOAuthStartProject starts the oauth2 flow for a project digitalocean request.
  10. // In this handler, the project id gets written to the session (along with the oauth
  11. // state param), so that the correct project id can be identified in the callback.
  12. func (app *App) HandleDOOAuthStartProject(w http.ResponseWriter, r *http.Request) {
  13. state := oauth.CreateRandomState()
  14. err := app.populateOAuthSession(w, r, state, true)
  15. if err != nil {
  16. app.handleErrorDataRead(err, w)
  17. return
  18. }
  19. // specify access type offline to get a refresh token
  20. url := app.DOConf.AuthCodeURL(state, oauth2.AccessTypeOffline)
  21. http.Redirect(w, r, url, 302)
  22. }
  23. // HandleDOOAuthCallback verifies the callback request by checking that the
  24. // state parameter has not been modified, and validates the token.
  25. func (app *App) HandleDOOAuthCallback(w http.ResponseWriter, r *http.Request) {
  26. session, err := app.Store.Get(r, app.ServerConf.CookieName)
  27. if err != nil {
  28. app.handleErrorDataRead(err, w)
  29. return
  30. }
  31. if _, ok := session.Values["state"]; !ok {
  32. app.sendExternalError(
  33. err,
  34. http.StatusForbidden,
  35. HTTPError{
  36. Code: http.StatusForbidden,
  37. Errors: []string{
  38. "Could not read cookie: are cookies enabled?",
  39. },
  40. },
  41. w,
  42. )
  43. return
  44. }
  45. if r.URL.Query().Get("state") != session.Values["state"] {
  46. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  47. return
  48. }
  49. token, err := app.DOConf.Exchange(oauth2.NoContext, r.URL.Query().Get("code"))
  50. if err != nil {
  51. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  52. return
  53. }
  54. if !token.Valid() {
  55. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  56. return
  57. }
  58. userID, _ := session.Values["user_id"].(uint)
  59. projID, _ := session.Values["project_id"].(uint)
  60. oauthInt := &integrations.OAuthIntegration{
  61. Client: integrations.OAuthDigitalOcean,
  62. UserID: userID,
  63. ProjectID: projID,
  64. AccessToken: []byte(token.AccessToken),
  65. RefreshToken: []byte(token.RefreshToken),
  66. }
  67. // create the oauth integration first
  68. oauthInt, err = app.Repo.OAuthIntegration.CreateOAuthIntegration(oauthInt)
  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. }