mirror of
https://github.com/labstack/echo.git
synced 2025-09-16 09:16:29 +02:00
Updated README.md with JWT middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
@@ -237,6 +237,7 @@ Middleware | Description
|
|||||||
[Recover](https://labstack.com/echo/guide/middleware#recover-middleware:37ab2f15ff048f67959bcac0a6032f32) | Recover from panics
|
[Recover](https://labstack.com/echo/guide/middleware#recover-middleware:37ab2f15ff048f67959bcac0a6032f32) | Recover from panics
|
||||||
[Gzip](https://labstack.com/echo/guide/middleware#gzip-middleware:37ab2f15ff048f67959bcac0a6032f32) | Send gzip HTTP response
|
[Gzip](https://labstack.com/echo/guide/middleware#gzip-middleware:37ab2f15ff048f67959bcac0a6032f32) | Send gzip HTTP response
|
||||||
[BasicAuth](https://labstack.com/echo/guide/middleware#basicauth-middleware:37ab2f15ff048f67959bcac0a6032f32) | HTTP basic authentication
|
[BasicAuth](https://labstack.com/echo/guide/middleware#basicauth-middleware:37ab2f15ff048f67959bcac0a6032f32) | HTTP basic authentication
|
||||||
|
[JWTAuth](https://labstack.com/echo/guide/middleware#jwtauth-middleware:37ab2f15ff048f67959bcac0a6032f32) | JWT authentication
|
||||||
[CORS](https://labstack.com/echo/guide/middleware#cors-middleware:37ab2f15ff048f67959bcac0a6032f32) | Cross-Origin Resource Sharing
|
[CORS](https://labstack.com/echo/guide/middleware#cors-middleware:37ab2f15ff048f67959bcac0a6032f32) | Cross-Origin Resource Sharing
|
||||||
[Static](https://labstack.com/echo/guide/static-files#using-static-middleware:123f9d1043075fe4874616541b409e4d) | Serve static files
|
[Static](https://labstack.com/echo/guide/static-files#using-static-middleware:123f9d1043075fe4874616541b409e4d) | Serve static files
|
||||||
[AddTrailingSlash](https://labstack.com/echo/guide/middleware#addtrailingslash-middleware:37ab2f15ff048f67959bcac0a6032f32) | Add trailing slash to the request URI
|
[AddTrailingSlash](https://labstack.com/echo/guide/middleware#addtrailingslash-middleware:37ab2f15ff048f67959bcac0a6032f32) | Add trailing slash to the request URI
|
||||||
|
@@ -70,7 +70,7 @@ var (
|
|||||||
//
|
//
|
||||||
// For valid credentials it calls the next handler.
|
// For valid credentials it calls the next handler.
|
||||||
// For invalid credentials, it sends "401 - Unauthorized" response.
|
// For invalid credentials, it sends "401 - Unauthorized" response.
|
||||||
// For empty or invalid `Authorization` header, it sends "400 - Bad Request".
|
// For empty or invalid `Authorization` header, it sends "400 - Bad Request" response.
|
||||||
func BasicAuth(fn BasicAuthFunc) echo.MiddlewareFunc {
|
func BasicAuth(fn BasicAuthFunc) echo.MiddlewareFunc {
|
||||||
c := DefaultBasicAuthConfig
|
c := DefaultBasicAuthConfig
|
||||||
c.AuthFunc = fn
|
c.AuthFunc = fn
|
||||||
@@ -107,25 +107,6 @@ func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// JWTFromHeader is a `JWTExtractor` that extracts token from the `Authorization` request
|
|
||||||
// header.
|
|
||||||
func JWTFromHeader(c echo.Context) (string, error) {
|
|
||||||
auth := c.Request().Header().Get(echo.HeaderAuthorization)
|
|
||||||
l := len(bearer)
|
|
||||||
if len(auth) > l+1 && auth[:l] == bearer {
|
|
||||||
return auth[l+1:], nil
|
|
||||||
}
|
|
||||||
return "", echo.NewHTTPError(http.StatusBadRequest, "invalid jwt authorization header="+auth)
|
|
||||||
}
|
|
||||||
|
|
||||||
// JWTFromQuery returns a `JWTExtractor` that extracts token from the provided query
|
|
||||||
// parameter.
|
|
||||||
func JWTFromQuery(param string) JWTExtractor {
|
|
||||||
return func(c echo.Context) (string, error) {
|
|
||||||
return c.QueryParam(param), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// JWTAuth returns a JSON Web Token (JWT) auth middleware.
|
// JWTAuth returns a JSON Web Token (JWT) auth middleware.
|
||||||
//
|
//
|
||||||
// For valid token, it sets the user in context and calls next handler.
|
// For valid token, it sets the user in context and calls next handler.
|
||||||
@@ -179,3 +160,22 @@ func JWTAuthWithConfig(config JWTAuthConfig) echo.MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JWTFromHeader is a `JWTExtractor` that extracts token from the `Authorization` request
|
||||||
|
// header.
|
||||||
|
func JWTFromHeader(c echo.Context) (string, error) {
|
||||||
|
auth := c.Request().Header().Get(echo.HeaderAuthorization)
|
||||||
|
l := len(bearer)
|
||||||
|
if len(auth) > l+1 && auth[:l] == bearer {
|
||||||
|
return auth[l+1:], nil
|
||||||
|
}
|
||||||
|
return "", echo.NewHTTPError(http.StatusBadRequest, "invalid jwt authorization header="+auth)
|
||||||
|
}
|
||||||
|
|
||||||
|
// JWTFromQuery returns a `JWTExtractor` that extracts token from the provided query
|
||||||
|
// parameter.
|
||||||
|
func JWTFromQuery(param string) JWTExtractor {
|
||||||
|
return func(c echo.Context) (string, error) {
|
||||||
|
return c.QueryParam(param), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user