Skip to content

Commit

Permalink
make generic oauth provider flexible enough to handle bitbucket's oau…
Browse files Browse the repository at this point in the history
…th implementation (grafana#8248)
  • Loading branch information
DanCech authored and torkelo committed May 2, 2017
1 parent b4cfb22 commit 665cf55
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions pkg/social/generic_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"

"github.com/grafana/grafana/pkg/models"
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 665cf55

Please sign in to comment.