gitlab_oauth.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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(client *http.Client) bool {
  30. if len(s.allowedGroups) == 0 {
  31. return true
  32. }
  33. for groups, url := s.GetGroups(client, s.apiUrl+"/groups"); groups != nil; groups, url = s.GetGroups(client, url) {
  34. for _, allowedGroup := range s.allowedGroups {
  35. for _, group := range groups {
  36. if group == allowedGroup {
  37. return true
  38. }
  39. }
  40. }
  41. }
  42. return false
  43. }
  44. func (s *SocialGitlab) GetGroups(client *http.Client, url string) ([]string, string) {
  45. type Group struct {
  46. FullPath string `json:"full_path"`
  47. }
  48. var (
  49. groups []Group
  50. next string
  51. )
  52. if url == "" {
  53. return nil, next
  54. }
  55. response, err := HttpGet(client, url)
  56. if err != nil {
  57. s.log.Error("Error getting groups from GitLab API", "err", err)
  58. return nil, next
  59. }
  60. if err := json.Unmarshal(response.Body, &groups); err != nil {
  61. s.log.Error("Error parsing JSON from GitLab API", "err", err)
  62. return nil, next
  63. }
  64. fullPaths := make([]string, len(groups))
  65. for i, group := range groups {
  66. fullPaths[i] = group.FullPath
  67. }
  68. if link, ok := response.Headers["Link"]; ok {
  69. pattern := regexp.MustCompile(`<([^>]+)>; rel="next"`)
  70. if matches := pattern.FindStringSubmatch(link[0]); matches != nil {
  71. next = matches[1]
  72. }
  73. }
  74. return fullPaths, next
  75. }
  76. func (s *SocialGitlab) UserInfo(client *http.Client, token *oauth2.Token) (*BasicUserInfo, error) {
  77. var data struct {
  78. Id int
  79. Username string
  80. Email string
  81. Name string
  82. State string
  83. }
  84. response, err := HttpGet(client, s.apiUrl+"/user")
  85. if err != nil {
  86. return nil, fmt.Errorf("Error getting user info: %s", err)
  87. }
  88. err = json.Unmarshal(response.Body, &data)
  89. if err != nil {
  90. return nil, fmt.Errorf("Error getting user info: %s", err)
  91. }
  92. if data.State != "active" {
  93. return nil, fmt.Errorf("User %s is inactive", data.Username)
  94. }
  95. userInfo := &BasicUserInfo{
  96. Name: data.Name,
  97. Login: data.Username,
  98. Email: data.Email,
  99. }
  100. if !s.IsGroupMember(client) {
  101. return nil, ErrMissingGroupMembership
  102. }
  103. return userInfo, nil
  104. }