-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Allow JWT middleware to gracefully fail #2048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This is good change. In my own applications I have wrapped JWT into middlewarefunct to call next if errors is returned, but direct support for that in official middleware is better. Example: // NewJWTExtraction extracts JWToken for request and set it into context (if request had it)
// will return error when token existed and we had problems with it (ala token was expired)
// will not error when token did not exist - so our "not secured" routes will work
// real resource authorization is done in PrivilegeCheck middleware
func NewJWTExtraction(config MiddlewareConfig) echo.MiddlewareFunc {
jwtConf := middleware.JWTWithConfig(middleware.JWTConfig{
Claims: &JwtClaims{},
Skipper: APISkipper,
SigningKey: []byte(config.JwtSigningKey),
TokenLookup: "header:" + echo.HeaderAuthorization,
ContextKey: tokenContextKey,
})
return func(next echo.HandlerFunc) echo.HandlerFunc {
// dummy function for jwt middleware success path to execute
jwtFunc := jwtConf(func(context echo.Context) error {
return nil
})
return func(c echo.Context) error {
err := jwtFunc(c)
if err != nil && err != middleware.ErrJWTMissing {
return err
}
return next(c)
}
}
} in // Allow error handler to swallow the error and continue handler chain execution
// Useful in cases when portion of your site/api is publicly accessible and has extra features for authorized users
// In that case you can use ErrorHandler to set default public token to request and continue with handler chain
if handledErr := config.ErrorHandler(c, err); handledErr != nil {
return handledErr
}
return next(c) so maybe we could align if config.ErrorHandlerWithContext != nil {
if err := config.ErrorHandlerWithContext(err, c); err != nil {
return err
} else {
return next(c)
}
} note: in I propose instead of adding |
Codecov Report
@@ Coverage Diff @@
## master #2048 +/- ##
==========================================
- Coverage 91.33% 91.28% -0.06%
==========================================
Files 33 33
Lines 2875 2879 +4
==========================================
+ Hits 2626 2628 +2
- Misses 159 160 +1
- Partials 90 91 +1
Continue to review full report at Codecov.
|
Closing this feature is supported by using Lines 35 to 40 in 5ebed44
How it works: Lines 243 to 249 in 5ebed44
|
This is great, and works like a charm. Thank you, @aldas 🙌 |
This PR adds a
CredentialsOptional
configuration option to the JWT middleware, allowing it to gracefully fail. It allows the next handler to be called, even when there is no valid JWT token present.This was brought up before in #1039, but I think it might have gotten lost in the PR that was ultimately merged. This PR reintroduces the flag and adds some tests to validate the behavior.