social.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }
  15. type SocialConnector interface {
  16. Type() int
  17. UserInfo(token *oauth2.Token) (*BasicUserInfo, error)
  18. IsEmailAllowed(email string) bool
  19. IsSignupAllowed() bool
  20. AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string
  21. Exchange(ctx context.Context, code string) (*oauth2.Token, error)
  22. }
  23. var (
  24. SocialBaseUrl = "/login/"
  25. SocialMap = make(map[string]SocialConnector)
  26. )
  27. func NewOAuthService() {
  28. setting.OAuthService = &setting.OAuther{}
  29. setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo)
  30. allOauthes := []string{"github", "google", "generic_oauth"}
  31. for _, name := range allOauthes {
  32. sec := setting.Cfg.Section("auth." + name)
  33. info := &setting.OAuthInfo{
  34. ClientId: sec.Key("client_id").String(),
  35. ClientSecret: sec.Key("client_secret").String(),
  36. Scopes: sec.Key("scopes").Strings(" "),
  37. AuthUrl: sec.Key("auth_url").String(),
  38. TokenUrl: sec.Key("token_url").String(),
  39. ApiUrl: sec.Key("api_url").String(),
  40. Enabled: sec.Key("enabled").MustBool(),
  41. AllowedDomains: sec.Key("allowed_domains").Strings(" "),
  42. AllowSignup: sec.Key("allow_sign_up").MustBool(),
  43. }
  44. if !info.Enabled {
  45. continue
  46. }
  47. setting.OAuthService.OAuthInfos[name] = info
  48. config := oauth2.Config{
  49. ClientID: info.ClientId,
  50. ClientSecret: info.ClientSecret,
  51. Endpoint: oauth2.Endpoint{
  52. AuthURL: info.AuthUrl,
  53. TokenURL: info.TokenUrl,
  54. },
  55. RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
  56. Scopes: info.Scopes,
  57. }
  58. // GitHub.
  59. if name == "github" {
  60. setting.OAuthService.GitHub = true
  61. teamIds := sec.Key("team_ids").Ints(",")
  62. allowedOrganizations := sec.Key("allowed_organizations").Strings(" ")
  63. SocialMap["github"] = &SocialGithub{
  64. Config: &config,
  65. allowedDomains: info.AllowedDomains,
  66. apiUrl: info.ApiUrl,
  67. allowSignup: info.AllowSignup,
  68. teamIds: teamIds,
  69. allowedOrganizations: allowedOrganizations,
  70. }
  71. }
  72. // Google.
  73. if name == "google" {
  74. setting.OAuthService.Google = true
  75. SocialMap["google"] = &SocialGoogle{
  76. Config: &config, allowedDomains: info.AllowedDomains,
  77. apiUrl: info.ApiUrl,
  78. allowSignup: info.AllowSignup,
  79. }
  80. }
  81. // Generic - Uses the same scheme as Github.
  82. if name == "generic_oauth" {
  83. setting.OAuthService.Generic = true
  84. setting.OAuthService.OAuthProviderName = sec.Key("oauth_provider_name").String()
  85. teamIds := sec.Key("team_ids").Ints(",")
  86. allowedOrganizations := sec.Key("allowed_organizations").Strings(" ")
  87. SocialMap["generic_oauth"] = &GenericOAuth{
  88. Config: &config,
  89. allowedDomains: info.AllowedDomains,
  90. apiUrl: info.ApiUrl,
  91. allowSignup: info.AllowSignup,
  92. teamIds: teamIds,
  93. allowedOrganizations: allowedOrganizations,
  94. }
  95. }
  96. }
  97. }