Browse Source

fix login w blank email issue

Alexander Belanger 5 years ago
parent
commit
e745c4cc05
3 changed files with 24 additions and 16 deletions
  1. 1 1
      server/api/api.go
  2. 1 1
      server/api/helpers_test.go
  3. 22 14
      server/api/oauth_github_handler.go

+ 1 - 1
server/api/api.go

@@ -121,7 +121,7 @@ func New(conf *AppConfig) (*App, error) {
 		app.GithubUserConf = oauth.NewGithubClient(&oauth.Config{
 			ClientID:     sc.GithubClientID,
 			ClientSecret: sc.GithubClientSecret,
-			Scopes:       []string{"read:user"},
+			Scopes:       []string{"read:user", "user:email"},
 			BaseURL:      sc.ServerURL,
 		})
 

+ 1 - 1
server/api/helpers_test.go

@@ -16,7 +16,7 @@ import (
 	"github.com/porter-dev/porter/server/api"
 	"github.com/porter-dev/porter/server/router"
 
-	sessionstore "github.com/porter-dev/porter/internal/auth"
+	"github.com/porter-dev/porter/internal/auth/sessionstore"
 )
 
 type tester struct {

+ 22 - 14
server/api/oauth_github_handler.go

@@ -126,24 +126,12 @@ func (app *App) HandleGithubOAuthCallback(w http.ResponseWriter, r *http.Request
 
 		// log the user in
 		app.Logger.Info().Msgf("New user created: %d", user.ID)
-		var redirect string
-
-		if valR := session.Values["redirect"]; valR != nil {
-			redirect = session.Values["redirect"].(string)
-		}
 
 		session.Values["authenticated"] = true
 		session.Values["user_id"] = user.ID
 		session.Values["email"] = user.Email
 		session.Values["redirect"] = ""
 		session.Save(r, w)
-
-		w.WriteHeader(http.StatusCreated)
-
-		if err := app.sendUser(w, user.ID, user.Email, redirect); err != nil {
-			app.handleErrorFormDecoding(err, ErrUserDecode, w)
-			return
-		}
 	}
 
 	if session.Values["query_params"] != "" {
@@ -196,9 +184,29 @@ func (app *App) upsertUserFromToken(tok *oauth2.Token) (*models.User, error) {
 
 	// if the user does not exist, create new user
 	if err != nil && err == gorm.ErrRecordNotFound {
+		emails, _, err := client.Users.ListEmails(context.Background(), &github.ListOptions{})
+
+		if err != nil {
+			return nil, err
+		}
+
+		primary := ""
+
+		// get the primary email
+		for _, email := range emails {
+			if email.GetPrimary() {
+				primary = email.GetEmail()
+				break
+			}
+		}
+
+		if primary == "" {
+			return nil, fmt.Errorf("github user must have an email")
+		}
+
 		user = &models.User{
-			Email:        *githubUser.Email,
-			GithubUserID: *githubUser.ID,
+			Email:        primary,
+			GithubUserID: githubUser.GetID(),
 		}
 
 		user, err = app.Repo.User.CreateUser(user)