@@ -68,9 +68,14 @@ type (
68
68
// - "form:<name>"
69
69
// Multiply sources example:
70
70
// - "header: Authorization,cookie: myowncookie"
71
-
72
71
TokenLookup string
73
72
73
+ // TokenLookupFuncs defines a list of user-defined functions that extract JWT token from the given context.
74
+ // This is one of the two options to provide a token extractor.
75
+ // The order of precedence is user-defined TokenLookupFuncs, and TokenLookup.
76
+ // You can also provide both if you want.
77
+ TokenLookupFuncs []TokenLookupFunc
78
+
74
79
// AuthScheme to be used in the Authorization header.
75
80
// Optional. Default value "Bearer".
76
81
AuthScheme string
@@ -103,7 +108,8 @@ type (
103
108
// JWTErrorHandlerWithContext is almost identical to JWTErrorHandler, but it's passed the current context.
104
109
JWTErrorHandlerWithContext func (error , echo.Context ) error
105
110
106
- jwtExtractor func (echo.Context ) (string , error )
111
+ // TokenLookupFunc defines a function for extracting JWT token from the given context.
112
+ TokenLookupFunc func (echo.Context ) (string , error )
107
113
)
108
114
109
115
// Algorithms
@@ -120,13 +126,14 @@ var (
120
126
var (
121
127
// DefaultJWTConfig is the default JWT auth middleware config.
122
128
DefaultJWTConfig = JWTConfig {
123
- Skipper : DefaultSkipper ,
124
- SigningMethod : AlgorithmHS256 ,
125
- ContextKey : "user" ,
126
- TokenLookup : "header:" + echo .HeaderAuthorization ,
127
- AuthScheme : "Bearer" ,
128
- Claims : jwt.MapClaims {},
129
- KeyFunc : nil ,
129
+ Skipper : DefaultSkipper ,
130
+ SigningMethod : AlgorithmHS256 ,
131
+ ContextKey : "user" ,
132
+ TokenLookup : "header:" + echo .HeaderAuthorization ,
133
+ TokenLookupFuncs : nil ,
134
+ AuthScheme : "Bearer" ,
135
+ Claims : jwt.MapClaims {},
136
+ KeyFunc : nil ,
130
137
}
131
138
)
132
139
@@ -163,7 +170,7 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
163
170
if config .Claims == nil {
164
171
config .Claims = DefaultJWTConfig .Claims
165
172
}
166
- if config .TokenLookup == "" {
173
+ if config .TokenLookup == "" && len ( config . TokenLookupFuncs ) == 0 {
167
174
config .TokenLookup = DefaultJWTConfig .TokenLookup
168
175
}
169
176
if config .AuthScheme == "" {
@@ -179,7 +186,7 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
179
186
// Initialize
180
187
// Split sources
181
188
sources := strings .Split (config .TokenLookup , "," )
182
- var extractors [] jwtExtractor
189
+ var extractors = config . TokenLookupFuncs
183
190
for _ , source := range sources {
184
191
parts := strings .Split (source , ":" )
185
192
@@ -290,8 +297,8 @@ func (config *JWTConfig) defaultKeyFunc(t *jwt.Token) (interface{}, error) {
290
297
return config .SigningKey , nil
291
298
}
292
299
293
- // jwtFromHeader returns a `jwtExtractor ` that extracts token from the request header.
294
- func jwtFromHeader (header string , authScheme string ) jwtExtractor {
300
+ // jwtFromHeader returns a `TokenLookupFunc ` that extracts token from the request header.
301
+ func jwtFromHeader (header string , authScheme string ) TokenLookupFunc {
295
302
return func (c echo.Context ) (string , error ) {
296
303
auth := c .Request ().Header .Get (header )
297
304
l := len (authScheme )
@@ -302,8 +309,8 @@ func jwtFromHeader(header string, authScheme string) jwtExtractor {
302
309
}
303
310
}
304
311
305
- // jwtFromQuery returns a `jwtExtractor ` that extracts token from the query string.
306
- func jwtFromQuery (param string ) jwtExtractor {
312
+ // jwtFromQuery returns a `TokenLookupFunc ` that extracts token from the query string.
313
+ func jwtFromQuery (param string ) TokenLookupFunc {
307
314
return func (c echo.Context ) (string , error ) {
308
315
token := c .QueryParam (param )
309
316
if token == "" {
@@ -313,8 +320,8 @@ func jwtFromQuery(param string) jwtExtractor {
313
320
}
314
321
}
315
322
316
- // jwtFromParam returns a `jwtExtractor ` that extracts token from the url param string.
317
- func jwtFromParam (param string ) jwtExtractor {
323
+ // jwtFromParam returns a `TokenLookupFunc ` that extracts token from the url param string.
324
+ func jwtFromParam (param string ) TokenLookupFunc {
318
325
return func (c echo.Context ) (string , error ) {
319
326
token := c .Param (param )
320
327
if token == "" {
@@ -324,8 +331,8 @@ func jwtFromParam(param string) jwtExtractor {
324
331
}
325
332
}
326
333
327
- // jwtFromCookie returns a `jwtExtractor ` that extracts token from the named cookie.
328
- func jwtFromCookie (name string ) jwtExtractor {
334
+ // jwtFromCookie returns a `TokenLookupFunc ` that extracts token from the named cookie.
335
+ func jwtFromCookie (name string ) TokenLookupFunc {
329
336
return func (c echo.Context ) (string , error ) {
330
337
cookie , err := c .Cookie (name )
331
338
if err != nil {
@@ -335,8 +342,8 @@ func jwtFromCookie(name string) jwtExtractor {
335
342
}
336
343
}
337
344
338
- // jwtFromForm returns a `jwtExtractor ` that extracts token from the form field.
339
- func jwtFromForm (name string ) jwtExtractor {
345
+ // jwtFromForm returns a `TokenLookupFunc ` that extracts token from the form field.
346
+ func jwtFromForm (name string ) TokenLookupFunc {
340
347
return func (c echo.Context ) (string , error ) {
341
348
field := c .FormValue (name )
342
349
if field == "" {
0 commit comments