social.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package social
  2. import (
  3. "strings"
  4. "github.com/grafana/grafana/pkg/setting"
  5. "golang.org/x/net/context"
  6. "golang.org/x/oauth2"
  7. )
  8. type BasicUserInfo struct {
  9. Identity string
  10. Name string
  11. Email string
  12. Login string
  13. Company string
  14. Role string
  15. }
  16. type SocialConnector interface {
  17. Type() int
  18. UserInfo(token *oauth2.Token) (*BasicUserInfo, error)
  19. IsEmailAllowed(email string) bool
  20. IsSignupAllowed() bool
  21. AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string
  22. Exchange(ctx context.Context, code string) (*oauth2.Token, error)
  23. }
  24. var (
  25. SocialBaseUrl = "/login/"
  26. SocialMap = make(map[string]SocialConnector)
  27. )
  28. func NewOAuthService() {
  29. setting.OAuthService = &setting.OAuther{}
  30. setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo)
  31. allOauthes := []string{"github", "google", "generic_oauth", "grafananet"}
  32. for _, name := range allOauthes {
  33. sec := setting.Cfg.Section("auth." + name)
  34. info := &setting.OAuthInfo{
  35. ClientId: sec.Key("client_id").String(),
  36. ClientSecret: sec.Key("client_secret").String(),
  37. Scopes: sec.Key("scopes").Strings(" "),
  38. AuthUrl: sec.Key("auth_url").String(),
  39. TokenUrl: sec.Key("token_url").String(),
  40. ApiUrl: sec.Key("api_url").String(),
  41. Enabled: sec.Key("enabled").MustBool(),
  42. AllowedDomains: sec.Key("allowed_domains").Strings(" "),
  43. AllowSignup: sec.Key("allow_sign_up").MustBool(),
  44. Name: sec.Key("name").MustString(name),
  45. }
  46. if !info.Enabled {
  47. continue
  48. }
  49. setting.OAuthService.OAuthInfos[name] = info
  50. config := oauth2.Config{
  51. ClientID: info.ClientId,
  52. ClientSecret: info.ClientSecret,
  53. Endpoint: oauth2.Endpoint{
  54. AuthURL: info.AuthUrl,
  55. TokenURL: info.TokenUrl,
  56. },
  57. RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
  58. Scopes: info.Scopes,
  59. }
  60. // GitHub.
  61. if name == "github" {
  62. SocialMap["github"] = &SocialGithub{
  63. Config: &config,
  64. allowedDomains: info.AllowedDomains,
  65. apiUrl: info.ApiUrl,
  66. allowSignup: info.AllowSignup,
  67. teamIds: sec.Key("team_ids").Ints(","),
  68. allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
  69. }
  70. }
  71. // Google.
  72. if name == "google" {
  73. SocialMap["google"] = &SocialGoogle{
  74. Config: &config, allowedDomains: info.AllowedDomains,
  75. apiUrl: info.ApiUrl,
  76. allowSignup: info.AllowSignup,
  77. }
  78. }
  79. // Generic - Uses the same scheme as Github.
  80. if name == "generic_oauth" {
  81. SocialMap["generic_oauth"] = &GenericOAuth{
  82. Config: &config,
  83. allowedDomains: info.AllowedDomains,
  84. apiUrl: info.ApiUrl,
  85. allowSignup: info.AllowSignup,
  86. teamIds: sec.Key("team_ids").Ints(","),
  87. allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
  88. }
  89. }
  90. if name == "grafananet" {
  91. config := oauth2.Config{
  92. ClientID: info.ClientId,
  93. ClientSecret: info.ClientSecret,
  94. Endpoint: oauth2.Endpoint{
  95. AuthURL: setting.GrafanaNetUrl + "/oauth2/authorize",
  96. TokenURL: setting.GrafanaNetUrl + "/api/oauth2/token",
  97. },
  98. RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
  99. Scopes: info.Scopes,
  100. }
  101. SocialMap["grafananet"] = &SocialGrafanaNet{
  102. Config: &config,
  103. url: setting.GrafanaNetUrl,
  104. allowSignup: info.AllowSignup,
  105. allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
  106. }
  107. }
  108. }
  109. }