Skip to content

Commit 8d5b578

Browse files
authored
Add authCodeOptions to openidConnect Provider (#580)
* feat: add authURLParams to BeginAuth * test: add test case for openidConnect authCodeOptions * style: whitespace
1 parent 8ba0d36 commit 8d5b578

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

providers/openidConnect/openidConnect.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ const (
5151

5252
// Provider is the implementation of `goth.Provider` for accessing OpenID Connect provider
5353
type Provider struct {
54-
ClientKey string
55-
Secret string
56-
CallbackURL string
57-
HTTPClient *http.Client
58-
OpenIDConfig *OpenIDConfig
59-
config *oauth2.Config
60-
providerName string
54+
ClientKey string
55+
Secret string
56+
CallbackURL string
57+
HTTPClient *http.Client
58+
OpenIDConfig *OpenIDConfig
59+
config *oauth2.Config
60+
authCodeOptions []oauth2.AuthCodeOption
61+
providerName string
6162

6263
UserIdClaims []string
6364
NameClaims []string
@@ -186,6 +187,14 @@ func (p *Provider) SetName(name string) {
186187
p.providerName = name
187188
}
188189

190+
// SetAuthCodeOptions sets additional parameters for the authentication URL.
191+
// It takes a map of string key-value pairs and appends them to the provider's authCodeOptions.
192+
func (p *Provider) SetAuthCodeOptions(params map[string]string) {
193+
for k, v := range params {
194+
p.authCodeOptions = append(p.authCodeOptions, oauth2.SetAuthURLParam(k, v))
195+
}
196+
}
197+
189198
func (p *Provider) Client() *http.Client {
190199
return goth.HTTPClientWithFallBack(p.HTTPClient)
191200
}
@@ -195,7 +204,7 @@ func (p *Provider) Debug(debug bool) {}
195204

196205
// BeginAuth asks the OpenID Connect provider for an authentication end-point.
197206
func (p *Provider) BeginAuth(state string) (goth.Session, error) {
198-
url := p.config.AuthCodeURL(state)
207+
url := p.config.AuthCodeURL(state, p.authCodeOptions...)
199208
session := &Session{
200209
AuthURL: url,
201210
}

providers/openidConnect/openidConnect_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ func Test_BeginAuth(t *testing.T) {
7878
a.Contains(s.AuthURL, "scope=openid")
7979
}
8080

81+
func Test_BeginAuth_AuthCodeOptions(t *testing.T) {
82+
t.Parallel()
83+
a := assert.New(t)
84+
85+
provider := openidConnectProvider()
86+
provider.SetAuthCodeOptions(map[string]string{"domain_hint": "test_domain.com", "prompt": "none"})
87+
session, err := provider.BeginAuth("test_state")
88+
s := session.(*Session)
89+
a.NoError(err)
90+
a.Contains(s.AuthURL, "https://accounts.google.com/o/oauth2/v2/auth")
91+
a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("OPENID_CONNECT_KEY")))
92+
a.Contains(s.AuthURL, "state=test_state")
93+
a.Contains(s.AuthURL, "redirect_uri=http%3A%2F%2Flocalhost%2Ffoo")
94+
a.Contains(s.AuthURL, "scope=openid")
95+
a.Contains(s.AuthURL, "domain_hint=test_domain.com")
96+
a.Contains(s.AuthURL, "prompt=none")
97+
}
98+
8199
func Test_Implements_Provider(t *testing.T) {
82100
t.Parallel()
83101
a := assert.New(t)

0 commit comments

Comments
 (0)