gitlab_oauth.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package social
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "regexp"
  7. "github.com/grafana/grafana/pkg/models"
  8. "golang.org/x/oauth2"
  9. )
  10. type SocialGitlab struct {
  11. *SocialBase
  12. allowedDomains []string
  13. allowedGroups []string
  14. apiUrl string
  15. allowSignup bool
  16. }
  17. var (
  18. ErrMissingGroupMembership = &Error{"User not a member of one of the required groups"}
  19. )
  20. func (s *SocialGitlab) Type() int {
  21. return int(models.GITLAB)
  22. }
  23. func (s *SocialGitlab) IsEmailAllowed(email string) bool {
  24. return isEmailAllowed(email, s.allowedDomains)
  25. }
  26. func (s *SocialGitlab) IsSignupAllowed() bool {
  27. return s.allowSignup
  28. }
  29. func (s *SocialGitlab) IsGroupMember(groups []string) bool {
  30. if len(s.allowedGroups) == 0 {
  31. return true
  32. }
  33. for _, allowedGroup := range s.allowedGroups {
  34. for _, group := range groups {
  35. if group == allowedGroup {
  36. return true
  37. }
  38. }
  39. }
  40. return false
  41. }
  42. func (s *SocialGitlab) GetGroups(client *http.Client) []string {
  43. groups := make([]string, 0)
  44. for page, url := s.GetGroupsPage(client, s.apiUrl+"/groups"); page != nil; page, url = s.GetGroupsPage(client, url) {
  45. groups = append(groups, page...)
  46. }
  47. return groups
  48. }
  49. // GetGroupsPage returns groups and link to the next page if response is paginated
  50. func (s *SocialGitlab) GetGroupsPage(client *http.Client, url string) ([]string, string) {
  51. type Group struct {
  52. FullPath string `json:"full_path"`
  53. }
  54. var (
  55. groups []Group
  56. next string
  57. )
  58. if url == "" {
  59. return nil, next
  60. }
  61. response, err := HttpGet(client, url)
  62. if err != nil {
  63. s.log.Error("Error getting groups from GitLab API", "err", err)
  64. return nil, next
  65. }
  66. if err := json.Unmarshal(response.Body, &groups); err != nil {
  67. s.log.Error("Error parsing JSON from GitLab API", "err", err)
  68. return nil, next
  69. }
  70. fullPaths := make([]string, len(groups))
  71. for i, group := range groups {
  72. fullPaths[i] = group.FullPath
  73. }
  74. // GitLab uses Link header with "rel" set to prev/next/first/last page. We need "next".
  75. if link, ok := response.Headers["Link"]; ok {
  76. pattern := regexp.MustCompile(`<([^>]+)>; rel="next"`)
  77. if matches := pattern.FindStringSubmatch(link[0]); matches != nil {
  78. next = matches[1]
  79. }
  80. }
  81. return fullPaths, next
  82. }
  83. func (s *SocialGitlab) UserInfo(client *http.Client, token *oauth2.Token) (*BasicUserInfo, error) {
  84. var data struct {
  85. Id int
  86. Username string
  87. Email string
  88. Name string
  89. State string
  90. }
  91. response, err := HttpGet(client, s.apiUrl+"/user")
  92. if err != nil {
  93. return nil, fmt.Errorf("Error getting user info: %s", err)
  94. }
  95. err = json.Unmarshal(response.Body, &data)
  96. if err != nil {
  97. return nil, fmt.Errorf("Error getting user info: %s", err)
  98. }
  99. if data.State != "active" {
  100. return nil, fmt.Errorf("User %s is inactive", data.Username)
  101. }
  102. groups := s.GetGroups(client)
  103. userInfo := &BasicUserInfo{
  104. Id: fmt.Sprintf("%d", data.Id),
  105. Name: data.Name,
  106. Login: data.Username,
  107. Email: data.Email,
  108. Groups: groups,
  109. }
  110. if !s.IsGroupMember(groups) {
  111. return nil, ErrMissingGroupMembership
  112. }
  113. return userInfo, nil
  114. }