Files
grafana/pkg/social/social.go

127 lines
3.5 KiB
Go
Raw Normal View History

2014-10-07 15:54:38 -04:00
package social
import (
"strings"
2015-02-05 10:37:13 +01:00
"github.com/grafana/grafana/pkg/setting"
"golang.org/x/net/context"
2014-11-28 11:51:34 +01:00
2014-12-30 10:10:13 +01:00
"golang.org/x/oauth2"
2014-10-07 15:54:38 -04:00
)
type BasicUserInfo struct {
Identity string
Name string
Email string
Login string
Company string
Role string
2014-10-07 15:54:38 -04:00
}
type SocialConnector interface {
Type() int
2014-12-30 10:10:13 +01:00
UserInfo(token *oauth2.Token) (*BasicUserInfo, error)
IsEmailAllowed(email string) bool
IsSignupAllowed() bool
2014-10-07 15:54:38 -04:00
2014-12-30 10:10:13 +01:00
AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string
Exchange(ctx context.Context, code string) (*oauth2.Token, error)
2014-10-07 15:54:38 -04:00
}
var (
2014-10-07 17:56:37 -04:00
SocialBaseUrl = "/login/"
2014-10-07 15:54:38 -04:00
SocialMap = make(map[string]SocialConnector)
)
2014-10-07 17:56:37 -04:00
func NewOAuthService() {
2014-10-07 15:54:38 -04:00
setting.OAuthService = &setting.OAuther{}
setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo)
allOauthes := []string{"github", "google", "generic_oauth", "grafananet"}
2014-10-07 15:54:38 -04:00
for _, name := range allOauthes {
sec := setting.Cfg.Section("auth." + name)
2014-10-07 15:54:38 -04:00
info := &setting.OAuthInfo{
ClientId: sec.Key("client_id").String(),
ClientSecret: sec.Key("client_secret").String(),
Scopes: sec.Key("scopes").Strings(" "),
AuthUrl: sec.Key("auth_url").String(),
TokenUrl: sec.Key("token_url").String(),
ApiUrl: sec.Key("api_url").String(),
Enabled: sec.Key("enabled").MustBool(),
AllowedDomains: sec.Key("allowed_domains").Strings(" "),
AllowSignup: sec.Key("allow_sign_up").MustBool(),
2016-09-28 15:10:50 +02:00
Name: sec.Key("name").MustString(name),
2014-10-07 17:56:37 -04:00
}
if !info.Enabled {
continue
2014-10-07 15:54:38 -04:00
}
setting.OAuthService.OAuthInfos[name] = info
2014-12-30 10:10:13 +01:00
config := oauth2.Config{
ClientID: info.ClientId,
ClientSecret: info.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: info.AuthUrl,
TokenURL: info.TokenUrl,
},
RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
Scopes: info.Scopes,
2014-10-07 15:54:38 -04:00
}
2014-10-07 17:56:37 -04:00
// GitHub.
if name == "github" {
2015-04-29 09:49:22 +02:00
SocialMap["github"] = &SocialGithub{
2015-05-23 17:06:51 +03:00
Config: &config,
allowedDomains: info.AllowedDomains,
apiUrl: info.ApiUrl,
allowSignup: info.AllowSignup,
2016-09-28 15:10:50 +02:00
teamIds: sec.Key("team_ids").Ints(","),
allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
2015-04-29 09:49:22 +02:00
}
2014-10-07 17:56:37 -04:00
}
2014-10-07 15:54:38 -04:00
2014-10-07 17:56:37 -04:00
// Google.
if name == "google" {
2015-04-29 09:49:22 +02:00
SocialMap["google"] = &SocialGoogle{
Config: &config, allowedDomains: info.AllowedDomains,
apiUrl: info.ApiUrl,
allowSignup: info.AllowSignup,
}
2014-10-07 17:56:37 -04:00
}
// Generic - Uses the same scheme as Github.
if name == "generic_oauth" {
SocialMap["generic_oauth"] = &GenericOAuth{
Config: &config,
allowedDomains: info.AllowedDomains,
apiUrl: info.ApiUrl,
allowSignup: info.AllowSignup,
2016-09-28 15:10:50 +02:00
teamIds: sec.Key("team_ids").Ints(","),
allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
}
}
if name == "grafananet" {
config := oauth2.Config{
ClientID: info.ClientId,
ClientSecret: info.ClientSecret,
Endpoint: oauth2.Endpoint{
2016-09-28 15:10:50 +02:00
AuthURL: setting.GrafanaNetUrl + "/oauth2/authorize",
TokenURL: setting.GrafanaNetUrl + "/api/oauth2/token",
},
RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
Scopes: info.Scopes,
}
SocialMap["grafananet"] = &SocialGrafanaNet{
Config: &config,
2016-09-28 15:10:50 +02:00
url: setting.GrafanaNetUrl,
allowSignup: info.AllowSignup,
2016-09-28 15:10:50 +02:00
allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
}
}
2014-10-07 15:54:38 -04:00
}
}