git.iko.soy/typeable/oauth2-proxy
on master help sign in
Use comma separated multiple values for header (#799)

* Use comma separated value for multiple claims * Fix lint error * Fix more tests * Fix one more test * Always flatten the headers * Ensure we test the real multi-groups * Only update map when necessary * Update CHANGELOG * Move to the right location of change log * Fix blank line

commitb541805dc1a7ab0dceba9959a31e4c7a06c7c6cf authorLida Li <lilida@lilida.org> dateFri 22 Jan 2021, 00:48 (6 years ago) parent8087de7
4 files changed+19−7
CHANGELOG.md+1 oauthproxy_test.go+1−1 pkg/middleware/headers.go+11 pkg/middleware/headers_test.go+6−6
CHANGELOG.md+1
@@ -67,6 +67,7 @@
6767
6868## Changes since v6.1.1
6969
70+- [#799](https://github.com/oauth2-proxy/oauth2-proxy/pull/799) Use comma separated multiple values for header (@lilida)
7071- [#903](https://github.com/oauth2-proxy/oauth2-proxy/pull/903) Add docs and generated reference for Alpha configuration (@JoelSpeed)
7172- [#995](https://github.com/oauth2-proxy/oauth2-proxy/pull/995) Add Security Policy (@JoelSpeed)
7273- [#964](https://github.com/oauth2-proxy/oauth2-proxy/pull/964) Require `--reverse-proxy` true to trust `X-Forwareded-*` type headers (@NickMeves)
oauthproxy_test.go+1−1
@@ -612,7 +612,7 @@ func TestPassGroupsHeadersWithGroups(t *testing.T) {
612612 rw = httptest.NewRecorder()
613613 proxy.ServeHTTP(rw, req)
614614
615− assert.Equal(t, groups, req.Header["X-Forwarded-Groups"])
615+ assert.Equal(t, []string{"a,b"}, req.Header["X-Forwarded-Groups"])
616616}
617617
618618type PassAccessTokenTest struct {
pkg/middleware/headers.go+11
@@ -3,6 +3,7 @@ package middleware
33import (
44 "fmt"
55 "net/http"
6+ "strings"
67
78 "github.com/justinas/alice"
89 middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
@@ -40,6 +41,14 @@ func newStripHeaders(headers []options.Header) alice.Constructor {
4041 }
4142}
4243
44+func flattenHeaders(headers http.Header) {
45+ for name, values := range headers {
46+ if len(values) > 1 {
47+ headers.Set(name, strings.Join(values, ","))
48+ }
49+ }
50+}
51+
4352func stripHeaders(headers []string, next http.Handler) http.Handler {
4453 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
4554 for _, header := range headers {
@@ -67,6 +76,7 @@ func injectRequestHeaders(injector header.Injector, next http.Handler) http.Hand
6776 // If scope is nil, this will panic.
6877 // A scope should always be injected before this handler is called.
6978 injector.Inject(req.Header, scope.Session)
79+ flattenHeaders(req.Header)
7080 next.ServeHTTP(rw, req)
7181 })
7282}
@@ -98,6 +108,7 @@ func injectResponseHeaders(injector header.Injector, next http.Handler) http.Han
98108 // If scope is nil, this will panic.
99109 // A scope should always be injected before this handler is called.
100110 injector.Inject(rw.Header(), scope.Session)
111+ flattenHeaders(req.Header)
101112 next.ServeHTTP(rw, req)
102113 })
103114}
pkg/middleware/headers_test.go+6−6
@@ -55,11 +55,11 @@ var _ = Describe("Headers Suite", func() {
5555 Entry("with no configured headers", headersTableInput{
5656 headers: []options.Header{},
5757 initialHeaders: http.Header{
58− "foo": []string{"bar", "baz"},
58+ "Foo": []string{"bar", "baz"},
5959 },
6060 session: &sessionsapi.SessionState{},
6161 expectedHeaders: http.Header{
62− "foo": []string{"bar", "baz"},
62+ "Foo": []string{"bar,baz"},
6363 },
6464 expectedErr: "",
6565 }),
@@ -77,13 +77,13 @@ var _ = Describe("Headers Suite", func() {
7777 },
7878 },
7979 initialHeaders: http.Header{
80− "foo": []string{"bar", "baz"},
80+ "Foo": []string{"bar", "baz"},
8181 },
8282 session: &sessionsapi.SessionState{
8383 IDToken: "IDToken-1234",
8484 },
8585 expectedHeaders: http.Header{
86− "foo": []string{"bar", "baz"},
86+ "Foo": []string{"bar,baz"},
8787 "Claim": []string{"IDToken-1234"},
8888 },
8989 expectedErr: "",
@@ -133,7 +133,7 @@ var _ = Describe("Headers Suite", func() {
133133 IDToken: "IDToken-1234",
134134 },
135135 expectedHeaders: http.Header{
136− "Claim": []string{"bar", "baz", "IDToken-1234"},
136+ "Claim": []string{"bar,baz,IDToken-1234"},
137137 },
138138 expectedErr: "",
139139 }),
@@ -176,7 +176,7 @@ var _ = Describe("Headers Suite", func() {
176176 },
177177 session: nil,
178178 expectedHeaders: http.Header{
179− "Claim": []string{"bar", "baz"},
179+ "Claim": []string{"bar,baz"},
180180 },
181181 expectedErr: "",
182182 }),