Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add authCodeOptions to openidConnect Provider #580

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions providers/openidConnect/openidConnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ const (

// Provider is the implementation of `goth.Provider` for accessing OpenID Connect provider
type Provider struct {
ClientKey string
Secret string
CallbackURL string
HTTPClient *http.Client
OpenIDConfig *OpenIDConfig
config *oauth2.Config
providerName string
ClientKey string
Secret string
CallbackURL string
HTTPClient *http.Client
OpenIDConfig *OpenIDConfig
config *oauth2.Config
authCodeOptions []oauth2.AuthCodeOption
providerName string

UserIdClaims []string
NameClaims []string
Expand Down Expand Up @@ -186,6 +187,14 @@ func (p *Provider) SetName(name string) {
p.providerName = name
}

// SetAuthCodeOptions sets additional parameters for the authentication URL.
// It takes a map of string key-value pairs and appends them to the provider's authCodeOptions.
func (p *Provider) SetAuthCodeOptions(params map[string]string) {
for k, v := range params {
p.authCodeOptions = append(p.authCodeOptions, oauth2.SetAuthURLParam(k, v))
}
}

func (p *Provider) Client() *http.Client {
return goth.HTTPClientWithFallBack(p.HTTPClient)
}
Expand All @@ -195,7 +204,7 @@ func (p *Provider) Debug(debug bool) {}

// BeginAuth asks the OpenID Connect provider for an authentication end-point.
func (p *Provider) BeginAuth(state string) (goth.Session, error) {
url := p.config.AuthCodeURL(state)
url := p.config.AuthCodeURL(state, p.authCodeOptions...)
session := &Session{
AuthURL: url,
}
Expand Down
18 changes: 18 additions & 0 deletions providers/openidConnect/openidConnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ func Test_BeginAuth(t *testing.T) {
a.Contains(s.AuthURL, "scope=openid")
}

func Test_BeginAuth_AuthCodeOptions(t *testing.T) {
t.Parallel()
a := assert.New(t)

provider := openidConnectProvider()
provider.SetAuthCodeOptions(map[string]string{"domain_hint": "test_domain.com", "prompt": "none"})
session, err := provider.BeginAuth("test_state")
s := session.(*Session)
a.NoError(err)
a.Contains(s.AuthURL, "https://accounts.google.com/o/oauth2/v2/auth")
a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("OPENID_CONNECT_KEY")))
a.Contains(s.AuthURL, "state=test_state")
a.Contains(s.AuthURL, "redirect_uri=http%3A%2F%2Flocalhost%2Ffoo")
a.Contains(s.AuthURL, "scope=openid")
a.Contains(s.AuthURL, "domain_hint=test_domain.com")
a.Contains(s.AuthURL, "prompt=none")
}

func Test_Implements_Provider(t *testing.T) {
t.Parallel()
a := assert.New(t)
Expand Down
Loading