Skip to content

Commit ca5e7d2

Browse files
committed
Ensure Authorization header is set for bearer_token authenticator
1 parent acb2584 commit ca5e7d2

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

middleware/grpc_middleware_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func testClient(t *testing.T, l *bufconn.Listener, dialOpts ...grpc.DialOption)
5050
func testTokenCheckServer(t *testing.T) *httptest.Server {
5151
s := httptest.NewServer(http.HandlerFunc(
5252
func(w http.ResponseWriter, r *http.Request) {
53-
if r.Header.Get("authorization") != "Bearer correct token" {
53+
if r.Header.Get("Authorization") != "bearer correct token" {
5454
t.Logf("denied request %+v", r)
5555
w.WriteHeader(http.StatusForbidden)
5656
return
@@ -77,7 +77,7 @@ func writeTestConfig(t *testing.T, pattern string, content string) string {
7777
type testToken string
7878

7979
func (t testToken) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
80-
return map[string]string{"authorization": "Bearer " + string(t)}, nil
80+
return map[string]string{"Authorization": "bearer " + string(t)}, nil
8181
}
8282
func (t testToken) RequireTransportSecurity() bool { return false }
8383

pipeline/authn/authenticator_bearer_token.go

+5
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ func (a *AuthenticatorBearerToken) Authenticate(r *http.Request, session *Authen
141141
return errors.WithStack(ErrAuthenticatorNotResponsible)
142142
}
143143

144+
if r.Header == nil {
145+
r.Header = make(http.Header)
146+
}
147+
r.Header.Set("Authorization", "bearer "+token)
148+
144149
body, err := forwardRequestToSessionStore(a.client, r, cf)
145150
if err != nil {
146151
return err

pipeline/authn/authenticator_bearer_token_test.go

+55-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestAuthenticatorBearerToken(t *testing.T) {
3838
t.Run("method=authenticate", func(t *testing.T) {
3939
for k, tc := range []struct {
4040
d string
41+
token string
4142
r *http.Request
4243
setup func(*testing.T, *httprouter.Router)
4344
router func(http.ResponseWriter, *http.Request)
@@ -96,6 +97,54 @@ func TestAuthenticatorBearerToken(t *testing.T) {
9697
Extra: map[string]interface{}{"foo": "bar"},
9798
},
9899
},
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+
},
99148
{
100149
d: "should pass through method, path, and headers to auth server; should NOT pass through query parameters by default for backwards compatibility",
101150
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) {
308357

309358
tc.config, _ = sjson.SetBytes(tc.config, "check_session_url", testCheckSessionUrl.String())
310359
sess := new(AuthenticationSession)
311-
originalHeaders := http.Header{}
360+
expectedHeaders := http.Header{}
312361
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)
314366
}
315367

316368
err = pipelineAuthenticator.Authenticate(tc.r, sess, tc.config, nil)
@@ -323,7 +375,7 @@ func TestAuthenticatorBearerToken(t *testing.T) {
323375
require.NoError(t, err)
324376
}
325377

326-
require.True(t, reflect.DeepEqual(tc.r.Header, originalHeaders))
378+
require.True(t, reflect.DeepEqual(tc.r.Header, expectedHeaders))
327379

328380
if tc.expectSess != nil {
329381
assert.Equal(t, tc.expectSess, sess)

0 commit comments

Comments
 (0)