Skip to content

Commit

Permalink
feat: forward original authorization header when using remote (json) …
Browse files Browse the repository at this point in the history
…authorizer (#554)

Closes #528
  • Loading branch information
catper committed Oct 13, 2020
1 parent 408e9f2 commit f4f781e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pipeline/authz/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func (a *AuthorizerRemote) Authorize(r *http.Request, session *authn.Authenticat
return errors.WithStack(err)
}
req.Header.Add("Content-Type", r.Header.Get("Content-Type"))
authz := r.Header.Get("Authorization")
if authz != "" {
req.Header.Add("Authorization", authz)
}

for hdr, templateString := range c.Headers {
var tmpl *template.Template
Expand Down
6 changes: 5 additions & 1 deletion pipeline/authz/remote_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (a *AuthorizerRemoteJSON) GetID() string {
}

// Authorize implements the Authorizer interface.
func (a *AuthorizerRemoteJSON) Authorize(_ *http.Request, session *authn.AuthenticationSession, config json.RawMessage, _ pipeline.Rule) error {
func (a *AuthorizerRemoteJSON) Authorize(r *http.Request, session *authn.AuthenticationSession, config json.RawMessage, _ pipeline.Rule) error {
c, err := a.Config(config)
if err != nil {
return err
Expand Down Expand Up @@ -84,6 +84,10 @@ func (a *AuthorizerRemoteJSON) Authorize(_ *http.Request, session *authn.Authent
return errors.WithStack(err)
}
req.Header.Add("Content-Type", "application/json")
authz := r.Header.Get("Authorization")
if authz != "" {
req.Header.Add("Authorization", authz)
}

res, err := a.client.Do(req)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion pipeline/authz/remote_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func TestAuthorizerRemoteJSONAuthorize(t *testing.T) {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.Header, "Content-Type")
assert.Contains(t, r.Header["Content-Type"], "application/json")
assert.Contains(t, r.Header, "Authorization")
assert.Contains(t, r.Header["Authorization"], "Bearer token")
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
assert.Equal(t, string(body), "{}")
Expand Down Expand Up @@ -139,7 +141,11 @@ func TestAuthorizerRemoteJSONAuthorize(t *testing.T) {

p := configuration.NewViperProvider(logrusx.New("", ""))
a := NewAuthorizerRemoteJSON(p)
if err := a.Authorize(&http.Request{}, tt.session, tt.config, &rule.Rule{}); (err != nil) != tt.wantErr {
if err := a.Authorize(&http.Request{
Header: map[string][]string{
"Authorization": {"Bearer token"},
},
}, tt.session, tt.config, &rule.Rule{}); (err != nil) != tt.wantErr {
t.Errorf("Authorize() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
1 change: 1 addition & 0 deletions pipeline/authz/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func TestAuthorizerRemoteAuthorize(t *testing.T) {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.Header, "Content-Type")
assert.Contains(t, r.Header["Content-Type"], "text/plain")
assert.Nil(t, r.Header["Authorization"])
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
assert.Equal(t, "testtest", string(body))
Expand Down

0 comments on commit f4f781e

Please sign in to comment.