helpers.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package gitinstallation
  2. import (
  3. "net/http"
  4. "github.com/bradleyfalzon/ghinstallation"
  5. "github.com/google/go-github/github"
  6. "github.com/porter-dev/porter/api/server/handlers"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/server/shared/requestutils"
  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. "github.com/porter-dev/porter/internal/oauth"
  13. "golang.org/x/oauth2"
  14. )
  15. // GetGithubAppOauthTokenFromRequest gets the GH oauth token from the request based on the currently
  16. // logged in user
  17. func GetGithubAppOauthTokenFromRequest(config *config.Config, r *http.Request) (*oauth2.Token, error) {
  18. // read the user from context
  19. user, _ := r.Context().Value(types.UserScope).(*models.User)
  20. getOAuthInt := config.Repo.GithubAppOAuthIntegration().ReadGithubAppOauthIntegration
  21. oauthInt, err := getOAuthInt(user.GithubAppIntegrationID)
  22. if err != nil {
  23. return nil, err
  24. }
  25. _, _, err = oauth.GetAccessToken(oauthInt.SharedOAuthModel,
  26. &config.GithubAppConf.Config,
  27. oauth.MakeUpdateGithubAppOauthIntegrationFunction(oauthInt, config.Repo),
  28. )
  29. if err != nil {
  30. // try again, in case the token got updated
  31. oauthInt2, err := getOAuthInt(user.GithubAppIntegrationID)
  32. if err != nil || oauthInt2.Expiry == oauthInt.Expiry {
  33. return nil, err
  34. }
  35. oauthInt.AccessToken = oauthInt2.AccessToken
  36. oauthInt.RefreshToken = oauthInt2.RefreshToken
  37. oauthInt.Expiry = oauthInt2.Expiry
  38. }
  39. return &oauth2.Token{
  40. AccessToken: string(oauthInt.AccessToken),
  41. RefreshToken: string(oauthInt.RefreshToken),
  42. Expiry: oauthInt.Expiry,
  43. TokenType: "Bearer",
  44. }, nil
  45. }
  46. // GetGithubAppClientFromRequest gets the github app installation id from the request and authenticates
  47. // using it and a private key file
  48. func GetGithubAppClientFromRequest(config *config.Config, r *http.Request) (*github.Client, error) {
  49. // get installation id from context
  50. ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
  51. itr, err := ghinstallation.NewKeyFromFile(
  52. http.DefaultTransport,
  53. config.GithubAppConf.AppID,
  54. ga.InstallationID,
  55. config.GithubAppConf.SecretPath,
  56. )
  57. if err != nil {
  58. return nil, err
  59. }
  60. return github.NewClient(&http.Client{Transport: itr}), nil
  61. }
  62. // GetOwnerAndNameParams gets the owner and name ref for the Github repo
  63. func GetOwnerAndNameParams(c handlers.PorterHandler, w http.ResponseWriter, r *http.Request) (string, string, bool) {
  64. owner, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoOwner)
  65. if reqErr != nil {
  66. c.HandleAPIError(w, r, reqErr)
  67. return "", "", false
  68. }
  69. name, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoName)
  70. if reqErr != nil {
  71. c.HandleAPIError(w, r, reqErr)
  72. return "", "", false
  73. }
  74. return owner, name, true
  75. }