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 access-token in proxy response headers #109

Merged
merged 5 commits into from
Nov 16, 2018
Merged
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
8 changes: 8 additions & 0 deletions internal/proxy/oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type OAuthProxy struct {
skipAuthPreflight bool
templates *template.Template

PassAccessToken bool

StatsdClient *statsd.Client

mux map[string]*route
Expand Down Expand Up @@ -303,6 +305,7 @@ func NewOAuthProxy(opts *Options, optFuncs ...func(*OAuthProxy) error) (*OAuthPr
redirectURL: &url.URL{Path: "/oauth2/callback"},
skipAuthPreflight: opts.SkipAuthPreflight,
templates: getTemplates(),
PassAccessToken: opts.PassAccessToken,
}

for _, optFunc := range optFuncs {
Expand Down Expand Up @@ -1037,6 +1040,11 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) (er
}

req.Header.Set("X-Forwarded-User", session.User)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add some test in oauthproxy_test.go to test that the right header is being set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done!

if p.PassAccessToken && session.AccessToken != "" {
req.Header.Set("X-Forwarded-Access-Token", session.AccessToken)
}

req.Header.Set("X-Forwarded-Email", session.Email)
req.Header.Set("X-Forwarded-Groups", strings.Join(session.Groups, ","))

Expand Down
11 changes: 7 additions & 4 deletions internal/proxy/oauthproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ func TestHeadersSentToUpstreams(t *testing.T) {
opts.ClientSecret = "foobar"
opts.CookieSecret = testEncodedCookieSecret
opts.CookieSecure = false
opts.PassAccessToken = true
opts.upstreamConfigs = generateTestUpstreamConfigs(upstream.URL)
opts.Validate()
providerURL, _ := url.Parse("http://sso-auth.example.com/")
Expand All @@ -1007,6 +1008,7 @@ func TestHeadersSentToUpstreams(t *testing.T) {
state := testSession()
state.Email = "foo@example.com"
state.User = "foo"
state.AccessToken = "SupErSensItiveAccesSToken"
state.Groups = []string{"fooGroup"}
proxy, _ := NewOAuthProxy(opts, testValidatorFunc(true))

Expand Down Expand Up @@ -1041,10 +1043,11 @@ func TestHeadersSentToUpstreams(t *testing.T) {
}

expectedHeaders := map[string]string{
"X-Forwarded-Email": "foo@example.com",
"X-Forwarded-User": "foo",
"X-Forwarded-Groups": "fooGroup",
"Cookie": tc.expectedCookieHeader,
"X-Forwarded-Email": "foo@example.com",
"X-Forwarded-User": "foo",
"X-Forwarded-Groups": "fooGroup",
"X-Forwarded-Access-Token": "SupErSensItiveAccesSToken",
"Cookie": tc.expectedCookieHeader,
}

for key, val := range expectedHeaders {
Expand Down
4 changes: 4 additions & 0 deletions internal/proxy/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
// CookieExpire - expire timeframe for cookie
// CookieSecure - set secure (HTTPS) cookie flag
// CookieHTTPOnly - set HttpOnly cookie flag
// PassAccessToken - send access token in the http headers
// Provider - OAuth provider
// Scope - OAuth scope specification
// SessionLifetimeTTL - time to live for a session lifetime
Expand Down Expand Up @@ -71,6 +72,8 @@ type Options struct {
CookieSecure bool `envconfig:"COOKIE_SECURE" default:"true"`
CookieHTTPOnly bool `envconfig:"COOKIE_HTTP_ONLY"`

PassAccessToken bool `envconfig:"PASS_ACCESS_TOKEN" default:"false"`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a simple unit test here for the new config value in options_test.go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And done as well!


// These options allow for other providers besides Google, with potential overrides.
Provider string `envconfig:"PROVIDER" default:"google"`
Scope string `envconfig:"SCOPE"`
Expand Down Expand Up @@ -106,6 +109,7 @@ func NewOptions() *Options {
SkipAuthPreflight: false,
RequestLogging: true,
DefaultUpstreamTimeout: time.Duration(1) * time.Second,
PassAccessToken: false,
}
}

Expand Down
8 changes: 8 additions & 0 deletions internal/proxy/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,11 @@ func TestValidateCookieBadName(t *testing.T) {
testutil.Equal(t, err.Error(), "Invalid configuration:\n"+
fmt.Sprintf(" invalid cookie name: %q", o.CookieName))
}

func TestPassAccessToken(t *testing.T) {
o := testOptions()
testutil.Equal(t, false, o.PassAccessToken)
o.PassAccessToken = true
testutil.Equal(t, nil, o.Validate())
testutil.Equal(t, true, o.PassAccessToken)
}