From 3cc57fee52b2b446c807fc19a308cf0ea025e530 Mon Sep 17 00:00:00 2001 From: kristinaspring Date: Wed, 17 Jul 2019 14:22:04 -0700 Subject: [PATCH] Added delimiter configuration (#34) --- basculehttp/constructor.go | 20 ++++++++++++++++---- basculehttp/constructor_test.go | 7 +++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/basculehttp/constructor.go b/basculehttp/constructor.go index 082139b..bc5c380 100644 --- a/basculehttp/constructor.go +++ b/basculehttp/constructor.go @@ -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 @@ -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) @@ -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) @@ -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 } } } @@ -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, diff --git a/basculehttp/constructor_test.go b/basculehttp/constructor_test.go index a689f6e..700c38d 100644 --- a/basculehttp/constructor_test.go +++ b/basculehttp/constructor_test.go @@ -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))) @@ -24,6 +26,7 @@ func TestConstructor(t *testing.T) { ) c2 := NewConstructor( WithHeaderName(""), + WithHeaderDelimiter(""), WithCLogger(func(_ context.Context) bascule.Logger { return nil }), ) tests := []struct { @@ -37,7 +40,7 @@ func TestConstructor(t *testing.T) { description: "Success", constructor: c, requestHeaderKey: testHeader, - requestHeaderValue: "Basic Y29kZXg6Y29kZXg=", + requestHeaderValue: "Basic=Y29kZXg6Y29kZXg=", expectedStatusCode: http.StatusOK, }, { @@ -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, }, }