@@ -38,6 +38,7 @@ func TestAuthenticatorBearerToken(t *testing.T) {
38
38
t .Run ("method=authenticate" , func (t * testing.T ) {
39
39
for k , tc := range []struct {
40
40
d string
41
+ token string
41
42
r * http.Request
42
43
setup func (* testing.T , * httprouter.Router )
43
44
router func (http.ResponseWriter , * http.Request )
@@ -96,6 +97,54 @@ func TestAuthenticatorBearerToken(t *testing.T) {
96
97
Extra : map [string ]interface {}{"foo" : "bar" },
97
98
},
98
99
},
100
+ {
101
+ d : "should pass because session token was provided in the correct custom header" ,
102
+ token : "custom-header-token-value" ,
103
+ r : & http.Request {Header : http.Header {"X-Custom-Header" : {"custom-header-token-value" }}, URL : & url.URL {Path : "" }},
104
+ router : func (w http.ResponseWriter , r * http.Request ) {
105
+ assert .Equal (t , r .Header .Get ("Authorization" ), "bearer custom-header-token-value" )
106
+ w .WriteHeader (200 )
107
+ w .Write ([]byte (`{"sub": "123", "extra": {"foo": "bar"}}` ))
108
+ },
109
+ config : []byte (`{"token_from": {"header": "X-Custom-Header"}}` ),
110
+ expectErr : false ,
111
+ expectSess : & AuthenticationSession {
112
+ Subject : "123" ,
113
+ Extra : map [string ]interface {}{"foo" : "bar" },
114
+ },
115
+ },
116
+ {
117
+ d : "should pass because session token was provided in the correct custom query parameter" ,
118
+ token : "query-param-token-value" ,
119
+ r : & http.Request {Header : http.Header {}, URL : & url.URL {Path : "" , RawQuery : "custom-query-param=query-param-token-value" }},
120
+ router : func (w http.ResponseWriter , r * http.Request ) {
121
+ assert .Equal (t , r .Header .Get ("Authorization" ), "bearer query-param-token-value" )
122
+ w .WriteHeader (200 )
123
+ w .Write ([]byte (`{"sub": "123", "extra": {"foo": "bar"}}` ))
124
+ },
125
+ config : []byte (`{"token_from": {"query_parameter": "custom-query-param"}}` ),
126
+ expectErr : false ,
127
+ expectSess : & AuthenticationSession {
128
+ Subject : "123" ,
129
+ Extra : map [string ]interface {}{"foo" : "bar" },
130
+ },
131
+ },
132
+ {
133
+ d : "should pass because session token was provided in the correct cookie" ,
134
+ token : "cooke-token-value" ,
135
+ r : & http.Request {Header : http.Header {"Cookie" : {"custom-cookie-name=cooke-token-value" }}, URL : & url.URL {Path : "" }},
136
+ router : func (w http.ResponseWriter , r * http.Request ) {
137
+ assert .Equal (t , r .Header .Get ("Authorization" ), "bearer cooke-token-value" )
138
+ w .WriteHeader (200 )
139
+ w .Write ([]byte (`{"sub": "123", "extra": {"foo": "bar"}}` ))
140
+ },
141
+ config : []byte (`{"token_from": {"cookie": "custom-cookie-name"}}` ),
142
+ expectErr : false ,
143
+ expectSess : & AuthenticationSession {
144
+ Subject : "123" ,
145
+ Extra : map [string ]interface {}{"foo" : "bar" },
146
+ },
147
+ },
99
148
{
100
149
d : "should pass through method, path, and headers to auth server; should NOT pass through query parameters by default for backwards compatibility" ,
101
150
r : & http.Request {Header : http.Header {"Authorization" : {"bearer zyx" }}, URL : & url.URL {Path : "/users/123" , RawQuery : "query=string" }, Method : "PUT" },
@@ -308,9 +357,12 @@ func TestAuthenticatorBearerToken(t *testing.T) {
308
357
309
358
tc .config , _ = sjson .SetBytes (tc .config , "check_session_url" , testCheckSessionUrl .String ())
310
359
sess := new (AuthenticationSession )
311
- originalHeaders := http.Header {}
360
+ expectedHeaders := http.Header {}
312
361
for k , v := range tc .r .Header {
313
- originalHeaders [k ] = v
362
+ expectedHeaders [k ] = v
363
+ }
364
+ if tc .token != "" {
365
+ expectedHeaders .Set ("Authorization" , "bearer " + tc .token )
314
366
}
315
367
316
368
err = pipelineAuthenticator .Authenticate (tc .r , sess , tc .config , nil )
@@ -323,7 +375,7 @@ func TestAuthenticatorBearerToken(t *testing.T) {
323
375
require .NoError (t , err )
324
376
}
325
377
326
- require .True (t , reflect .DeepEqual (tc .r .Header , originalHeaders ))
378
+ require .True (t , reflect .DeepEqual (tc .r .Header , expectedHeaders ))
327
379
328
380
if tc .expectSess != nil {
329
381
assert .Equal (t , tc .expectSess , sess )
0 commit comments