generic_oauth.go 4.6 KB

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