2016-09-19 15:48:07 -05:00
|
|
|
package social
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SocialGrafanaNet struct {
|
|
|
|
*oauth2.Config
|
|
|
|
url string
|
|
|
|
allowedOrganizations []string
|
|
|
|
allowSignup bool
|
|
|
|
}
|
|
|
|
|
2016-10-11 01:51:44 -05:00
|
|
|
type OrgRecord struct {
|
|
|
|
Login string `json:"login"`
|
|
|
|
}
|
|
|
|
|
2016-09-19 15:48:07 -05:00
|
|
|
func (s *SocialGrafanaNet) Type() int {
|
|
|
|
return int(models.GRAFANANET)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SocialGrafanaNet) IsEmailAllowed(email string) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SocialGrafanaNet) IsSignupAllowed() bool {
|
|
|
|
return s.allowSignup
|
|
|
|
}
|
|
|
|
|
2016-10-11 01:51:44 -05:00
|
|
|
func (s *SocialGrafanaNet) IsOrganizationMember(organizations []OrgRecord) bool {
|
2016-09-19 15:48:07 -05:00
|
|
|
if len(s.allowedOrganizations) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, allowedOrganization := range s.allowedOrganizations {
|
|
|
|
for _, organization := range organizations {
|
2016-10-11 01:51:44 -05:00
|
|
|
if organization.Login == allowedOrganization {
|
2016-09-19 15:48:07 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-10-11 01:51:44 -05:00
|
|
|
func (s *SocialGrafanaNet) UserInfo(client *http.Client) (*BasicUserInfo, error) {
|
2016-09-19 15:48:07 -05:00
|
|
|
var data struct {
|
2016-12-13 21:15:35 -06:00
|
|
|
Name string `json:"name"`
|
|
|
|
Login string `json:"username"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Role string `json:"role"`
|
2016-10-11 01:51:44 -05:00
|
|
|
Orgs []OrgRecord `json:"orgs"`
|
2016-09-19 15:48:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
r, err := client.Get(s.url + "/api/oauth2/user")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userInfo := &BasicUserInfo{
|
2016-12-13 21:15:35 -06:00
|
|
|
Name: data.Name,
|
|
|
|
Login: data.Login,
|
|
|
|
Email: data.Email,
|
|
|
|
Role: data.Role,
|
2016-09-19 15:48:07 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 01:51:44 -05:00
|
|
|
if !s.IsOrganizationMember(data.Orgs) {
|
2016-09-19 15:48:07 -05:00
|
|
|
return nil, ErrMissingOrganizationMembership
|
|
|
|
}
|
|
|
|
|
|
|
|
return userInfo, nil
|
|
|
|
}
|