generic_oauth.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package social
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  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. Verified bool `json:"verified"`
  67. }
  68. emailsUrl := fmt.Sprintf(s.apiUrl + "/emails")
  69. r, err := client.Get(emailsUrl)
  70. if err != nil {
  71. return "", err
  72. }
  73. defer r.Body.Close()
  74. var records []Record
  75. if err = json.NewDecoder(r.Body).Decode(&records); err != nil {
  76. return "", err
  77. }
  78. var email = ""
  79. for _, record := range records {
  80. if record.Primary {
  81. email = record.Email
  82. }
  83. }
  84. return email, nil
  85. }
  86. func (s *GenericOAuth) FetchTeamMemberships(client *http.Client) ([]int, error) {
  87. type Record struct {
  88. Id int `json:"id"`
  89. }
  90. membershipUrl := fmt.Sprintf(s.apiUrl + "/teams")
  91. r, err := client.Get(membershipUrl)
  92. if err != nil {
  93. return nil, err
  94. }
  95. defer r.Body.Close()
  96. var records []Record
  97. if err = json.NewDecoder(r.Body).Decode(&records); err != nil {
  98. return nil, err
  99. }
  100. var ids = make([]int, len(records))
  101. for i, record := range records {
  102. ids[i] = record.Id
  103. }
  104. return ids, nil
  105. }
  106. func (s *GenericOAuth) FetchOrganizations(client *http.Client) ([]string, error) {
  107. type Record struct {
  108. Login string `json:"login"`
  109. }
  110. url := fmt.Sprintf(s.apiUrl + "/orgs")
  111. r, err := client.Get(url)
  112. if err != nil {
  113. return nil, err
  114. }
  115. defer r.Body.Close()
  116. var records []Record
  117. if err = json.NewDecoder(r.Body).Decode(&records); err != nil {
  118. return nil, err
  119. }
  120. var logins = make([]string, len(records))
  121. for i, record := range records {
  122. logins[i] = record.Login
  123. }
  124. return logins, nil
  125. }
  126. func (s *GenericOAuth) UserInfo(token *oauth2.Token) (*BasicUserInfo, error) {
  127. var data struct {
  128. Id int `json:"id"`
  129. Name string `json:"login"`
  130. Email string `json:"email"`
  131. }
  132. var err error
  133. client := s.Client(oauth2.NoContext, token)
  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. Identity: strconv.Itoa(data.Id),
  144. Name: data.Name,
  145. Email: data.Email,
  146. }
  147. if !s.IsTeamMember(client) {
  148. return nil, errors.New("User not a member of one of the required teams")
  149. }
  150. if !s.IsOrganizationMember(client) {
  151. return nil, errors.New("User not a member of one of the required organizations")
  152. }
  153. if userInfo.Email == "" {
  154. userInfo.Email, err = s.FetchPrivateEmail(client)
  155. if err != nil {
  156. return nil, err
  157. }
  158. }
  159. return userInfo, nil
  160. }