Skip to content

Commit f4f781e

Browse files
authored
feat: forward original authorization header when using remote (json) authorizer (#554)
Closes #528
1 parent 408e9f2 commit f4f781e

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

pipeline/authz/remote.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ func (a *AuthorizerRemote) Authorize(r *http.Request, session *authn.Authenticat
6565
return errors.WithStack(err)
6666
}
6767
req.Header.Add("Content-Type", r.Header.Get("Content-Type"))
68+
authz := r.Header.Get("Authorization")
69+
if authz != "" {
70+
req.Header.Add("Authorization", authz)
71+
}
6872

6973
for hdr, templateString := range c.Headers {
7074
var tmpl *template.Template

pipeline/authz/remote_json.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (a *AuthorizerRemoteJSON) GetID() string {
5353
}
5454

5555
// Authorize implements the Authorizer interface.
56-
func (a *AuthorizerRemoteJSON) Authorize(_ *http.Request, session *authn.AuthenticationSession, config json.RawMessage, _ pipeline.Rule) error {
56+
func (a *AuthorizerRemoteJSON) Authorize(r *http.Request, session *authn.AuthenticationSession, config json.RawMessage, _ pipeline.Rule) error {
5757
c, err := a.Config(config)
5858
if err != nil {
5959
return err
@@ -84,6 +84,10 @@ func (a *AuthorizerRemoteJSON) Authorize(_ *http.Request, session *authn.Authent
8484
return errors.WithStack(err)
8585
}
8686
req.Header.Add("Content-Type", "application/json")
87+
authz := r.Header.Get("Authorization")
88+
if authz != "" {
89+
req.Header.Add("Authorization", authz)
90+
}
8791

8892
res, err := a.client.Do(req)
8993
if err != nil {

pipeline/authz/remote_json_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ func TestAuthorizerRemoteJSONAuthorize(t *testing.T) {
8787
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8888
assert.Contains(t, r.Header, "Content-Type")
8989
assert.Contains(t, r.Header["Content-Type"], "application/json")
90+
assert.Contains(t, r.Header, "Authorization")
91+
assert.Contains(t, r.Header["Authorization"], "Bearer token")
9092
body, err := ioutil.ReadAll(r.Body)
9193
require.NoError(t, err)
9294
assert.Equal(t, string(body), "{}")
@@ -139,7 +141,11 @@ func TestAuthorizerRemoteJSONAuthorize(t *testing.T) {
139141

140142
p := configuration.NewViperProvider(logrusx.New("", ""))
141143
a := NewAuthorizerRemoteJSON(p)
142-
if err := a.Authorize(&http.Request{}, tt.session, tt.config, &rule.Rule{}); (err != nil) != tt.wantErr {
144+
if err := a.Authorize(&http.Request{
145+
Header: map[string][]string{
146+
"Authorization": {"Bearer token"},
147+
},
148+
}, tt.session, tt.config, &rule.Rule{}); (err != nil) != tt.wantErr {
143149
t.Errorf("Authorize() error = %v, wantErr %v", err, tt.wantErr)
144150
}
145151
})

pipeline/authz/remote_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func TestAuthorizerRemoteAuthorize(t *testing.T) {
9292
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9393
assert.Contains(t, r.Header, "Content-Type")
9494
assert.Contains(t, r.Header["Content-Type"], "text/plain")
95+
assert.Nil(t, r.Header["Authorization"])
9596
body, err := ioutil.ReadAll(r.Body)
9697
require.NoError(t, err)
9798
assert.Equal(t, "testtest", string(body))

0 commit comments

Comments
 (0)