diff --git a/.gitignore b/.gitignore index efb68d2d831..2bd8d89bbd2 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ config.js .idea/ data/sessions +grafana-pro diff --git a/conf/grafana.ini b/conf/grafana.ini index 54777e91a11..f6438d88bbc 100644 --- a/conf/grafana.ini +++ b/conf/grafana.ini @@ -34,6 +34,25 @@ session_id_hashfunc = sha1 ; Session hash key, default is use random string session_id_hashkey = +[oauth] +enabled = true + +[oauth.github] +enabled = true +client_id = de054205006b9baa2e17 +client_secret = 72b7ea52d9f1096fdf36cea95e95362a307e0322 +scopes = user:email +auth_url = https://github.com/login/oauth/authorize +token_url = https://github.com/login/oauth/access_token + +[oauth.google] +enabled = true +client_id = 106011922963-4pvl05e9urtrm8bbqr0vouosj3e8p8kb.apps.googleusercontent.com +client_secret = K2evIa4QhfbhhAm3SO72t2Zv +scopes = https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email +auth_url = https://accounts.google.com/o/oauth2/auth +token_url = https://accounts.google.com/o/oauth2/token + [log] root_path = ; Either "console", "file", "conn", "smtp" or "database", default is "console" diff --git a/grafana-pro b/grafana-pro deleted file mode 100755 index 7a82e7770d8..00000000000 Binary files a/grafana-pro and /dev/null differ diff --git a/pkg/cmd/web.go b/pkg/cmd/web.go index 76d26b7ed2c..15cc152405f 100644 --- a/pkg/cmd/web.go +++ b/pkg/cmd/web.go @@ -16,6 +16,7 @@ import ( "github.com/torkelo/grafana-pro/pkg/middleware" "github.com/torkelo/grafana-pro/pkg/routes" "github.com/torkelo/grafana-pro/pkg/setting" + "github.com/torkelo/grafana-pro/pkg/social" "github.com/torkelo/grafana-pro/pkg/stores/rethink" ) @@ -65,6 +66,7 @@ func runWeb(*cli.Context) { setting.NewConfigContext() setting.InitServices() rethink.Init() + social.NewOAuthService() log.Info("Starting Grafana-Pro v.1-alpha") diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index 8dd0919e5fb..f9d4705f515 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -3,6 +3,7 @@ package middleware import ( "encoding/json" "io/ioutil" + "strconv" "github.com/Unknwon/macaron" "github.com/macaron-contrib/session" @@ -52,7 +53,7 @@ func (ctx *Context) Handle(status int, title string, err error) { ctx.Data["Title"] = "Internal Server Error" } - ctx.HTML(status, "index") + ctx.HTML(status, strconv.Itoa(status)) } func (ctx *Context) JsonApiErr(status int, message string, err error) { diff --git a/pkg/routes/login/login_oauth.go b/pkg/routes/login/login_oauth.go index 6b31846a79a..0fac31ff088 100644 --- a/pkg/routes/login/login_oauth.go +++ b/pkg/routes/login/login_oauth.go @@ -13,14 +13,14 @@ import ( func OAuthLogin(ctx *middleware.Context) { if setting.OAuthService == nil { - ctx.Handle(404, "social.SocialSignIn(oauth service not enabled)", nil) + ctx.Handle(404, "login.OAuthLogin(oauth service not enabled)", nil) return } name := ctx.Params(":name") connect, ok := social.SocialMap[name] if !ok { - ctx.Handle(404, "social.SocialSignIn(social login not enabled)", errors.New(name)) + ctx.Handle(404, "login.OAuthLogin(social login not enabled)", errors.New(name)) return } @@ -29,23 +29,24 @@ func OAuthLogin(ctx *middleware.Context) { ctx.Redirect(connect.AuthCodeURL("", "online", "auto")) return } + log.Info("code: %v", code) // handle call back transport, err := connect.NewTransportWithCode(code) if err != nil { - ctx.Handle(500, "social.SocialSignIn(NewTransportWithCode)", err) + ctx.Handle(500, "login.OAuthLogin(NewTransportWithCode)", err) return } - log.Trace("social.SocialSignIn(Got token)") + log.Trace("login.OAuthLogin(Got token)") userInfo, err := connect.UserInfo(transport) if err != nil { - ctx.Handle(500, fmt.Sprintf("social.SocialSignIn(get info from %s)", name), err) + ctx.Handle(500, fmt.Sprintf("login.OAuthLogin(get info from %s)", name), err) return } - log.Info("social.SocialSignIn(social login): %s", userInfo) + log.Info("login.OAuthLogin(social login): %s", userInfo) account, err := models.GetAccountByLogin(userInfo.Email) diff --git a/pkg/setting/setting_oauth.go b/pkg/setting/setting_oauth.go index 24896c00ab6..43a022079b7 100644 --- a/pkg/setting/setting_oauth.go +++ b/pkg/setting/setting_oauth.go @@ -4,6 +4,7 @@ type OAuthInfo struct { ClientId, ClientSecret string Scopes []string AuthUrl, TokenUrl string + Enabled bool } type OAuther struct { diff --git a/pkg/social/social.go b/pkg/social/social.go index 168900a1e4e..87231278a74 100644 --- a/pkg/social/social.go +++ b/pkg/social/social.go @@ -29,31 +29,33 @@ type SocialConnector interface { } var ( - SocialBaseUrl = "/login" + SocialBaseUrl = "/login/" SocialMap = make(map[string]SocialConnector) ) -func NewOauthService() { +func NewOAuthService() { if !setting.Cfg.MustBool("oauth", "enabled") { return } - var err error setting.OAuthService = &setting.OAuther{} setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo) - socialConfigs := make(map[string]*oauth2.Config) - allOauthes := []string{"github", "google", "twitter"} // Load all OAuth config data. for _, name := range allOauthes { info := &setting.OAuthInfo{ ClientId: setting.Cfg.MustValue("oauth."+name, "client_id"), - ClientSecret: setting.Cfg.MustValue("oauth."+name, "client_secrect"), + ClientSecret: setting.Cfg.MustValue("oauth."+name, "client_secret"), Scopes: setting.Cfg.MustValueArray("oauth."+name, "scopes", " "), AuthUrl: setting.Cfg.MustValue("oauth."+name, "auth_url"), TokenUrl: setting.Cfg.MustValue("oauth."+name, "token_url"), + Enabled: setting.Cfg.MustBool("oauth."+name, "enabled"), + } + + if !info.Enabled { + continue } opts := &oauth2.Options{ @@ -64,26 +66,24 @@ func NewOauthService() { } setting.OAuthService.OAuthInfos[name] = info - socialConfigs[name], err = oauth2.NewConfig(opts, info.AuthUrl, info.TokenUrl) + config, err := oauth2.NewConfig(opts, info.AuthUrl, info.TokenUrl) + if err != nil { - log.Error(4, "Failed to init oauth service", err) + log.Error(3, "Failed to init oauth service", err) + continue } - } - enabledOauths := make([]string, 0, 10) + // GitHub. + if name == "github" { + setting.OAuthService.GitHub = true + SocialMap["github"] = &SocialGithub{Config: config} + } - // GitHub. - if setting.Cfg.MustBool("oauth.github", "enabled") { - setting.OAuthService.GitHub = true - newGitHubOAuth(socialConfigs["github"]) - enabledOauths = append(enabledOauths, "GitHub") - } - - // Google. - if setting.Cfg.MustBool("oauth.google", "enabled") { - setting.OAuthService.Google = true - newGoogleOAuth(socialConfigs["google"]) - enabledOauths = append(enabledOauths, "Google") + // Google. + if name == "google" { + setting.OAuthService.Google = true + SocialMap["google"] = &SocialGoogle{Config: config} + } } } @@ -95,12 +95,6 @@ func (s *SocialGithub) Type() int { return int(models.GITHUB) } -func newGitHubOAuth(config *oauth2.Config) { - SocialMap["github"] = &SocialGithub{ - Config: config, - } -} - func (s *SocialGithub) UserInfo(transport *oauth2.Transport) (*BasicUserInfo, error) { var data struct { Id int `json:"id"` @@ -143,12 +137,6 @@ func (s *SocialGoogle) Type() int { return int(models.GOOGLE) } -func newGoogleOAuth(config *oauth2.Config) { - SocialMap["google"] = &SocialGoogle{ - Config: config, - } -} - func (s *SocialGoogle) UserInfo(transport *oauth2.Transport) (*BasicUserInfo, error) { var data struct { Id string `json:"id"` diff --git a/views/404.html b/views/404.html index e69de29bb2d..7f975a35e3b 100644 --- a/views/404.html +++ b/views/404.html @@ -0,0 +1,18 @@ + + +
+ + + + +