oauth_github_handler.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "github.com/go-chi/chi"
  8. "github.com/google/go-github/github"
  9. "github.com/porter-dev/porter/internal/oauth"
  10. "golang.org/x/oauth2"
  11. )
  12. // There is a difference between the oauth flow when logging a user in, and when
  13. // linking a repository.
  14. // When logging a user in, the access token gets stored in the session, and no refresh
  15. // token is requested. We store the access token in the session because a user can be
  16. // logged in multiple times with a single access token.
  17. // NOTE: this user flow will likely be augmented with Dex, or entirely replaced with Dex.
  18. // However, when linking a repository, the access token and refresh token are requested when
  19. // the flow has started. A project also gets linked to the session. After callback, a new
  20. // github config gets stored for the project, and the user will then get redirected to
  21. // a URL that allows them to select their repositories they'd like to link. We require a refresh
  22. // token because we need permanent access to the linked repository.
  23. func (app *App) HandleOAuthStartUser(w http.ResponseWriter, r *http.Request) {
  24. state := oauth.CreateRandomState()
  25. err := app.populateOAuthSession(w, r, state, false)
  26. if err != nil {
  27. app.handleErrorDataRead(err, w)
  28. return
  29. }
  30. // specify access type offline to get a refresh token
  31. url := app.GithubConfig.AuthCodeURL(state, oauth2.AccessTypeOnline)
  32. http.Redirect(w, r, url, 302)
  33. }
  34. func (app *App) HandleOAuthStartProject(w http.ResponseWriter, r *http.Request) {
  35. state := oauth.CreateRandomState()
  36. err := app.populateOAuthSession(w, r, state, true)
  37. if err != nil {
  38. app.handleErrorDataRead(err, w)
  39. return
  40. }
  41. // specify access type offline to get a refresh token
  42. url := app.GithubConfig.AuthCodeURL(state, oauth2.AccessTypeOffline)
  43. http.Redirect(w, r, url, 302)
  44. }
  45. func (app *App) HandleOauthCallback(w http.ResponseWriter, r *http.Request) {
  46. session, err := app.store.Get(r, app.cookieName)
  47. if err != nil {
  48. app.handleErrorDataRead(err, w)
  49. return
  50. }
  51. if _, ok := session.Values["state"]; !ok {
  52. // TODO -- SEND A CUSTOM ERROR, PROBABLY MEANS COOKIES ARE NOT ENABLED
  53. // FOR NOW JUST SEND DATA READ ERROR
  54. app.handleErrorDataRead(err, w)
  55. return
  56. }
  57. if r.URL.Query().Get("state") != session.Values["state"] {
  58. // TODO -- SEND A CUSTOM ERROR, THIS MEANS THAT IDP CANNOT BE TRUSTED
  59. app.handleErrorDataRead(err, w)
  60. return
  61. }
  62. token, err := app.GithubConfig.Exchange(oauth2.NoContext, r.URL.Query().Get("code"))
  63. if err != nil {
  64. // TODO -- SEND A CUSTOM ERROR
  65. app.handleErrorDataRead(err, w)
  66. return
  67. }
  68. if !token.Valid() {
  69. // TODO -- SEND A CUSTOM ERROR
  70. app.handleErrorDataRead(err, w)
  71. return
  72. }
  73. app.updateProjectFromToken(token)
  74. // client := github.NewClient(app.GithubConfig.Client(oauth2.NoContext, token))
  75. // client.
  76. // TODO -- determine if the user already exists as a github user with that email
  77. // If the user does not exist, create the user in the database
  78. // If the user does exist, save the username, kind, and access_token in the session
  79. // user, _, err := client.Users.Get(context.Background(), "")
  80. // if err != nil {
  81. // fmt.Println(w, "error getting name")
  82. // return
  83. // }
  84. // session.Values["githubUserName"] = user.Name
  85. // session.Values["githubAccessToken"] = token
  86. // session.Save(r, w)
  87. http.Redirect(w, r, "/", 302)
  88. }
  89. func (app *App) populateOAuthSession(w http.ResponseWriter, r *http.Request, state string, isProject bool) error {
  90. session, err := app.store.Get(r, app.cookieName)
  91. if err != nil {
  92. return err
  93. }
  94. // need state parameter to validate when redirected
  95. session.Values["state"] = state
  96. if isProject {
  97. // read the project id and add it to the session
  98. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  99. if err != nil || projID == 0 {
  100. return fmt.Errorf("could not read project id")
  101. }
  102. session.Values["project_id"] = projID
  103. }
  104. if err := session.Save(r, w); err != nil {
  105. app.logger.Warn().Err(err)
  106. }
  107. return nil
  108. }
  109. func (app *App) upsertUserFromToken() error {
  110. return fmt.Errorf("UNIMPLEMENTED")
  111. }
  112. // updates a project's repository clients with the token information.
  113. func (app *App) updateProjectFromToken(tok *oauth2.Token) error {
  114. // get the list of repositories that this token has access to
  115. client := github.NewClient(app.GithubConfig.Client(oauth2.NoContext, tok))
  116. repos, _, err := client.Repositories.List(context.Background(), "", nil)
  117. if err != nil {
  118. return err
  119. }
  120. for _, repo := range repos {
  121. fmt.Println(repo.GetName())
  122. }
  123. return fmt.Errorf("UNIMPLEMENTED")
  124. }