@@ -32,13 +32,13 @@ func New(clientKey, secret, callbackURL string, scopes ...string) *Provider {
32
32
33
33
// Provider is the implementation of `goth.Provider` for accessing Google.
34
34
type Provider struct {
35
- ClientKey string
36
- Secret string
37
- CallbackURL string
38
- HTTPClient * http.Client
39
- config * oauth2.Config
40
- prompt oauth2.AuthCodeOption
41
- providerName string
35
+ ClientKey string
36
+ Secret string
37
+ CallbackURL string
38
+ HTTPClient * http.Client
39
+ config * oauth2.Config
40
+ authCodeOptions [] oauth2.AuthCodeOption
41
+ providerName string
42
42
}
43
43
44
44
// Name is the name used to retrieve this provider later.
@@ -61,11 +61,7 @@ func (p *Provider) Debug(debug bool) {}
61
61
62
62
// BeginAuth asks Google for an authentication endpoint.
63
63
func (p * Provider ) BeginAuth (state string ) (goth.Session , error ) {
64
- var opts []oauth2.AuthCodeOption
65
- if p .prompt != nil {
66
- opts = append (opts , p .prompt )
67
- }
68
- url := p .config .AuthCodeURL (state , opts ... )
64
+ url := p .config .AuthCodeURL (state , p .authCodeOptions ... )
69
65
session := & Session {
70
66
AuthURL : url ,
71
67
}
@@ -152,12 +148,12 @@ func newConfig(provider *Provider, scopes []string) *oauth2.Config {
152
148
return c
153
149
}
154
150
155
- //RefreshTokenAvailable refresh token is provided by auth provider or not
151
+ // RefreshTokenAvailable refresh token is provided by auth provider or not
156
152
func (p * Provider ) RefreshTokenAvailable () bool {
157
153
return true
158
154
}
159
155
160
- //RefreshToken get new access token based on the refresh token
156
+ // RefreshToken get new access token based on the refresh token
161
157
func (p * Provider ) RefreshToken (refreshToken string ) (* oauth2.Token , error ) {
162
158
token := & oauth2.Token {RefreshToken : refreshToken }
163
159
ts := p .config .TokenSource (goth .ContextForClient (p .Client ()), token )
@@ -176,5 +172,15 @@ func (p *Provider) SetPrompt(prompt ...string) {
176
172
if len (prompt ) == 0 {
177
173
return
178
174
}
179
- p .prompt = oauth2 .SetAuthURLParam ("prompt" , strings .Join (prompt , " " ))
175
+ p .authCodeOptions = append (p .authCodeOptions , oauth2 .SetAuthURLParam ("prompt" , strings .Join (prompt , " " )))
176
+ }
177
+
178
+ // SetHostedDomain sets the hd parameter for google OAuth call.
179
+ // Use this to force user to pick user from specific hosted domain.
180
+ // See https://developers.google.com/identity/protocols/oauth2/openid-connect#hd-param
181
+ func (p * Provider ) SetHostedDomain (hd string ) {
182
+ if hd == "" {
183
+ return
184
+ }
185
+ p .authCodeOptions = append (p .authCodeOptions , oauth2 .SetAuthURLParam ("hd" , hd ))
180
186
}
0 commit comments