Skip to content

Commit 35184a8

Browse files
committed
Expose middleware.CreateExtractors function so we can use it from echo-contrib repository
1 parent b010b69 commit 35184a8

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

middleware/csrf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func CSRFWithConfig(config CSRFConfig) echo.MiddlewareFunc {
119119
config.CookieSecure = true
120120
}
121121

122-
extractors, err := createExtractors(config.TokenLookup, "")
122+
extractors, err := CreateExtractors(config.TokenLookup)
123123
if err != nil {
124124
panic(err)
125125
}

middleware/extractor.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ var errFormExtractorValueMissing = errors.New("missing value in the form")
2424
// ValuesExtractor defines a function for extracting values (keys/tokens) from the given context.
2525
type ValuesExtractor func(c echo.Context) ([]string, error)
2626

27+
// CreateExtractors creates ValuesExtractors from given lookups.
28+
// Lookups is a string in the form of "<source>:<name>" or "<source>:<name>,<source>:<name>" that is used
29+
// to extract key from the request.
30+
// Possible values:
31+
// - "header:<name>" or "header:<name>:<cut-prefix>"
32+
// `<cut-prefix>` is argument value to cut/trim prefix of the extracted value. This is useful if header
33+
// value has static prefix like `Authorization: <auth-scheme> <authorisation-parameters>` where part that we
34+
// want to cut is `<auth-scheme> ` note the space at the end.
35+
// In case of basic authentication `Authorization: Basic <credentials>` prefix we want to remove is `Basic `.
36+
// - "query:<name>"
37+
// - "param:<name>"
38+
// - "form:<name>"
39+
// - "cookie:<name>"
40+
//
41+
// Multiple sources example:
42+
// - "header:Authorization,header:X-Api-Key"
43+
func CreateExtractors(lookups string) ([]ValuesExtractor, error) {
44+
return createExtractors(lookups, "")
45+
}
46+
2747
func createExtractors(lookups string, authScheme string) ([]ValuesExtractor, error) {
2848
if lookups == "" {
2949
return nil, nil

middleware/extractor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func TestCreateExtractors(t *testing.T) {
110110
setPathParams(c, tc.givenPathParams)
111111
}
112112

113-
extractors, err := createExtractors(tc.whenLoopups, "")
113+
extractors, err := CreateExtractors(tc.whenLoopups)
114114
if tc.expectCreateError != "" {
115115
assert.EqualError(t, err, tc.expectCreateError)
116116
return

0 commit comments

Comments
 (0)