mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
GitHub users without a public email should be authenticated using their primary private email address
This commit is contained in:
parent
57fac6b9aa
commit
cf147cdeaf
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user