Skip to content

Commit

Permalink
Added delimiter configuration (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristinapathak authored and kcajmagic committed Jul 17, 2019
1 parent bd8eebe commit 3cc57fe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
20 changes: 16 additions & 4 deletions basculehttp/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ const (
// DefaultHeaderName is the http header to get the authorization
// information from.
DefaultHeaderName = "Authorization"

// DefaultHeaderDelimiter is the character between the authorization and
// its key.
DefaultHeaderDelimiter = " "
)

type constructor struct {
headerName string
headerDelimiter string
authorizations map[bascule.Authorization]TokenFactory
getLogger func(context.Context) bascule.Logger
onErrorResponse OnErrorResponse
Expand All @@ -39,7 +44,7 @@ func (c *constructor) decorate(next http.Handler) http.Handler {
return
}

i := strings.IndexByte(authorization, ' ')
i := strings.Index(authorization, c.headerDelimiter)
if i < 1 {
err := errors.New("unexpected authorization header value")
c.error(logger, InvalidHeader, authorization, err)
Expand All @@ -60,7 +65,7 @@ func (c *constructor) decorate(next http.Handler) http.Handler {
}

ctx := request.Context()
token, err := tf.ParseAndValidate(ctx, request, key, authorization[i+1:])
token, err := tf.ParseAndValidate(ctx, request, key, authorization[i+len(c.headerDelimiter):])
if err != nil {
c.error(logger, ParseFailed, authorization, err)
WriteResponse(response, http.StatusForbidden, err)
Expand Down Expand Up @@ -100,8 +105,14 @@ func WithHeaderName(headerName string) COption {
return func(c *constructor) {
if len(headerName) > 0 {
c.headerName = headerName
} else {
c.headerName = DefaultHeaderName
}
}
}

func WithHeaderDelimiter(delimiter string) COption {
return func(c *constructor) {
if len(delimiter) > 0 {
c.headerDelimiter = delimiter
}
}
}
Expand Down Expand Up @@ -134,6 +145,7 @@ func WithCErrorResponseFunc(f OnErrorResponse) COption {
func NewConstructor(options ...COption) func(http.Handler) http.Handler {
c := &constructor{
headerName: DefaultHeaderName,
headerDelimiter: DefaultHeaderDelimiter,
authorizations: make(map[bascule.Authorization]TokenFactory),
getLogger: bascule.GetDefaultLoggerFunc,
onErrorResponse: DefaultOnErrorResponse,
Expand Down
7 changes: 5 additions & 2 deletions basculehttp/constructor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (

func TestConstructor(t *testing.T) {
testHeader := "test header"
testDelimiter := "="
c := NewConstructor(
WithHeaderName(testHeader),
WithHeaderDelimiter(testDelimiter),
WithTokenFactory("Basic", BasicTokenFactory{"codex": "codex"}),
WithCLogger(func(_ context.Context) bascule.Logger {
return bascule.Logger(log.NewJSONLogger(log.NewSyncWriter(os.Stdout)))
Expand All @@ -24,6 +26,7 @@ func TestConstructor(t *testing.T) {
)
c2 := NewConstructor(
WithHeaderName(""),
WithHeaderDelimiter(""),
WithCLogger(func(_ context.Context) bascule.Logger { return nil }),
)
tests := []struct {
Expand All @@ -37,7 +40,7 @@ func TestConstructor(t *testing.T) {
description: "Success",
constructor: c,
requestHeaderKey: testHeader,
requestHeaderValue: "Basic Y29kZXg6Y29kZXg=",
requestHeaderValue: "Basic=Y29kZXg6Y29kZXg=",
expectedStatusCode: http.StatusOK,
},
{
Expand Down Expand Up @@ -65,7 +68,7 @@ func TestConstructor(t *testing.T) {
description: "Parse and Validate Error",
constructor: c,
requestHeaderKey: testHeader,
requestHeaderValue: "Basic AFJDK",
requestHeaderValue: "Basic=AFJDK",
expectedStatusCode: http.StatusForbidden,
},
}
Expand Down

0 comments on commit 3cc57fe

Please sign in to comment.