social.go 3.9 KB

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