Commit e5fc4332 by Torkel Ödegaard

feat(oauth): refactoring PR #6077

parent 5ccdbf01
...@@ -25,11 +25,12 @@ func LoginView(c *middleware.Context) { ...@@ -25,11 +25,12 @@ func LoginView(c *middleware.Context) {
return return
} }
viewData.Settings["googleAuthEnabled"] = setting.OAuthService.Google enabledOAuths := make(map[string]interface{})
viewData.Settings["githubAuthEnabled"] = setting.OAuthService.GitHub for key, oauth := range setting.OAuthService.OAuthInfos {
viewData.Settings["grafanaNetAuthEnabled"] = setting.OAuthService.GrafanaNet enabledOAuths[key] = map[string]string{"name": oauth.Name}
viewData.Settings["genericOAuthEnabled"] = setting.OAuthService.Generic }
viewData.Settings["oauthProviderName"] = setting.OAuthService.OAuthProviderName
viewData.Settings["oauth"] = enabledOAuths
viewData.Settings["disableUserSignUp"] = !setting.AllowUserSignUp viewData.Settings["disableUserSignUp"] = !setting.AllowUserSignUp
viewData.Settings["loginHint"] = setting.LoginHint viewData.Settings["loginHint"] = setting.LoginHint
viewData.Settings["allowUserPassLogin"] = setting.AllowUserPassLogin viewData.Settings["allowUserPassLogin"] = setting.AllowUserPassLogin
......
...@@ -8,12 +8,11 @@ type OAuthInfo struct { ...@@ -8,12 +8,11 @@ type OAuthInfo struct {
AllowedDomains []string AllowedDomains []string
ApiUrl string ApiUrl string
AllowSignup bool AllowSignup bool
Name string
} }
type OAuther struct { type OAuther struct {
GitHub, Google, Twitter, Generic, GrafanaNet bool OAuthInfos map[string]*OAuthInfo
OAuthInfos map[string]*OAuthInfo
OAuthProviderName string
} }
var OAuthService *OAuther var OAuthService *OAuther
...@@ -51,6 +51,7 @@ func NewOAuthService() { ...@@ -51,6 +51,7 @@ func NewOAuthService() {
Enabled: sec.Key("enabled").MustBool(), Enabled: sec.Key("enabled").MustBool(),
AllowedDomains: sec.Key("allowed_domains").Strings(" "), AllowedDomains: sec.Key("allowed_domains").Strings(" "),
AllowSignup: sec.Key("allow_sign_up").MustBool(), AllowSignup: sec.Key("allow_sign_up").MustBool(),
Name: sec.Key("name").MustString(name),
} }
if !info.Enabled { if !info.Enabled {
...@@ -71,22 +72,18 @@ func NewOAuthService() { ...@@ -71,22 +72,18 @@ func NewOAuthService() {
// GitHub. // GitHub.
if name == "github" { if name == "github" {
setting.OAuthService.GitHub = true
teamIds := sec.Key("team_ids").Ints(",")
allowedOrganizations := sec.Key("allowed_organizations").Strings(" ")
SocialMap["github"] = &SocialGithub{ SocialMap["github"] = &SocialGithub{
Config: &config, Config: &config,
allowedDomains: info.AllowedDomains, allowedDomains: info.AllowedDomains,
apiUrl: info.ApiUrl, apiUrl: info.ApiUrl,
allowSignup: info.AllowSignup, allowSignup: info.AllowSignup,
teamIds: teamIds, teamIds: sec.Key("team_ids").Ints(","),
allowedOrganizations: allowedOrganizations, allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
} }
} }
// Google. // Google.
if name == "google" { if name == "google" {
setting.OAuthService.Google = true
SocialMap["google"] = &SocialGoogle{ SocialMap["google"] = &SocialGoogle{
Config: &config, allowedDomains: info.AllowedDomains, Config: &config, allowedDomains: info.AllowedDomains,
apiUrl: info.ApiUrl, apiUrl: info.ApiUrl,
...@@ -96,35 +93,23 @@ func NewOAuthService() { ...@@ -96,35 +93,23 @@ func NewOAuthService() {
// Generic - Uses the same scheme as Github. // Generic - Uses the same scheme as Github.
if name == "generic_oauth" { if name == "generic_oauth" {
setting.OAuthService.Generic = true
setting.OAuthService.OAuthProviderName = sec.Key("oauth_provider_name").String()
teamIds := sec.Key("team_ids").Ints(",")
allowedOrganizations := sec.Key("allowed_organizations").Strings(" ")
SocialMap["generic_oauth"] = &GenericOAuth{ SocialMap["generic_oauth"] = &GenericOAuth{
Config: &config, Config: &config,
allowedDomains: info.AllowedDomains, allowedDomains: info.AllowedDomains,
apiUrl: info.ApiUrl, apiUrl: info.ApiUrl,
allowSignup: info.AllowSignup, allowSignup: info.AllowSignup,
teamIds: teamIds, teamIds: sec.Key("team_ids").Ints(","),
allowedOrganizations: allowedOrganizations, allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
} }
} }
if name == "grafananet" { if name == "grafananet" {
setting.OAuthService.GrafanaNet = true
allowedOrganizations := sec.Key("allowed_organizations").Strings(" ")
url := sec.Key("url").String()
if url == "" {
url = "https://grafana.net"
}
config := oauth2.Config{ config := oauth2.Config{
ClientID: info.ClientId, ClientID: info.ClientId,
ClientSecret: info.ClientSecret, ClientSecret: info.ClientSecret,
Endpoint: oauth2.Endpoint{ Endpoint: oauth2.Endpoint{
AuthURL: url + "/oauth2/authorize", AuthURL: setting.GrafanaNetUrl + "/oauth2/authorize",
TokenURL: url + "/api/oauth2/token", TokenURL: setting.GrafanaNetUrl + "/api/oauth2/token",
}, },
RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name, RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
Scopes: info.Scopes, Scopes: info.Scopes,
...@@ -132,9 +117,9 @@ func NewOAuthService() { ...@@ -132,9 +117,9 @@ func NewOAuthService() {
SocialMap["grafananet"] = &SocialGrafanaNet{ SocialMap["grafananet"] = &SocialGrafanaNet{
Config: &config, Config: &config,
url: url, url: setting.GrafanaNetUrl,
allowSignup: info.AllowSignup, allowSignup: info.AllowSignup,
allowedOrganizations: allowedOrganizations, allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
} }
} }
} }
......
define([ define([
'angular', 'angular',
'lodash',
'../core_module', '../core_module',
'app/core/config', 'app/core/config',
], ],
function (angular, coreModule, config) { function (angular, _, coreModule, config) {
'use strict'; 'use strict';
var failCodes = { var failCodes = {
...@@ -21,18 +22,10 @@ function (angular, coreModule, config) { ...@@ -21,18 +22,10 @@ function (angular, coreModule, config) {
contextSrv.sidemenu = false; contextSrv.sidemenu = false;
$scope.googleAuthEnabled = config.googleAuthEnabled; $scope.oauth = config.oauth;
$scope.githubAuthEnabled = config.githubAuthEnabled; $scope.oauthEnabled = _.keys(config.oauth).length > 0;
$scope.grafanaNetAuthEnabled = config.grafanaNetAuthEnabled;
$scope.oauthEnabled = (
config.githubAuthEnabled
|| config.googleAuthEnabled
|| config.grafanaNetAuthEnabled
|| config.genericOAuthEnabled
);
$scope.allowUserPassLogin = config.allowUserPassLogin; $scope.allowUserPassLogin = config.allowUserPassLogin;
$scope.genericOAuthEnabled = config.genericOAuthEnabled;
$scope.oauthProviderName = config.oauthProviderName;
$scope.disableUserSignUp = config.disableUserSignUp; $scope.disableUserSignUp = config.disableUserSignUp;
$scope.loginHint = config.loginHint; $scope.loginHint = config.loginHint;
......
...@@ -51,20 +51,20 @@ ...@@ -51,20 +51,20 @@
<div class="clearfix"></div> <div class="clearfix"></div>
<div class="login-oauth text-center" ng-show="oauthEnabled"> <div class="login-oauth text-center" ng-show="oauthEnabled">
<a class="btn btn-large btn-google" href="login/google" target="_self" ng-if="googleAuthEnabled"> <a class="btn btn-large btn-google" href="login/google" target="_self" ng-if="oauth.google">
<i class="fa fa-google"></i> <i class="fa fa-google"></i>
with Google with Google
</a> </a>
<a class="btn btn-large btn-github" href="login/github" target="_self" ng-if="githubAuthEnabled"> <a class="btn btn-large btn-github" href="login/github" target="_self" ng-if="oauth.github">
<i class="fa fa-github"></i> <i class="fa fa-github"></i>
with Github with Github
</a> </a>
<a class="btn btn-large btn-grafana-net" href="login/grafananet" target="_self" ng-if="grafanaNetAuthEnabled"> <a class="btn btn-large btn-grafana-net" href="login/grafananet" target="_self" ng-if="oauth.grafananet">
with <span>Grafana.net</span> with <span>Grafana.net</span>
</a> </a>
<a class="btn btn-large btn-generic-oauth" href="login/generic_oauth" target="_self" ng-if="genericOAuthEnabled"> <a class="btn btn-large btn-generic-oauth" href="login/generic_oauth" target="_self" ng-if="oauth.generic_oauth">
<i class="fa fa-gear"></i> <i class="fa fa-gear"></i>
with {{oauthProviderName || "OAuth 2"}} with {{oauth.generic_oauth.name}}
</a> </a>
</div> </div>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment