social.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "github.com/grafana/grafana/pkg/util"
  9. )
  10. type BasicUserInfo struct {
  11. Name string
  12. Email string
  13. Login string
  14. Company string
  15. Role string
  16. }
  17. type SocialConnector interface {
  18. Type() int
  19. UserInfo(client *http.Client) (*BasicUserInfo, error)
  20. IsEmailAllowed(email string) bool
  21. IsSignupAllowed() bool
  22. AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string
  23. Exchange(ctx context.Context, code string) (*oauth2.Token, error)
  24. Client(ctx context.Context, t *oauth2.Token) *http.Client
  25. }
  26. type Error struct {
  27. s string
  28. }
  29. func (e *Error) Error() string {
  30. return e.s
  31. }
  32. var (
  33. SocialBaseUrl = "/login/"
  34. SocialMap = make(map[string]SocialConnector)
  35. )
  36. func NewOAuthService() {
  37. setting.OAuthService = &setting.OAuther{}
  38. setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo)
  39. allOauthes := []string{"github", "google", "generic_oauth", "grafananet", "grafana_com"}
  40. for _, name := range allOauthes {
  41. sec := setting.Cfg.Section("auth." + name)
  42. info := &setting.OAuthInfo{
  43. ClientId: sec.Key("client_id").String(),
  44. ClientSecret: sec.Key("client_secret").String(),
  45. Scopes: util.SplitString(sec.Key("scopes").String()),
  46. AuthUrl: sec.Key("auth_url").String(),
  47. TokenUrl: sec.Key("token_url").String(),
  48. ApiUrl: sec.Key("api_url").String(),
  49. Enabled: sec.Key("enabled").MustBool(),
  50. AllowedDomains: util.SplitString(sec.Key("allowed_domains").String()),
  51. HostedDomain: sec.Key("hosted_domain").String(),
  52. AllowSignup: sec.Key("allow_sign_up").MustBool(),
  53. Name: sec.Key("name").MustString(name),
  54. TlsClientCert: sec.Key("tls_client_cert").String(),
  55. TlsClientKey: sec.Key("tls_client_key").String(),
  56. TlsClientCa: sec.Key("tls_client_ca").String(),
  57. }
  58. if !info.Enabled {
  59. continue
  60. }
  61. if name == "grafananet" {
  62. name = "grafana_com"
  63. }
  64. setting.OAuthService.OAuthInfos[name] = info
  65. config := oauth2.Config{
  66. ClientID: info.ClientId,
  67. ClientSecret: info.ClientSecret,
  68. Endpoint: oauth2.Endpoint{
  69. AuthURL: info.AuthUrl,
  70. TokenURL: info.TokenUrl,
  71. },
  72. RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
  73. Scopes: info.Scopes,
  74. }
  75. // GitHub.
  76. if name == "github" {
  77. SocialMap["github"] = &SocialGithub{
  78. Config: &config,
  79. allowedDomains: info.AllowedDomains,
  80. apiUrl: info.ApiUrl,
  81. allowSignup: info.AllowSignup,
  82. teamIds: sec.Key("team_ids").Ints(","),
  83. allowedOrganizations: util.SplitString(sec.Key("allowed_organizations").String()),
  84. }
  85. }
  86. // Google.
  87. if name == "google" {
  88. SocialMap["google"] = &SocialGoogle{
  89. Config: &config,
  90. allowedDomains: info.AllowedDomains,
  91. hostedDomain: info.HostedDomain,
  92. apiUrl: info.ApiUrl,
  93. allowSignup: info.AllowSignup,
  94. }
  95. }
  96. // Generic - Uses the same scheme as Github.
  97. if name == "generic_oauth" {
  98. SocialMap["generic_oauth"] = &GenericOAuth{
  99. Config: &config,
  100. allowedDomains: info.AllowedDomains,
  101. apiUrl: info.ApiUrl,
  102. allowSignup: info.AllowSignup,
  103. teamIds: sec.Key("team_ids").Ints(","),
  104. allowedOrganizations: util.SplitString(sec.Key("allowed_organizations").String()),
  105. }
  106. }
  107. if name == "grafana_com" {
  108. config = oauth2.Config{
  109. ClientID: info.ClientId,
  110. ClientSecret: info.ClientSecret,
  111. Endpoint: oauth2.Endpoint{
  112. AuthURL: setting.GrafanaComUrl + "/oauth2/authorize",
  113. TokenURL: setting.GrafanaComUrl + "/api/oauth2/token",
  114. },
  115. RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
  116. Scopes: info.Scopes,
  117. }
  118. SocialMap["grafana_com"] = &SocialGrafanaCom{
  119. Config: &config,
  120. url: setting.GrafanaComUrl,
  121. allowSignup: info.AllowSignup,
  122. allowedOrganizations: util.SplitString(sec.Key("allowed_organizations").String()),
  123. }
  124. }
  125. }
  126. }