Skip to content

Commit e29ceb4

Browse files
committed
feat: allow multiple cookie domains via env var
1 parent 732b480 commit e29ceb4

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

internal/configuration/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ type Config struct {
3636
ProviderURI string `long:"provider-uri" env:"PROVIDER_URI" description:"OIDC Provider URI"`
3737
ClientID string `long:"client-id" env:"CLIENT_ID" description:"Client ID"`
3838
ClientSecret string `long:"client-secret" env:"CLIENT_SECRET" description:"Client Secret" json:"-"`
39-
Scope string `long:"scope" env:"SCOPE" description:"Define scope"`
39+
Scope []string `long:"scope" env:"SCOPE" env-delim:" " description:"Define scope. Space delimited when used as env var."`
4040
AuthHost string `long:"auth-host" env:"AUTH_HOST" description:"Single host to use when returning from 3rd party auth"`
4141
Config func(s string) error `long:"config" env:"CONFIG" description:"Path to config file" json:"-"`
42-
CookieDomains []util.CookieDomain `long:"cookie-domain" env:"COOKIE_DOMAIN" description:"Domain to set auth cookie on, can be set multiple times"`
42+
CookieDomains []util.CookieDomain `long:"cookie-domain" env:"COOKIE_DOMAIN" env-delim:"," description:"Domain to set auth cookie on, can be set multiple times. Comma delimited when used as env var."`
4343
InsecureCookie bool `long:"insecure-cookie" env:"INSECURE_COOKIE" description:"Use insecure cookies"`
4444
CookieName string `long:"cookie-name" env:"COOKIE_NAME" default:"_forward_auth" description:"ID Cookie Name"`
4545
EmailHeaderNames CommaSeparatedList `long:"email-header-names" env:"EMAIL_HEADER_NAMES" default:"X-Forwarded-User" description:"Response headers containing the authenticated user's username"`

internal/configuration/config_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func TestConfigParseArgs(t *testing.T) {
3838
assert := assert.New(t)
3939
c, err := NewConfig([]string{
4040
"--cookie-name=cookiename",
41+
"--cookie-domain=example.com",
42+
"--cookie-domain=example2.com",
4143
"--csrf-cookie-name", "\"csrfcookiename\"",
4244
"--rule.1.action=allow",
4345
"--rule.1.rule=PathPrefix(`/one`)",
@@ -61,6 +63,12 @@ func TestConfigParseArgs(t *testing.T) {
6163
Rule: "Host(`two.com`) && Path(`/two`)",
6264
},
6365
}, c.Rules)
66+
67+
// Check cookie domain
68+
if assert.Len(c.CookieDomains, 2, "there must be 2 cookie domains") {
69+
assert.Equal("example.com", c.CookieDomains[0].Domain, "first cookie domain should be read from environment")
70+
assert.Equal("example2.com", c.CookieDomains[1].Domain, "second cookie domain should be read from environment")
71+
}
6472
}
6573

6674
func TestConfigParseUnknownFlags(t *testing.T) {
@@ -116,6 +124,10 @@ func TestConfigParseIni(t *testing.T) {
116124
Rule: "Host(`two.com`) && Path(`/two`)",
117125
},
118126
}, c.Rules)
127+
if assert.Len(c.CookieDomains, 2, "there must be 2 cookie domains") {
128+
assert.Equal("example.com", c.CookieDomains[0].Domain, "first cookie domain should be read from environment")
129+
assert.Equal("example2.com", c.CookieDomains[1].Domain, "second cookie domain should be read from environment")
130+
}
119131
}
120132

121133
func TestConfigParseEnvironment(t *testing.T) {
@@ -129,6 +141,31 @@ func TestConfigParseEnvironment(t *testing.T) {
129141
os.Unsetenv("COOKIE_NAME")
130142
}
131143

144+
func TestConfigParseCookieDomainFromEnvironment(t *testing.T) {
145+
assert := assert.New(t)
146+
os.Setenv("COOKIE_DOMAIN", "example.com,example2.com")
147+
c, err := NewConfig([]string{})
148+
assert.Nil(err)
149+
150+
if assert.Len(c.CookieDomains, 2, "there must be 2 cookie domains") {
151+
assert.Equal("example.com", c.CookieDomains[0].Domain, "first cookie domain should be read from environment")
152+
assert.Equal("example2.com", c.CookieDomains[1].Domain, "second cookie domain should be read from environment")
153+
}
154+
155+
os.Unsetenv("COOKIE_DOMAIN")
156+
}
157+
158+
func TestConfigParseScopeFromEnvironment(t *testing.T) {
159+
assert := assert.New(t)
160+
os.Setenv("SCOPE", "openid email")
161+
c, err := NewConfig([]string{})
162+
assert.Nil(err)
163+
164+
assert.Equal([]string{"openid", "email"}, c.Scope, "scope array should be populated")
165+
166+
os.Unsetenv("SCOPE")
167+
}
168+
132169
func TestConfigTransformation(t *testing.T) {
133170
assert := assert.New(t)
134171
c, err := NewConfig([]string{

internal/handlers/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ func (s *Server) AuthCallbackHandler() http.HandlerFunc {
284284

285285
// Mapping scope
286286
var scope []string
287-
if s.config.Scope != "" {
288-
scope = []string{s.config.Scope}
287+
if len(s.config.Scope) > 0 {
288+
scope = s.config.Scope
289289
} else {
290290
scope = []string{oidc.ScopeOpenID, "profile", "email", "groups"}
291291
}
@@ -442,8 +442,8 @@ func (s *Server) authRedirect(logger *logrus.Entry, w http.ResponseWriter, r *ht
442442

443443
// Mapping scope
444444
var scope []string
445-
if s.config.Scope != "" {
446-
scope = []string{s.config.Scope}
445+
if len(s.config.Scope) > 0 {
446+
scope = s.config.Scope
447447
} else {
448448
scope = []string{oidc.ScopeOpenID, "profile", "email", "groups"}
449449
}

test/config0

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ csrf-cookie-name=inicsrfcookiename
33
url-path=one
44
rule.1.action=allow
55
rule.1.rule=PathPrefix(`/one`)
6+
cookie-domain=example.com
7+
cookie-domain=example2.com

0 commit comments

Comments
 (0)