-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pipeline/authn: Add token_from config to introspection and jwt (#271)
Add additional optional configuration to jwt and oauth2_introspection authenticators allowing to set from where (which header or query parameter) the token should be received. The configuration is a token_from field in per-rule-configuration, as described in a linked issue. Closes #257
- Loading branch information
Showing
9 changed files
with
322 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package helper_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/ory/oathkeeper/helper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
defaultHeaderName = "Authorization" | ||
) | ||
|
||
func TestBearerTokenFromRequest(t *testing.T) { | ||
t.Run("case=token should be received from default header if custom location is not set and token is present", func(t *testing.T) { | ||
expectedToken := "token" | ||
request := &http.Request{Header: http.Header{defaultHeaderName: {"bearer " + expectedToken}}} | ||
token := helper.BearerTokenFromRequest(request, nil) | ||
assert.Equal(t, expectedToken, token) | ||
}) | ||
t.Run("case=should return empty string if custom location is not set and token is not present in default header", func(t *testing.T) { | ||
request := &http.Request{} | ||
token := helper.BearerTokenFromRequest(request, nil) | ||
assert.Empty(t, token) | ||
}) | ||
t.Run("case=should return empty string if custom location is set to header and token is not present in that header", func(t *testing.T) { | ||
customHeaderName := "Custom-Auth-Header" | ||
request := &http.Request{Header: http.Header{defaultHeaderName: {"bearer token"}}} | ||
tokenLocation := helper.BearerTokenLocation{Header: &customHeaderName} | ||
token := helper.BearerTokenFromRequest(request, &tokenLocation) | ||
assert.Empty(t, token) | ||
}) | ||
t.Run("case=should return empty string if custom location is set to query parameter and token is not present in that query parameter", func(t *testing.T) { | ||
customQueryParameterName := "Custom-Auth" | ||
request := &http.Request{Header: http.Header{defaultHeaderName: {"bearer token"}}} | ||
tokenLocation := helper.BearerTokenLocation{QueryParameter: &customQueryParameterName} | ||
token := helper.BearerTokenFromRequest(request, &tokenLocation) | ||
assert.Empty(t, token) | ||
}) | ||
t.Run("case=token should be received from custom header if custom location is set to header and token is present", func(t *testing.T) { | ||
expectedToken := "token" | ||
customHeaderName := "Custom-Auth-Header" | ||
request := &http.Request{Header: http.Header{customHeaderName: {expectedToken}}} | ||
tokenLocation := helper.BearerTokenLocation{Header: &customHeaderName} | ||
token := helper.BearerTokenFromRequest(request, &tokenLocation) | ||
assert.Equal(t, expectedToken, token) | ||
}) | ||
t.Run("case=token should be received from custom header if custom location is set to query parameter and token is present", func(t *testing.T) { | ||
expectedToken := "token" | ||
customQueryParameterName := "Custom-Auth" | ||
request := &http.Request{ | ||
Form: map[string][]string{ | ||
customQueryParameterName: []string{expectedToken}, | ||
}, | ||
} | ||
tokenLocation := helper.BearerTokenLocation{QueryParameter: &customQueryParameterName} | ||
token := helper.BearerTokenFromRequest(request, &tokenLocation) | ||
assert.Equal(t, expectedToken, token) | ||
}) | ||
t.Run("case=token should be received from default header if custom token location is set, but neither Header nor Query Param is configured", func(t *testing.T) { | ||
expectedToken := "token" | ||
request := &http.Request{Header: http.Header{defaultHeaderName: {"bearer " + expectedToken}}} | ||
tokenLocation := helper.BearerTokenLocation{} | ||
token := helper.BearerTokenFromRequest(request, &tokenLocation) | ||
assert.Equal(t, expectedToken, token) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.