social.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. HostedDomain: sec.Key("hosted_domain").String(),
  45. AllowSignup: sec.Key("allow_sign_up").MustBool(),
  46. Name: sec.Key("name").MustString(name),
  47. TlsClientCert: sec.Key("tls_client_cert").String(),
  48. TlsClientKey: sec.Key("tls_client_key").String(),
  49. TlsClientCa: sec.Key("tls_client_ca").String(),
  50. }
  51. if !info.Enabled {
  52. continue
  53. }
  54. setting.OAuthService.OAuthInfos[name] = info
  55. config := oauth2.Config{
  56. ClientID: info.ClientId,
  57. ClientSecret: info.ClientSecret,
  58. Endpoint: oauth2.Endpoint{
  59. AuthURL: info.AuthUrl,
  60. TokenURL: info.TokenUrl,
  61. },
  62. RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
  63. Scopes: info.Scopes,
  64. }
  65. // GitHub.
  66. if name == "github" {
  67. SocialMap["github"] = &SocialGithub{
  68. Config: &config,
  69. allowedDomains: info.AllowedDomains,
  70. apiUrl: info.ApiUrl,
  71. allowSignup: info.AllowSignup,
  72. teamIds: sec.Key("team_ids").Ints(","),
  73. allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
  74. }
  75. }
  76. // Google.
  77. if name == "google" {
  78. SocialMap["google"] = &SocialGoogle{
  79. Config: &config,
  80. allowedDomains: info.AllowedDomains,
  81. hostedDomain: info.HostedDomain,
  82. apiUrl: info.ApiUrl,
  83. allowSignup: info.AllowSignup,
  84. }
  85. }
  86. // Generic - Uses the same scheme as Github.
  87. if name == "generic_oauth" {
  88. SocialMap["generic_oauth"] = &GenericOAuth{
  89. Config: &config,
  90. allowedDomains: info.AllowedDomains,
  91. apiUrl: info.ApiUrl,
  92. allowSignup: info.AllowSignup,
  93. teamIds: sec.Key("team_ids").Ints(","),
  94. allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
  95. }
  96. }
  97. if name == "grafananet" {
  98. config = oauth2.Config{
  99. ClientID: info.ClientId,
  100. ClientSecret: info.ClientSecret,
  101. Endpoint: oauth2.Endpoint{
  102. AuthURL: setting.GrafanaNetUrl + "/oauth2/authorize",
  103. TokenURL: setting.GrafanaNetUrl + "/api/oauth2/token",
  104. },
  105. RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
  106. Scopes: info.Scopes,
  107. }
  108. SocialMap["grafananet"] = &SocialGrafanaNet{
  109. Config: &config,
  110. url: setting.GrafanaNetUrl,
  111. allowSignup: info.AllowSignup,
  112. allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
  113. }
  114. }
  115. }
  116. }