Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce auth scheme and jumping to next authentication #982

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: introduce auth scheme and jumping to next authentication
Marlinc authored and aeneasr committed Sep 10, 2022
commit 64e332f0bedfaeca54a61c470b6a7496df0f2d5d
15 changes: 15 additions & 0 deletions .schema/config.schema.json
Original file line number Diff line number Diff line change
@@ -456,6 +456,11 @@
"title": "Header",
"type": "string",
"description": "The header (case insensitive) that must contain a token for request authentication.\n It can't be set along with query_parameter or cookie."
},
"auth_scheme": {
"title": "Auth scheme",
"type": "string",
"description": "The auth scheme to accept in case the header is set to Authorization, this is by default set to Bearer."
}
}
},
@@ -619,6 +624,11 @@
"title": "Header",
"type": "string",
"description": "The header (case insensitive) that must contain a token for request authentication.\n It can't be set along with query_parameter or cookie."
},
"auth_scheme": {
"title": "Auth scheme",
"type": "string",
"description": "The auth scheme to accept in case the header is set to Authorization, this is by default set to Bearer."
}
}
},
@@ -842,6 +852,11 @@
"title": "Header",
"type": "string",
"description": "The header (case insensitive) that must contain a token for request authentication.\n It can't be set along with query_parameter or cookie."
},
"auth_scheme": {
"title": "Auth scheme",
"type": "string",
"description": "The auth scheme to accept in case the header is set to Authorization, this is by default set to Bearer."
}
}
},
17 changes: 13 additions & 4 deletions helper/bearer.go
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@ const (

type BearerTokenLocation struct {
Header *string `json:"header"`
AuthScheme *string `json:"auth_scheme"`
QueryParameter *string `json:"query_parameter"`
Cookie *string `json:"cookie"`
}
@@ -39,7 +40,11 @@ func BearerTokenFromRequest(r *http.Request, tokenLocation *BearerTokenLocation)
if tokenLocation != nil {
if tokenLocation.Header != nil {
if *tokenLocation.Header == defaultAuthorizationHeader {
return DefaultBearerTokenFromRequest(r)
authScheme := "Bearer"
if tokenLocation.AuthScheme != nil {
authScheme = *tokenLocation.AuthScheme
}
return DefaultBearerTokenFromRequest(r, authScheme)
}
return r.Header.Get(*tokenLocation.Header)
} else if tokenLocation.QueryParameter != nil {
@@ -53,13 +58,17 @@ func BearerTokenFromRequest(r *http.Request, tokenLocation *BearerTokenLocation)
}
}

return DefaultBearerTokenFromRequest(r)
return DefaultBearerTokenFromRequest(r, "Bearer")
}

func DefaultBearerTokenFromRequest(r *http.Request) string {
func DefaultBearerTokenFromRequest(r *http.Request, authScheme string) string {
token := r.Header.Get(defaultAuthorizationHeader)
if authScheme == "" {
return token
}

split := strings.SplitN(token, " ", 2)
if len(split) != 2 || !strings.EqualFold(strings.ToLower(split[0]), "bearer") {
if len(split) != 2 || !strings.EqualFold(strings.ToLower(split[0]), strings.ToLower(authScheme)) {
return ""
}
return split[1]
18 changes: 18 additions & 0 deletions helper/bearer_test.go
Original file line number Diff line number Diff line change
@@ -74,4 +74,22 @@ func TestBearerTokenFromRequest(t *testing.T) {
token := helper.BearerTokenFromRequest(request, &tokenLocation)
assert.Equal(t, expectedToken, token)
})
t.Run("case=token should be received from authorization header with custom auth scheme if custom location is set to header and token is present", func(t *testing.T) {
expectedToken := "token"
customHeaderName := "Authorization"
customAuthScheme := "AccessToken"
request := &http.Request{Header: http.Header{customHeaderName: {customAuthScheme + " " + expectedToken}}}
tokenLocation := helper.BearerTokenLocation{Header: &customHeaderName, AuthScheme: &customAuthScheme}
token := helper.BearerTokenFromRequest(request, &tokenLocation)
assert.Equal(t, expectedToken, token)
})
t.Run("case=token should be received from authorization header with an empty custom auth scheme if custom location is set to header and token is present", func(t *testing.T) {
expectedToken := "token"
customHeaderName := "Authorization"
customAuthScheme := ""
request := &http.Request{Header: http.Header{customHeaderName: {expectedToken}}}
tokenLocation := helper.BearerTokenLocation{Header: &customHeaderName, AuthScheme: &customAuthScheme}
token := helper.BearerTokenFromRequest(request, &tokenLocation)
assert.Equal(t, expectedToken, token)
})
}
27 changes: 27 additions & 0 deletions pipeline/authn/authenticator_bearer_token_test.go
Original file line number Diff line number Diff line change
@@ -60,6 +60,17 @@ func TestAuthenticatorBearerToken(t *testing.T) {
expectErr: true,
expectExactErr: ErrAuthenticatorNotResponsible,
},
{
d: "should return error saying that authenticator is not responsible for validating the request, as the session store returns HTTP 406 Not Acceptable",
r: &http.Request{Header: http.Header{"Authorization": {"bearer token"}}, URL: &url.URL{Path: ""}},
setup: func(t *testing.T, m *httprouter.Router) {
m.GET("/", func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
w.WriteHeader(406)
})
},
expectErr: true,
expectExactErr: ErrAuthenticatorNotResponsible,
},
{
d: "should fail because session store returned 400",
r: &http.Request{Header: http.Header{"Authorization": {"bearer token"}}, URL: &url.URL{Path: ""}},
@@ -85,6 +96,22 @@ func TestAuthenticatorBearerToken(t *testing.T) {
Extra: map[string]interface{}{"foo": "bar"},
},
},
{
d: "should pass because session store returned 200",
r: &http.Request{Header: http.Header{"Authorization": {"AccessToken token"}}, URL: &url.URL{Path: ""}},
config: []byte(`{"token_from": {"header": "Authorization", "auth_scheme": "AccessToken"}}`),
setup: func(t *testing.T, m *httprouter.Router) {
m.GET("/", func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
w.WriteHeader(200)
w.Write([]byte(`{"sub": "123", "extra": {"foo": "bar"}}`))
})
},
expectErr: false,
expectSess: &AuthenticationSession{
Subject: "123",
Extra: map[string]interface{}{"foo": "bar"},
},
},
{
d: "should pass through method, path, and headers to auth server; should NOT pass through query parameters by default for backwards compatibility",
r: &http.Request{Header: http.Header{"Authorization": {"bearer zyx"}}, URL: &url.URL{Path: "/users/123", RawQuery: "query=string"}, Method: "PUT"},
5 changes: 5 additions & 0 deletions pipeline/authn/authenticator_cookie_session.go
Original file line number Diff line number Diff line change
@@ -177,6 +177,11 @@ func forwardRequestToSessionStore(r *http.Request, cf AuthenticatorForwardConfig

defer res.Body.Close()

// HTTP 406 Not Acceptable
if res.StatusCode == http.StatusNotAcceptable {
return nil, errors.WithStack(ErrAuthenticatorNotResponsible)
}

if res.StatusCode == http.StatusOK {
body, err := ioutil.ReadAll(res.Body)
if err != nil {