File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 52
52
// Possible values:
53
53
// - "header:<name>"
54
54
// - "query:<name>"
55
+ // - "param:<name>"
55
56
// - "cookie:<name>"
56
57
TokenLookup string
57
58
@@ -155,6 +156,8 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
155
156
switch parts [0 ] {
156
157
case "query" :
157
158
extractor = jwtFromQuery (parts [1 ])
159
+ case "param" :
160
+ extractor = jwtFromParam (parts [1 ])
158
161
case "cookie" :
159
162
extractor = jwtFromCookie (parts [1 ])
160
163
}
@@ -228,6 +231,17 @@ func jwtFromQuery(param string) jwtExtractor {
228
231
}
229
232
}
230
233
234
+ // jwtFromParam returns a `jwtExtractor` that extracts token from the url param string.
235
+ func jwtFromParam (param string ) jwtExtractor {
236
+ return func (c echo.Context ) (string , error ) {
237
+ token := c .Param (param )
238
+ if token == "" {
239
+ return "" , ErrJWTMissing
240
+ }
241
+ return token , nil
242
+ }
243
+ }
244
+
231
245
// jwtFromCookie returns a `jwtExtractor` that extracts token from the named cookie.
232
246
func jwtFromCookie (name string ) jwtExtractor {
233
247
return func (c echo.Context ) (string , error ) {
Original file line number Diff line number Diff line change @@ -159,6 +159,14 @@ func TestJWT(t *testing.T) {
159
159
expErrCode : http .StatusBadRequest ,
160
160
info : "Empty query" ,
161
161
},
162
+ {
163
+ config : JWTConfig {
164
+ SigningKey : validKey ,
165
+ TokenLookup : "param:jwt" ,
166
+ },
167
+ reqURL : "/" + token ,
168
+ info : "Valid param method" ,
169
+ },
162
170
{
163
171
config : JWTConfig {
164
172
SigningKey : validKey ,
@@ -195,6 +203,11 @@ func TestJWT(t *testing.T) {
195
203
req .Header .Set (echo .HeaderCookie , tc .hdrCookie )
196
204
c := e .NewContext (req , res )
197
205
206
+ if tc .reqURL == "/" + token {
207
+ c .SetParamNames ("jwt" )
208
+ c .SetParamValues (token )
209
+ }
210
+
198
211
if tc .expPanic {
199
212
assert .Panics (t , func () {
200
213
JWTWithConfig (tc .config )
You can’t perform that action at this time.
0 commit comments