瀏覽代碼

make generic oauth provider flexible enough to handle bitbucket's oauth implementation (#8248)

Dan Cech 8 年之前
父節點
當前提交
665cf55e6e
共有 1 個文件被更改,包括 34 次插入10 次删除
  1. 34 10
      pkg/social/generic_oauth.go

+ 34 - 10
pkg/social/generic_oauth.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"io/ioutil"
 	"net/http"
 
 	"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) {
 	type Record struct {
-		Email    string `json:"email"`
-		Primary  bool   `json:"primary"`
-		Verified bool   `json:"verified"`
+		Email       string `json:"email"`
+		Primary     bool   `json:"primary"`
+		IsPrimary   bool   `json:"is_primary"`
+		Verified    bool   `json:"verified"`
+		IsConfirmed bool   `json:"is_confirmed"`
 	}
 
 	emailsUrl := fmt.Sprintf(s.apiUrl + "/emails")
@@ -91,14 +94,30 @@ func (s *GenericOAuth) FetchPrivateEmail(client *http.Client) (string, error) {
 
 	var records []Record
 
-	if err = json.NewDecoder(r.Body).Decode(&records); err != nil {
+	body, err := ioutil.ReadAll(r.Body)
+	if err != nil {
 		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 = ""
 	for _, record := range records {
-		if record.Primary {
+		if record.Primary || record.IsPrimary {
 			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) {
 	var data struct {
-		Name       string              `json:"name"`
-		Login      string              `json:"login"`
-		Username   string              `json:"username"`
-		Email      string              `json:"email"`
-		Attributes map[string][]string `json:"attributes"`
+		Name        string              `json:"name"`
+		DisplayName string              `json:"display_name"`
+		Login       string              `json:"login"`
+		Username    string              `json:"username"`
+		Email       string              `json:"email"`
+		Attributes  map[string][]string `json:"attributes"`
 	}
 
 	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 != "" {
 		userInfo.Login = data.Username
 	}