generic_oauth.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package social
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "github.com/grafana/grafana/pkg/models"
  8. "golang.org/x/oauth2"
  9. )
  10. type GenericOAuth struct {
  11. *oauth2.Config
  12. allowedDomains []string
  13. allowedOrganizations []string
  14. apiUrl string
  15. allowSignup bool
  16. teamIds []int
  17. }
  18. func (s *GenericOAuth) Type() int {
  19. return int(models.GENERIC)
  20. }
  21. func (s *GenericOAuth) IsEmailAllowed(email string) bool {
  22. return isEmailAllowed(email, s.allowedDomains)
  23. }
  24. func (s *GenericOAuth) IsSignupAllowed() bool {
  25. return s.allowSignup
  26. }
  27. func (s *GenericOAuth) IsTeamMember(client *http.Client) bool {
  28. if len(s.teamIds) == 0 {
  29. return true
  30. }
  31. teamMemberships, err := s.FetchTeamMemberships(client)
  32. if err != nil {
  33. return false
  34. }
  35. for _, teamId := range s.teamIds {
  36. for _, membershipId := range teamMemberships {
  37. if teamId == membershipId {
  38. return true
  39. }
  40. }
  41. }
  42. return false
  43. }
  44. func (s *GenericOAuth) IsOrganizationMember(client *http.Client) bool {
  45. if len(s.allowedOrganizations) == 0 {
  46. return true
  47. }
  48. organizations, err := s.FetchOrganizations(client)
  49. if err != nil {
  50. return false
  51. }
  52. for _, allowedOrganization := range s.allowedOrganizations {
  53. for _, organization := range organizations {
  54. if organization == allowedOrganization {
  55. return true
  56. }
  57. }
  58. }
  59. return false
  60. }
  61. func (s *GenericOAuth) FetchPrivateEmail(client *http.Client) (string, error) {
  62. type Record struct {
  63. Email string `json:"email"`
  64. Primary bool `json:"primary"`
  65. Verified bool `json:"verified"`
  66. }
  67. emailsUrl := fmt.Sprintf(s.apiUrl + "/emails")
  68. r, err := client.Get(emailsUrl)
  69. if err != nil {
  70. return "", err
  71. }
  72. defer r.Body.Close()
  73. var records []Record
  74. if err = json.NewDecoder(r.Body).Decode(&records); err != nil {
  75. return "", err
  76. }
  77. var email = ""
  78. for _, record := range records {
  79. if record.Primary {
  80. email = record.Email
  81. }
  82. }
  83. return email, nil
  84. }
  85. func (s *GenericOAuth) FetchTeamMemberships(client *http.Client) ([]int, error) {
  86. type Record struct {
  87. Id int `json:"id"`
  88. }
  89. membershipUrl := fmt.Sprintf(s.apiUrl + "/teams")
  90. r, err := client.Get(membershipUrl)
  91. if err != nil {
  92. return nil, err
  93. }
  94. defer r.Body.Close()
  95. var records []Record
  96. if err = json.NewDecoder(r.Body).Decode(&records); err != nil {
  97. return nil, err
  98. }
  99. var ids = make([]int, len(records))
  100. for i, record := range records {
  101. ids[i] = record.Id
  102. }
  103. return ids, nil
  104. }
  105. func (s *GenericOAuth) FetchOrganizations(client *http.Client) ([]string, error) {
  106. type Record struct {
  107. Login string `json:"login"`
  108. }
  109. url := fmt.Sprintf(s.apiUrl + "/orgs")
  110. r, err := client.Get(url)
  111. if err != nil {
  112. return nil, err
  113. }
  114. defer r.Body.Close()
  115. var records []Record
  116. if err = json.NewDecoder(r.Body).Decode(&records); err != nil {
  117. return nil, err
  118. }
  119. var logins = make([]string, len(records))
  120. for i, record := range records {
  121. logins[i] = record.Login
  122. }
  123. return logins, nil
  124. }
  125. func (s *GenericOAuth) UserInfo(client *http.Client) (*BasicUserInfo, error) {
  126. var data struct {
  127. Name string `json:"name"`
  128. Login string `json:"login"`
  129. Username string `json:"username"`
  130. Email string `json:"email"`
  131. Attributes map[string][]string `json:"attributes"`
  132. }
  133. var err error
  134. r, err := client.Get(s.apiUrl)
  135. if err != nil {
  136. return nil, err
  137. }
  138. defer r.Body.Close()
  139. if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
  140. return nil, err
  141. }
  142. userInfo := &BasicUserInfo{
  143. Name: data.Name,
  144. Login: data.Login,
  145. Email: data.Email,
  146. }
  147. if userInfo.Email == "" && data.Attributes["email:primary"] != nil {
  148. userInfo.Email = data.Attributes["email:primary"][0]
  149. }
  150. if userInfo.Email == "" {
  151. userInfo.Email, err = s.FetchPrivateEmail(client)
  152. if err != nil {
  153. return nil, err
  154. }
  155. }
  156. if userInfo.Login == "" && data.Username != "" {
  157. userInfo.Login = data.Username
  158. }
  159. if userInfo.Login == "" {
  160. userInfo.Login = data.Email
  161. }
  162. if !s.IsTeamMember(client) {
  163. return nil, errors.New("User not a member of one of the required teams")
  164. }
  165. if !s.IsOrganizationMember(client) {
  166. return nil, errors.New("User not a member of one of the required organizations")
  167. }
  168. return userInfo, nil
  169. }