Skip to content

Commit 658fc1a

Browse files
ericuldalltorkelo
authored andcommitted
added hosted domain suppport to google oauth login (grafana#6372)
1 parent 7acdbde commit 658fc1a

File tree

5 files changed

+10
-1
lines changed

5 files changed

+10
-1
lines changed

conf/defaults.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ auth_url = https://accounts.google.com/o/oauth2/auth
229229
token_url = https://accounts.google.com/o/oauth2/token
230230
api_url = https://www.googleapis.com/oauth2/v1/userinfo
231231
allowed_domains =
232+
hosted_domain =
232233

233234
#################################### Grafana.net Auth ####################
234235
[auth.grafananet]

pkg/api/login_oauth.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ func OAuthLogin(ctx *middleware.Context) {
5353
if code == "" {
5454
state := GenStateString()
5555
ctx.Session.Set(middleware.SESS_KEY_OAUTH_STATE, state)
56-
ctx.Redirect(connect.AuthCodeURL(state, oauth2.AccessTypeOnline))
56+
if setting.OAuthService.OAuthInfos[name].HostedDomain == "" {
57+
ctx.Redirect(connect.AuthCodeURL(state, oauth2.AccessTypeOnline))
58+
}else{
59+
ctx.Redirect(connect.AuthCodeURL(state, oauth2.SetParam("hd", setting.OAuthService.OAuthInfos[name].HostedDomain), oauth2.AccessTypeOnline));
60+
}
5761
return
5862
}
5963

pkg/setting/setting_oauth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type OAuthInfo struct {
66
AuthUrl, TokenUrl string
77
Enabled bool
88
AllowedDomains []string
9+
HostedDomain string
910
ApiUrl string
1011
AllowSignup bool
1112
Name string

pkg/social/google_oauth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
type SocialGoogle struct {
1313
*oauth2.Config
1414
allowedDomains []string
15+
hostedDomain string
1516
apiUrl string
1617
allowSignup bool
1718
}

pkg/social/social.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func NewOAuthService() {
5151
ApiUrl: sec.Key("api_url").String(),
5252
Enabled: sec.Key("enabled").MustBool(),
5353
AllowedDomains: sec.Key("allowed_domains").Strings(" "),
54+
HostedDomain: sec.Key("hosted_domain").String(),
5455
AllowSignup: sec.Key("allow_sign_up").MustBool(),
5556
Name: sec.Key("name").MustString(name),
5657
TlsClientCert: sec.Key("tls_client_cert").String(),
@@ -92,6 +93,7 @@ func NewOAuthService() {
9293
SocialMap["google"] = &SocialGoogle{
9394
Config: &config,
9495
allowedDomains: info.AllowedDomains,
96+
hostedDomain: info.HostedDomain,
9597
apiUrl: info.ApiUrl,
9698
allowSignup: info.AllowSignup,
9799
}

0 commit comments

Comments
 (0)