feat(oauth): refactoring PR #6077
This commit is contained in:
+6
-5
@@ -25,11 +25,12 @@ func LoginView(c *middleware.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
viewData.Settings["googleAuthEnabled"] = setting.OAuthService.Google
|
||||
viewData.Settings["githubAuthEnabled"] = setting.OAuthService.GitHub
|
||||
viewData.Settings["grafanaNetAuthEnabled"] = setting.OAuthService.GrafanaNet
|
||||
viewData.Settings["genericOAuthEnabled"] = setting.OAuthService.Generic
|
||||
viewData.Settings["oauthProviderName"] = setting.OAuthService.OAuthProviderName
|
||||
enabledOAuths := make(map[string]interface{})
|
||||
for key, oauth := range setting.OAuthService.OAuthInfos {
|
||||
enabledOAuths[key] = map[string]string{"name": oauth.Name}
|
||||
}
|
||||
|
||||
viewData.Settings["oauth"] = enabledOAuths
|
||||
viewData.Settings["disableUserSignUp"] = !setting.AllowUserSignUp
|
||||
viewData.Settings["loginHint"] = setting.LoginHint
|
||||
viewData.Settings["allowUserPassLogin"] = setting.AllowUserPassLogin
|
||||
|
||||
@@ -8,12 +8,11 @@ type OAuthInfo struct {
|
||||
AllowedDomains []string
|
||||
ApiUrl string
|
||||
AllowSignup bool
|
||||
Name string
|
||||
}
|
||||
|
||||
type OAuther struct {
|
||||
GitHub, Google, Twitter, Generic, GrafanaNet bool
|
||||
OAuthInfos map[string]*OAuthInfo
|
||||
OAuthProviderName string
|
||||
OAuthInfos map[string]*OAuthInfo
|
||||
}
|
||||
|
||||
var OAuthService *OAuther
|
||||
|
||||
+9
-24
@@ -51,6 +51,7 @@ func NewOAuthService() {
|
||||
Enabled: sec.Key("enabled").MustBool(),
|
||||
AllowedDomains: sec.Key("allowed_domains").Strings(" "),
|
||||
AllowSignup: sec.Key("allow_sign_up").MustBool(),
|
||||
Name: sec.Key("name").MustString(name),
|
||||
}
|
||||
|
||||
if !info.Enabled {
|
||||
@@ -71,22 +72,18 @@ func NewOAuthService() {
|
||||
|
||||
// GitHub.
|
||||
if name == "github" {
|
||||
setting.OAuthService.GitHub = true
|
||||
teamIds := sec.Key("team_ids").Ints(",")
|
||||
allowedOrganizations := sec.Key("allowed_organizations").Strings(" ")
|
||||
SocialMap["github"] = &SocialGithub{
|
||||
Config: &config,
|
||||
allowedDomains: info.AllowedDomains,
|
||||
apiUrl: info.ApiUrl,
|
||||
allowSignup: info.AllowSignup,
|
||||
teamIds: teamIds,
|
||||
allowedOrganizations: allowedOrganizations,
|
||||
teamIds: sec.Key("team_ids").Ints(","),
|
||||
allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
|
||||
}
|
||||
}
|
||||
|
||||
// Google.
|
||||
if name == "google" {
|
||||
setting.OAuthService.Google = true
|
||||
SocialMap["google"] = &SocialGoogle{
|
||||
Config: &config, allowedDomains: info.AllowedDomains,
|
||||
apiUrl: info.ApiUrl,
|
||||
@@ -96,35 +93,23 @@ func NewOAuthService() {
|
||||
|
||||
// Generic - Uses the same scheme as Github.
|
||||
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{
|
||||
Config: &config,
|
||||
allowedDomains: info.AllowedDomains,
|
||||
apiUrl: info.ApiUrl,
|
||||
allowSignup: info.AllowSignup,
|
||||
teamIds: teamIds,
|
||||
allowedOrganizations: allowedOrganizations,
|
||||
teamIds: sec.Key("team_ids").Ints(","),
|
||||
allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
|
||||
}
|
||||
}
|
||||
|
||||
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{
|
||||
ClientID: info.ClientId,
|
||||
ClientSecret: info.ClientSecret,
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: url + "/oauth2/authorize",
|
||||
TokenURL: url + "/api/oauth2/token",
|
||||
AuthURL: setting.GrafanaNetUrl + "/oauth2/authorize",
|
||||
TokenURL: setting.GrafanaNetUrl + "/api/oauth2/token",
|
||||
},
|
||||
RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name,
|
||||
Scopes: info.Scopes,
|
||||
@@ -132,9 +117,9 @@ func NewOAuthService() {
|
||||
|
||||
SocialMap["grafananet"] = &SocialGrafanaNet{
|
||||
Config: &config,
|
||||
url: url,
|
||||
url: setting.GrafanaNetUrl,
|
||||
allowSignup: info.AllowSignup,
|
||||
allowedOrganizations: allowedOrganizations,
|
||||
allowedOrganizations: sec.Key("allowed_organizations").Strings(" "),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user