From 35184a893b98d2e2451d061c210a61e2d50030a0 Mon Sep 17 00:00:00 2001 From: toimtoimtoim Date: Sat, 12 Nov 2022 23:14:59 +0200 Subject: [PATCH] Expose middleware.CreateExtractors function so we can use it from echo-contrib repository --- middleware/csrf.go | 2 +- middleware/extractor.go | 20 ++++++++++++++++++++ middleware/extractor_test.go | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/middleware/csrf.go b/middleware/csrf.go index ea90fdba7..8661c9f89 100644 --- a/middleware/csrf.go +++ b/middleware/csrf.go @@ -119,7 +119,7 @@ func CSRFWithConfig(config CSRFConfig) echo.MiddlewareFunc { config.CookieSecure = true } - extractors, err := createExtractors(config.TokenLookup, "") + extractors, err := CreateExtractors(config.TokenLookup) if err != nil { panic(err) } diff --git a/middleware/extractor.go b/middleware/extractor.go index afdfd8195..5d9cee6d0 100644 --- a/middleware/extractor.go +++ b/middleware/extractor.go @@ -24,6 +24,26 @@ var errFormExtractorValueMissing = errors.New("missing value in the form") // ValuesExtractor defines a function for extracting values (keys/tokens) from the given context. type ValuesExtractor func(c echo.Context) ([]string, error) +// CreateExtractors creates ValuesExtractors from given lookups. +// Lookups is a string in the form of ":" or ":,:" that is used +// to extract key from the request. +// Possible values: +// - "header:" or "header::" +// `` is argument value to cut/trim prefix of the extracted value. This is useful if header +// value has static prefix like `Authorization: ` where part that we +// want to cut is ` ` note the space at the end. +// In case of basic authentication `Authorization: Basic ` prefix we want to remove is `Basic `. +// - "query:" +// - "param:" +// - "form:" +// - "cookie:" +// +// Multiple sources example: +// - "header:Authorization,header:X-Api-Key" +func CreateExtractors(lookups string) ([]ValuesExtractor, error) { + return createExtractors(lookups, "") +} + func createExtractors(lookups string, authScheme string) ([]ValuesExtractor, error) { if lookups == "" { return nil, nil diff --git a/middleware/extractor_test.go b/middleware/extractor_test.go index 2e898f541..428c5563e 100644 --- a/middleware/extractor_test.go +++ b/middleware/extractor_test.go @@ -110,7 +110,7 @@ func TestCreateExtractors(t *testing.T) { setPathParams(c, tc.givenPathParams) } - extractors, err := createExtractors(tc.whenLoopups, "") + extractors, err := CreateExtractors(tc.whenLoopups) if tc.expectCreateError != "" { assert.EqualError(t, err, tc.expectCreateError) return