Skip to content

Commit

Permalink
Add support for resource indicator to OIDC provider (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomseddon committed Aug 22, 2020
1 parent db03097 commit 5ae3bb7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ OIDC Provider:
--providers.oidc.issuer-url= Issuer URL [$PROVIDERS_OIDC_ISSUER_URL]
--providers.oidc.client-id= Client ID [$PROVIDERS_OIDC_CLIENT_ID]
--providers.oidc.client-secret= Client Secret [$PROVIDERS_OIDC_CLIENT_SECRET]
--providers.oidc.resource= Optional resource indicator [$PROVIDERS_OIDC_RESOURCE]
Help Options:
-h, --help Show this help message
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

// OIDC provider
type OIDC struct {
OAuthProvider

IssuerURL string `long:"issuer-url" env:"ISSUER_URL" description:"Issuer URL"`
ClientID string `long:"client-id" env:"CLIENT_ID" description:"Client ID"`
ClientSecret string `long:"client-secret" env:"CLIENT_SECRET" description:"Client Secret" json:"-"`

OAuthProvider

provider *oidc.Provider
verifier *oidc.IDTokenVerifier
}
Expand Down
27 changes: 27 additions & 0 deletions internal/provider/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ func TestOIDCGetLoginURL(t *testing.T) {

// Calling the method should not modify the underlying config
assert.Equal("", provider.Config.RedirectURL)

//
// Test with resource config option
//
provider.Resource = "resourcetest"

// Check url
uri, err = url.Parse(provider.GetLoginURL("http://example.com/_oauth", "state"))
assert.Nil(err)
assert.Equal(serverURL.Scheme, uri.Scheme)
assert.Equal(serverURL.Host, uri.Host)
assert.Equal("/auth", uri.Path)

// Check query string
qs = uri.Query()
expectedQs = url.Values{
"client_id": []string{"idtest"},
"redirect_uri": []string{"http://example.com/_oauth"},
"response_type": []string{"code"},
"scope": []string{"openid profile email"},
"state": []string{"state"},
"resource": []string{"resourcetest"},
}
assert.Equal(expectedQs, qs)

// Calling the method should not modify the underlying config
assert.Equal("", provider.Config.RedirectURL)
}

func TestOIDCExchangeCode(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type User struct {

// OAuthProvider is a provider using the oauth2 library
type OAuthProvider struct {
Resource string `long:"resource" env:"RESOURCE" description:"Optional resource indicator"`

Config *oauth2.Config
ctx context.Context
}
Expand All @@ -51,6 +53,11 @@ func (p *OAuthProvider) ConfigCopy(redirectURI string) oauth2.Config {
// OAuthGetLoginURL provides a base "GetLoginURL" for proiders using OAauth2
func (p *OAuthProvider) OAuthGetLoginURL(redirectURI, state string) string {
config := p.ConfigCopy(redirectURI)

if p.Resource != "" {
return config.AuthCodeURL(state, oauth2.SetAuthURLParam("resource", p.Resource))
}

return config.AuthCodeURL(state)
}

Expand Down

0 comments on commit 5ae3bb7

Please sign in to comment.