8 middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
9 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
10 sessionsapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
11 . "github.com/onsi/ginkgo"
12 . "github.com/onsi/ginkgo/extensions/table"
13 . "github.com/onsi/gomega"
16var _ = Describe("Headers Suite", func() {
17 type headersTableInput struct {
18 headers []options.Header
19 initialHeaders http.Header
20 session *sessionsapi.SessionState
21 expectedHeaders http.Header
25 DescribeTable("the request header injector",
26 func(in headersTableInput) {
27 scope := &middlewareapi.RequestScope{
31 // Set up the request with a request scope
32 req := httptest.NewRequest("", "/", nil)
33 req = middlewareapi.AddRequestScope(req, scope)
34 req.Header = in.initialHeaders.Clone()
36 rw := httptest.NewRecorder()
38 // Create the handler with a next handler that will capture the headers
40 var gotHeaders http.Header
41 injector, err := NewRequestHeaderInjector(in.headers)
42 if in.expectedErr != "" {
43 Expect(err).To(MatchError(in.expectedErr))
46 Expect(err).ToNot(HaveOccurred())
48 handler := injector(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
49 gotHeaders = r.Header.Clone()
51 handler.ServeHTTP(rw, req)
53 Expect(gotHeaders).To(Equal(in.expectedHeaders))
55 Entry("with no configured headers", headersTableInput{
56 headers: []options.Header{},
57 initialHeaders: http.Header{
58 "Foo": []string{"bar", "baz"},
60 session: &sessionsapi.SessionState{},
61 expectedHeaders: http.Header{
62 "Foo": []string{"bar,baz"},
66 Entry("with a claim valued header", headersTableInput{
67 headers: []options.Header{
70 Values: []options.HeaderValue{
72 ClaimSource: &options.ClaimSource{
79 initialHeaders: http.Header{
80 "Foo": []string{"bar", "baz"},
82 session: &sessionsapi.SessionState{
83 IDToken: "IDToken-1234",
85 expectedHeaders: http.Header{
86 "Foo": []string{"bar,baz"},
87 "Claim": []string{"IDToken-1234"},
91 Entry("with a claim valued header (without preservation)", headersTableInput{
92 headers: []options.Header{
95 Values: []options.HeaderValue{
97 ClaimSource: &options.ClaimSource{
104 initialHeaders: http.Header{
105 "Claim": []string{"bar", "baz"},
107 session: &sessionsapi.SessionState{
108 IDToken: "IDToken-1234",
110 expectedHeaders: http.Header{
111 "Claim": []string{"IDToken-1234"},
115 Entry("with a claim valued header (with preservation)", headersTableInput{
116 headers: []options.Header{
119 PreserveRequestValue: true,
120 Values: []options.HeaderValue{
122 ClaimSource: &options.ClaimSource{
129 initialHeaders: http.Header{
130 "Claim": []string{"bar", "baz"},
132 session: &sessionsapi.SessionState{
133 IDToken: "IDToken-1234",
135 expectedHeaders: http.Header{
136 "Claim": []string{"bar,baz,IDToken-1234"},
140 Entry("with a claim valued header that's not present (without preservation)", headersTableInput{
141 headers: []options.Header{
144 Values: []options.HeaderValue{
146 ClaimSource: &options.ClaimSource{
153 initialHeaders: http.Header{
154 "Claim": []string{"bar", "baz"},
157 expectedHeaders: http.Header{},
160 Entry("with a claim valued header that's not present (with preservation)", headersTableInput{
161 headers: []options.Header{
164 PreserveRequestValue: true,
165 Values: []options.HeaderValue{
167 ClaimSource: &options.ClaimSource{
174 initialHeaders: http.Header{
175 "Claim": []string{"bar", "baz"},
178 expectedHeaders: http.Header{
179 "Claim": []string{"bar,baz"},
183 Entry("with an invalid basicAuthPassword claim valued header", headersTableInput{
184 headers: []options.Header{
186 Name: "X-Auth-Request-Authorization",
187 Values: []options.HeaderValue{
189 ClaimSource: &options.ClaimSource{
191 BasicAuthPassword: &options.SecretSource{
192 Value: []byte(base64.StdEncoding.EncodeToString([]byte("basic-password"))),
193 FromEnv: "SECRET_ENV",
200 initialHeaders: http.Header{
201 "foo": []string{"bar", "baz"},
203 session: &sessionsapi.SessionState{
206 expectedHeaders: nil,
207 expectedErr: "error building request header injector: error building request injector: error building injector for header \"X-Auth-Request-Authorization\": error loading basicAuthPassword: secret source is invalid: exactly one entry required, specify either value, fromEnv or fromFile",
211 DescribeTable("the response header injector",
212 func(in headersTableInput) {
213 scope := &middlewareapi.RequestScope{
217 // Set up the request with a request scope
218 req := httptest.NewRequest("", "/", nil)
219 req = middlewareapi.AddRequestScope(req, scope)
221 rw := httptest.NewRecorder()
222 for key, values := range in.initialHeaders {
223 for _, value := range values {
224 rw.Header().Add(key, value)
228 // Create the handler with a next handler that will capture the headers
230 var gotHeaders http.Header
231 injector, err := NewResponseHeaderInjector(in.headers)
232 if in.expectedErr != "" {
233 Expect(err).To(MatchError(in.expectedErr))
236 Expect(err).ToNot(HaveOccurred())
238 handler := injector(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
239 gotHeaders = w.Header().Clone()
241 handler.ServeHTTP(rw, req)
243 Expect(gotHeaders).To(Equal(in.expectedHeaders))
245 Entry("with no configured headers", headersTableInput{
246 headers: []options.Header{},
247 initialHeaders: http.Header{
248 "Foo": []string{"bar", "baz"},
250 session: &sessionsapi.SessionState{},
251 expectedHeaders: http.Header{
252 "Foo": []string{"bar,baz"},
257 Entry("with flattenHeaders (set-cookie and any other)", headersTableInput{
258 headers: []options.Header{
261 Values: []options.HeaderValue{
263 SecretSource: &options.SecretSource{
264 Value: []byte("_oauth2_proxy=ey123123123"),
271 Values: []options.HeaderValue{
273 SecretSource: &options.SecretSource{
274 Value: []byte("oauth_user"),
280 initialHeaders: http.Header{
281 "Set-Cookie": []string{"cookie1=value1", "cookie2=value2"},
282 "X-Auth-User": []string{"oauth_user_1"},
285 expectedHeaders: http.Header{
286 "Set-Cookie": []string{"cookie1=value1", "cookie2=value2", "_oauth2_proxy=ey123123123"},
287 "X-Auth-User": []string{"oauth_user_1,oauth_user"},
292 Entry("with a claim valued header", headersTableInput{
293 headers: []options.Header{
296 Values: []options.HeaderValue{
298 ClaimSource: &options.ClaimSource{
305 initialHeaders: http.Header{
306 "Foo": []string{"bar", "baz"},
308 session: &sessionsapi.SessionState{
309 IDToken: "IDToken-1234",
311 expectedHeaders: http.Header{
312 "Foo": []string{"bar,baz"},
313 "Claim": []string{"IDToken-1234"},
317 Entry("with a claim valued header (without preservation)", headersTableInput{
318 headers: []options.Header{
321 Values: []options.HeaderValue{
323 ClaimSource: &options.ClaimSource{
330 initialHeaders: http.Header{
331 "Claim": []string{"bar", "baz"},
333 session: &sessionsapi.SessionState{
334 IDToken: "IDToken-1234",
336 expectedHeaders: http.Header{
337 "Claim": []string{"bar,baz,IDToken-1234"},
341 Entry("with a claim valued header (with preservation)", headersTableInput{
342 headers: []options.Header{
345 PreserveRequestValue: true,
346 Values: []options.HeaderValue{
348 ClaimSource: &options.ClaimSource{
355 initialHeaders: http.Header{
356 "Claim": []string{"bar", "baz"},
358 session: &sessionsapi.SessionState{
359 IDToken: "IDToken-1234",
361 expectedHeaders: http.Header{
362 "Claim": []string{"bar,baz,IDToken-1234"},
366 Entry("with a claim valued header that's not present (without preservation)", headersTableInput{
367 headers: []options.Header{
370 Values: []options.HeaderValue{
372 ClaimSource: &options.ClaimSource{
379 initialHeaders: http.Header{
380 "Claim": []string{"bar", "baz"},
383 expectedHeaders: http.Header{
384 "Claim": []string{"bar,baz"},
388 Entry("with a claim valued header that's not present (with preservation)", headersTableInput{
389 headers: []options.Header{
392 PreserveRequestValue: true,
393 Values: []options.HeaderValue{
395 ClaimSource: &options.ClaimSource{
402 initialHeaders: http.Header{
403 "Claim": []string{"bar", "baz"},
406 expectedHeaders: http.Header{
407 "Claim": []string{"bar,baz"},
411 Entry("with an invalid basicAuthPassword claim valued header", headersTableInput{
412 headers: []options.Header{
414 Name: "X-Auth-Request-Authorization",
415 Values: []options.HeaderValue{
417 ClaimSource: &options.ClaimSource{
419 BasicAuthPassword: &options.SecretSource{
420 Value: []byte(base64.StdEncoding.EncodeToString([]byte("basic-password"))),
421 FromEnv: "SECRET_ENV",
428 initialHeaders: http.Header{
429 "foo": []string{"bar", "baz"},
431 session: &sessionsapi.SessionState{
434 expectedHeaders: nil,
435 expectedErr: "error building response header injector: error building response injector: error building injector for header \"X-Auth-Request-Authorization\": error loading basicAuthPassword: secret source is invalid: exactly one entry required, specify either value, fromEnv or fromFile",
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list