social.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package social
  2. import (
  3. "net/http"
  4. "strings"
  5. "golang.org/x/net/context"
  6. "golang.org/x/oauth2"
  7. "github.com/grafana/grafana/pkg/setting"
  8. )
  9. type BasicUserInfo struct {
  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(client *http.Client) (*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. Client(ctx context.Context, t *oauth2.Token) *http.Client
  24. }
  25. var (
  26. SocialBaseUrl = "/login/"
  27. SocialMap = make(map[string]SocialConnector)
  28. )
  29. func NewOAuthService() {
  30. setting.OAuthService = &setting.OAuther{}
  31. setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo)
  32. allOauthes := []string{"github", "google", "generic_oauth", "grafananet"}
  33. for _, name := range allOauthes {
  34. sec := setting.Cfg.Section("auth." + name)
  35. info := &setting.OAuthInfo{
  36. ClientId: sec.Key("client_id").String(),
  37. ClientSecret: sec.Key("client_secret").String(),
  38. Scopes: sec.Key("scopes").Strings(" "),
  39. AuthUrl: sec.Key("auth_url").String(),
  40. TokenUrl: sec.Key("token_url").String(),
  41. ApiUrl: sec.Key("api_url").String(),
  42. Enabled: sec.Key("enabled").MustBool(),
  43. AllowedDomains: sec.Key("allowed_domains").Strings(" "),
  44. AllowSignup: sec.Key("allow_sign_up").MustBool(),
  45. Name: sec.Key("name").MustString(name),
  46. TlsClientCert: sec.Key("tls_client_cert").String(),
  47. TlsClientKey: sec.Key("tls_client_key").String(),
  48. TlsClientCa: sec.Key("tls_client_ca").String(),
  49. }
  50. if !info.Enabled {
  51. continue
  52. }
  53. setting.OAuthService.OAuthInfos[name] = info
  54. config := oauth2.Config{
  55. ClientID: info.ClientId,
  56. ClientSecret: info.ClientSecret,
  57. Endpoint: oauth2.Endpoint{
  58. AuthURL: info.AuthUrl,
  59. TokenURL: info.TokenUrl,
  60. },
  61. RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
  62. Scopes: info.Scopes,
  63. }
  64. // GitHub.
  65. if name == "github" {
  66. SocialMap["github"] = &SocialGithub{
  67. Config: &config,
  68. allowedDomains: info.AllowedDomains,
  69. apiUrl: info.ApiUrl,
  70. allowSignup: info.AllowSignup,
  71. teamIds: sec.Key("team_ids").Ints(","),
  72. allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
  73. }
  74. }
  75. // Google.
  76. if name == "google" {
  77. SocialMap["google"] = &SocialGoogle{
  78. Config: &config,
  79. allowedDomains: info.AllowedDomains,
  80. apiUrl: info.ApiUrl,
  81. allowSignup: info.AllowSignup,
  82. }
  83. }
  84. // Generic - Uses the same scheme as Github.
  85. if name == "generic_oauth" {
  86. SocialMap["generic_oauth"] = &GenericOAuth{
  87. Config: &config,
  88. allowedDomains: info.AllowedDomains,
  89. apiUrl: info.ApiUrl,
  90. allowSignup: info.AllowSignup,
  91. teamIds: sec.Key("team_ids").Ints(","),
  92. allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
  93. }
  94. }
  95. if name == "grafananet" {
  96. config = oauth2.Config{
  97. ClientID: info.ClientId,
  98. ClientSecret: info.ClientSecret,
  99. Endpoint: oauth2.Endpoint{
  100. AuthURL: setting.GrafanaNetUrl + "/oauth2/authorize",
  101. TokenURL: setting.GrafanaNetUrl + "/api/oauth2/token",
  102. },
  103. RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
  104. Scopes: info.Scopes,
  105. }
  106. SocialMap["grafananet"] = &SocialGrafanaNet{
  107. Config: &config,
  108. url: setting.GrafanaNetUrl,
  109. allowSignup: info.AllowSignup,
  110. allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
  111. }
  112. }
  113. }
  114. }