From cf147cdeafb4e299984e0552d423bad4d7660481 Mon Sep 17 00:00:00 2001 From: Andrea Bernardo Ciddio Date: Thu, 28 May 2015 16:47:20 +0100 Subject: [PATCH] GitHub users without a public email should be authenticated using their primary private email address --- pkg/social/social.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pkg/social/social.go b/pkg/social/social.go index 49812ddd87f..1a00934b937 100644 --- a/pkg/social/social.go +++ b/pkg/social/social.go @@ -186,6 +186,37 @@ func (s *SocialGithub) IsOrganizationMember(client *http.Client) bool { return false } +func (s *SocialGithub) FetchPrivateEmail(client *http.Client) (string, error) { + type Record struct { + Email string `json:"email"` + Primary bool `json:"primary"` + Verified bool `json:"verified"` + } + + emailsUrl := fmt.Sprintf("https://api.github.com/user/emails") + r, err := client.Get(emailsUrl) + if err != nil { + return "", err + } + + defer r.Body.Close() + + var records []Record + + if err = json.NewDecoder(r.Body).Decode(&records); err != nil { + return "", err + } + + var email = "" + for _, record := range records { + if record.Primary { + email = record.Email + } + } + + return email, nil +} + func (s *SocialGithub) FetchTeamMemberships(client *http.Client) ([]int, error) { type Record struct { Id int `json:"id"` @@ -274,6 +305,13 @@ func (s *SocialGithub) UserInfo(token *oauth2.Token) (*BasicUserInfo, error) { return nil, ErrMissingOrganizationMembership } + if userInfo.Email == "" { + userInfo.Email, err = s.FetchPrivateEmail(client) + if err != nil { + return nil, err + } + } + return userInfo, nil }