1
0
mirror of https://github.com/labstack/echo.git synced 2025-12-05 23:08:17 +02:00

feat(jwt): make KeyFunc public in JWT middleware (#1756)

* feat(jwt): make KeyFunc public in JWT middleware

It allows a user-defined function to supply the key for a token
verification.
This commit is contained in:
antonindrawan
2021-05-08 21:19:24 +02:00
committed by GitHub
parent 643066594d
commit 76f186ad3b
2 changed files with 72 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
package middleware
import (
"errors"
"net/http"
"net/http/httptest"
"net/url"
@@ -220,6 +221,35 @@ func TestJWT(t *testing.T) {
expErrCode: http.StatusBadRequest,
info: "Empty form field",
},
{
hdrAuth: validAuth,
config: JWTConfig{
KeyFunc: func(*jwt.Token) (interface{}, error) {
return validKey, nil
},
},
info: "Valid JWT with a valid key using a user-defined KeyFunc",
},
{
hdrAuth: validAuth,
config: JWTConfig{
KeyFunc: func(*jwt.Token) (interface{}, error) {
return invalidKey, nil
},
},
expErrCode: http.StatusUnauthorized,
info: "Valid JWT with an invalid key using a user-defined KeyFunc",
},
{
hdrAuth: validAuth,
config: JWTConfig{
KeyFunc: func(*jwt.Token) (interface{}, error) {
return nil, errors.New("faulty KeyFunc")
},
},
expErrCode: http.StatusUnauthorized,
info: "Token verification does not pass using a user-defined KeyFunc",
},
} {
if tc.reqURL == "" {
tc.reqURL = "/"