2016-10-20 11:30:53 -07:00
|
|
|
+++
|
|
|
|
title = "JWT Middleware"
|
|
|
|
description = "JWT middleware for Echo"
|
2016-11-20 14:16:22 -08:00
|
|
|
[menu.main]
|
2016-10-20 11:30:53 -07:00
|
|
|
name = "JWT"
|
|
|
|
parent = "middleware"
|
|
|
|
weight = 5
|
|
|
|
+++
|
|
|
|
|
|
|
|
JWT provides a JSON Web Token (JWT) authentication middleware.
|
|
|
|
|
|
|
|
- For valid token, it sets the user in context and calls next handler.
|
|
|
|
- For invalid token, it sends "401 - Unauthorized" response.
|
2017-01-02 20:12:06 -08:00
|
|
|
- For missing or invalid `Authorization` header, it sends "400 - Bad Request".
|
2016-10-20 11:30:53 -07:00
|
|
|
|
|
|
|
*Usage*
|
|
|
|
|
|
|
|
`e.Use(middleware.JWT([]byte("secret"))`
|
|
|
|
|
2016-11-19 22:13:05 -08:00
|
|
|
## Custom Configuration
|
2016-10-20 11:30:53 -07:00
|
|
|
|
|
|
|
*Usage*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
|
|
|
|
SigningKey: []byte("secret"),
|
|
|
|
TokenLookup: "query:token",
|
|
|
|
}))
|
|
|
|
```
|
|
|
|
|
2016-11-19 22:13:05 -08:00
|
|
|
## Configuration
|
2016-10-20 11:30:53 -07:00
|
|
|
|
|
|
|
```go
|
|
|
|
// JWTConfig defines the config for JWT middleware.
|
|
|
|
JWTConfig struct {
|
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
|
|
|
// Signing key to validate token.
|
|
|
|
// Required.
|
2017-01-02 20:12:06 -08:00
|
|
|
SigningKey interface{}
|
2016-10-20 11:30:53 -07:00
|
|
|
|
|
|
|
// Signing method, used to check token signing method.
|
|
|
|
// Optional. Default value HS256.
|
2017-01-02 20:12:06 -08:00
|
|
|
SigningMethod string
|
2016-10-20 11:30:53 -07:00
|
|
|
|
|
|
|
// Context key to store user information from the token into context.
|
|
|
|
// Optional. Default value "user".
|
2017-01-02 20:12:06 -08:00
|
|
|
ContextKey string
|
2016-10-20 11:30:53 -07:00
|
|
|
|
|
|
|
// Claims are extendable claims data defining token content.
|
|
|
|
// Optional. Default value jwt.MapClaims
|
|
|
|
Claims jwt.Claims
|
|
|
|
|
|
|
|
// TokenLookup is a string in the form of "<source>:<name>" that is used
|
|
|
|
// to extract token from the request.
|
|
|
|
// Optional. Default value "header:Authorization".
|
|
|
|
// Possible values:
|
|
|
|
// - "header:<name>"
|
|
|
|
// - "query:<name>"
|
|
|
|
// - "cookie:<name>"
|
2017-01-02 20:12:06 -08:00
|
|
|
TokenLookup string
|
|
|
|
|
|
|
|
// AuthScheme to be used in the Authorization header.
|
|
|
|
// Optional. Default value "Bearer".
|
|
|
|
AuthScheme string
|
2016-10-20 11:30:53 -07:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
*Default Configuration*
|
|
|
|
|
|
|
|
```go
|
|
|
|
DefaultJWTConfig = JWTConfig{
|
|
|
|
Skipper: defaultSkipper,
|
|
|
|
SigningMethod: AlgorithmHS256,
|
|
|
|
ContextKey: "user",
|
|
|
|
TokenLookup: "header:" + echo.HeaderAuthorization,
|
2017-01-02 20:12:06 -08:00
|
|
|
AuthScheme: "Bearer",
|
2016-10-20 11:30:53 -07:00
|
|
|
Claims: jwt.MapClaims{},
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-11-19 22:13:05 -08:00
|
|
|
## [Recipe]({{< ref "recipes/jwt.md">}})
|