mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
make generic oauth provider flexible enough to handle bitbucket's oauth implementation (#8248)
This commit is contained in:
parent
b4cfb225cf
commit
665cf55e6e
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@ -76,9 +77,11 @@ func (s *GenericOAuth) IsOrganizationMember(client *http.Client) bool {
|
|||||||
|
|
||||||
func (s *GenericOAuth) FetchPrivateEmail(client *http.Client) (string, error) {
|
func (s *GenericOAuth) FetchPrivateEmail(client *http.Client) (string, error) {
|
||||||
type Record struct {
|
type Record struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Primary bool `json:"primary"`
|
Primary bool `json:"primary"`
|
||||||
Verified bool `json:"verified"`
|
IsPrimary bool `json:"is_primary"`
|
||||||
|
Verified bool `json:"verified"`
|
||||||
|
IsConfirmed bool `json:"is_confirmed"`
|
||||||
}
|
}
|
||||||
|
|
||||||
emailsUrl := fmt.Sprintf(s.apiUrl + "/emails")
|
emailsUrl := fmt.Sprintf(s.apiUrl + "/emails")
|
||||||
@ -91,14 +94,30 @@ func (s *GenericOAuth) FetchPrivateEmail(client *http.Client) (string, error) {
|
|||||||
|
|
||||||
var records []Record
|
var records []Record
|
||||||
|
|
||||||
if err = json.NewDecoder(r.Body).Decode(&records); err != nil {
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(body, records)
|
||||||
|
if err != nil {
|
||||||
|
var data struct {
|
||||||
|
Values []Record `json:"values"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(body, &data)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
records = data.Values
|
||||||
|
}
|
||||||
|
|
||||||
var email = ""
|
var email = ""
|
||||||
for _, record := range records {
|
for _, record := range records {
|
||||||
if record.Primary {
|
if record.Primary || record.IsPrimary {
|
||||||
email = record.Email
|
email = record.Email
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,11 +180,12 @@ func (s *GenericOAuth) FetchOrganizations(client *http.Client) ([]string, error)
|
|||||||
|
|
||||||
func (s *GenericOAuth) UserInfo(client *http.Client) (*BasicUserInfo, error) {
|
func (s *GenericOAuth) UserInfo(client *http.Client) (*BasicUserInfo, error) {
|
||||||
var data struct {
|
var data struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Login string `json:"login"`
|
DisplayName string `json:"display_name"`
|
||||||
Username string `json:"username"`
|
Login string `json:"login"`
|
||||||
Email string `json:"email"`
|
Username string `json:"username"`
|
||||||
Attributes map[string][]string `json:"attributes"`
|
Email string `json:"email"`
|
||||||
|
Attributes map[string][]string `json:"attributes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
@ -197,6 +217,10 @@ func (s *GenericOAuth) UserInfo(client *http.Client) (*BasicUserInfo, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if userInfo.Name == "" && data.DisplayName != "" {
|
||||||
|
userInfo.Name = data.DisplayName
|
||||||
|
}
|
||||||
|
|
||||||
if userInfo.Login == "" && data.Username != "" {
|
if userInfo.Login == "" && data.Username != "" {
|
||||||
userInfo.Login = data.Username
|
userInfo.Login = data.Username
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user