Merge pull request #2067 from bcandrea/private-email-in-github-oauth
Use private email address in GitHub OAUTH
This commit is contained in:
@@ -186,6 +186,37 @@ func (s *SocialGithub) IsOrganizationMember(client *http.Client) bool {
|
|||||||
return false
|
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) {
|
func (s *SocialGithub) FetchTeamMemberships(client *http.Client) ([]int, error) {
|
||||||
type Record struct {
|
type Record struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
@@ -274,6 +305,13 @@ func (s *SocialGithub) UserInfo(token *oauth2.Token) (*BasicUserInfo, error) {
|
|||||||
return nil, ErrMissingOrganizationMembership
|
return nil, ErrMissingOrganizationMembership
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if userInfo.Email == "" {
|
||||||
|
userInfo.Email, err = s.FetchPrivateEmail(client)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return userInfo, nil
|
return userInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user