2015-05-12 00:43:54 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2016-05-13 17:18:00 +02:00
|
|
|
"errors"
|
2016-04-25 19:58:11 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2016-05-26 23:06:30 +02:00
|
|
|
"strings"
|
2015-07-05 15:21:05 +02:00
|
|
|
|
2016-04-25 19:58:11 +02:00
|
|
|
"github.com/dgrijalva/jwt-go"
|
2015-07-05 15:21:05 +02:00
|
|
|
"github.com/labstack/echo"
|
2015-05-12 00:43:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-05-07 16:45:03 +02:00
|
|
|
// JWTConfig defines the config for JWT auth middleware.
|
|
|
|
JWTConfig struct {
|
2016-05-10 20:52:04 +02:00
|
|
|
// Signing key to validate token.
|
2016-04-25 19:58:11 +02:00
|
|
|
// Required.
|
2016-05-19 03:53:54 +02:00
|
|
|
SigningKey []byte `json:"signing_key"`
|
2016-04-25 19:58:11 +02:00
|
|
|
|
2016-05-10 20:52:04 +02:00
|
|
|
// Signing method, used to check token signing method.
|
|
|
|
// Optional. Default value HS256.
|
2016-05-19 03:53:54 +02:00
|
|
|
SigningMethod string `json:"signing_method"`
|
2016-04-25 19:58:11 +02:00
|
|
|
|
2016-05-10 20:52:04 +02:00
|
|
|
// Context key to store user information from the token into context.
|
|
|
|
// Optional. Default value "user".
|
2016-05-19 03:53:54 +02:00
|
|
|
ContextKey string `json:"context_key"`
|
2016-04-25 19:58:11 +02:00
|
|
|
|
2016-05-27 04:23:46 +02:00
|
|
|
// TokenLookup is a string in the form of "<source>:<name>" that is used
|
|
|
|
// to extract token from the request.
|
2016-05-26 23:06:30 +02:00
|
|
|
// Optional. Default value "header:Authorization".
|
|
|
|
// Possible values:
|
|
|
|
// - "header:<name>"
|
2016-05-27 04:23:46 +02:00
|
|
|
// - "query:<name>"
|
2016-07-02 11:26:05 +02:00
|
|
|
// - "cookie:<name>"
|
2016-05-27 04:23:46 +02:00
|
|
|
TokenLookup string `json:"token_lookup"`
|
2016-07-02 00:55:11 +02:00
|
|
|
|
|
|
|
// HandleEmptyToken is handler executed when there is no token.
|
|
|
|
// It could be used for redirection.
|
|
|
|
HandleEmptyToken echo.HandlerFunc
|
2016-04-25 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 23:06:30 +02:00
|
|
|
jwtExtractor func(echo.Context) (string, error)
|
2015-05-12 00:43:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-04-25 19:58:11 +02:00
|
|
|
bearer = "Bearer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Algorithims
|
|
|
|
const (
|
|
|
|
AlgorithmHS256 = "HS256"
|
2015-05-12 00:43:54 +02:00
|
|
|
)
|
|
|
|
|
2016-03-14 22:55:38 +02:00
|
|
|
var (
|
2016-05-07 16:45:03 +02:00
|
|
|
// DefaultJWTConfig is the default JWT auth middleware config.
|
|
|
|
DefaultJWTConfig = JWTConfig{
|
2016-04-25 19:58:11 +02:00
|
|
|
SigningMethod: AlgorithmHS256,
|
|
|
|
ContextKey: "user",
|
2016-05-27 04:23:46 +02:00
|
|
|
TokenLookup: "header:" + echo.HeaderAuthorization,
|
2016-07-02 00:55:11 +02:00
|
|
|
HandleEmptyToken: func(c echo.Context) error {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, errJWTEmptyToken.Error())
|
|
|
|
},
|
2016-04-25 19:58:11 +02:00
|
|
|
}
|
2016-07-02 00:55:11 +02:00
|
|
|
|
|
|
|
errJWTEmptyToken = errors.New("empty jwt")
|
2016-03-14 22:55:38 +02:00
|
|
|
)
|
|
|
|
|
2016-05-07 16:45:03 +02:00
|
|
|
// JWT returns a JSON Web Token (JWT) auth middleware.
|
2016-04-25 19:58:11 +02:00
|
|
|
//
|
|
|
|
// For valid token, it sets the user in context and calls next handler.
|
|
|
|
// For invalid token, it sends "401 - Unauthorized" response.
|
|
|
|
// For empty or invalid `Authorization` header, it sends "400 - Bad Request".
|
|
|
|
//
|
2016-05-13 02:45:00 +02:00
|
|
|
// See: https://jwt.io/introduction
|
2016-07-02 11:26:05 +02:00
|
|
|
// See `JWTConfig.TokenLookup`
|
2016-05-07 16:45:03 +02:00
|
|
|
func JWT(key []byte) echo.MiddlewareFunc {
|
|
|
|
c := DefaultJWTConfig
|
2016-04-25 19:58:11 +02:00
|
|
|
c.SigningKey = key
|
2016-05-07 16:45:03 +02:00
|
|
|
return JWTWithConfig(c)
|
2016-04-25 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
2016-05-07 16:45:03 +02:00
|
|
|
// JWTWithConfig returns a JWT auth middleware from config.
|
2016-05-13 02:45:00 +02:00
|
|
|
// See: `JWT()`.
|
2016-05-07 16:45:03 +02:00
|
|
|
func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
|
2016-04-25 19:58:11 +02:00
|
|
|
// Defaults
|
2016-04-26 11:10:57 +02:00
|
|
|
if config.SigningKey == nil {
|
2016-04-25 19:58:11 +02:00
|
|
|
panic("jwt middleware requires signing key")
|
|
|
|
}
|
|
|
|
if config.SigningMethod == "" {
|
2016-05-07 16:45:03 +02:00
|
|
|
config.SigningMethod = DefaultJWTConfig.SigningMethod
|
2016-04-25 19:58:11 +02:00
|
|
|
}
|
|
|
|
if config.ContextKey == "" {
|
2016-05-07 16:45:03 +02:00
|
|
|
config.ContextKey = DefaultJWTConfig.ContextKey
|
2016-04-25 19:58:11 +02:00
|
|
|
}
|
2016-05-27 04:23:46 +02:00
|
|
|
if config.TokenLookup == "" {
|
|
|
|
config.TokenLookup = DefaultJWTConfig.TokenLookup
|
2016-05-26 23:06:30 +02:00
|
|
|
}
|
2016-07-02 00:55:11 +02:00
|
|
|
if config.HandleEmptyToken == nil {
|
|
|
|
config.HandleEmptyToken = DefaultJWTConfig.HandleEmptyToken
|
|
|
|
}
|
2016-05-26 23:06:30 +02:00
|
|
|
|
|
|
|
// Initialize
|
2016-05-27 04:23:46 +02:00
|
|
|
parts := strings.Split(config.TokenLookup, ":")
|
2016-07-02 11:26:05 +02:00
|
|
|
var extractor jwtExtractor
|
2016-05-26 23:06:30 +02:00
|
|
|
switch parts[0] {
|
2016-05-27 04:23:46 +02:00
|
|
|
case "query":
|
2016-05-26 23:06:30 +02:00
|
|
|
extractor = jwtFromQuery(parts[1])
|
2016-07-02 11:26:05 +02:00
|
|
|
case "cookie":
|
|
|
|
extractor = jwtFromCookie(parts[1])
|
|
|
|
default:
|
|
|
|
extractor = jwtFromHeader(parts[1])
|
2016-04-25 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-05-26 23:06:30 +02:00
|
|
|
auth, err := extractor(c)
|
2016-04-25 19:58:11 +02:00
|
|
|
if err != nil {
|
2016-07-02 00:55:11 +02:00
|
|
|
if err == errJWTEmptyToken {
|
|
|
|
return config.HandleEmptyToken(c)
|
|
|
|
}
|
2016-04-25 19:58:11 +02:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
token, err := jwt.Parse(auth, func(t *jwt.Token) (interface{}, error) {
|
|
|
|
// Check the signing method
|
|
|
|
if t.Method.Alg() != config.SigningMethod {
|
|
|
|
return nil, fmt.Errorf("unexpected jwt signing method=%v", t.Header["alg"])
|
|
|
|
}
|
2016-04-26 11:10:57 +02:00
|
|
|
return config.SigningKey, nil
|
2016-04-25 19:58:11 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
if err == nil && token.Valid {
|
|
|
|
// Store user information from token into context.
|
|
|
|
c.Set(config.ContextKey, token)
|
|
|
|
return next(c)
|
|
|
|
}
|
2016-03-12 20:18:40 +02:00
|
|
|
return echo.ErrUnauthorized
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2016-02-18 07:49:31 +02:00
|
|
|
}
|
2015-06-10 05:06:51 +02:00
|
|
|
}
|
2016-04-25 22:41:09 +02:00
|
|
|
|
2016-05-26 23:06:30 +02:00
|
|
|
// jwtFromHeader returns a `jwtExtractor` that extracts token from the provided
|
|
|
|
// request header.
|
|
|
|
func jwtFromHeader(header string) jwtExtractor {
|
|
|
|
return func(c echo.Context) (string, error) {
|
|
|
|
auth := c.Request().Header().Get(header)
|
2016-07-02 00:55:11 +02:00
|
|
|
lenAuth := len(auth)
|
|
|
|
if lenAuth == 0 {
|
|
|
|
return "", errJWTEmptyToken
|
|
|
|
}
|
2016-05-26 23:06:30 +02:00
|
|
|
l := len(bearer)
|
2016-07-02 00:55:11 +02:00
|
|
|
if lenAuth > l+1 && auth[:l] == bearer {
|
2016-05-26 23:06:30 +02:00
|
|
|
return auth[l+1:], nil
|
|
|
|
}
|
2016-07-02 00:55:11 +02:00
|
|
|
return "", errors.New("invalid jwt in authorization header")
|
2016-04-25 22:41:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 23:06:30 +02:00
|
|
|
// jwtFromQuery returns a `jwtExtractor` that extracts token from the provided query
|
2016-04-25 22:41:09 +02:00
|
|
|
// parameter.
|
2016-05-26 23:06:30 +02:00
|
|
|
func jwtFromQuery(param string) jwtExtractor {
|
2016-04-25 22:41:09 +02:00
|
|
|
return func(c echo.Context) (string, error) {
|
2016-05-13 17:18:00 +02:00
|
|
|
token := c.QueryParam(param)
|
2016-07-02 00:55:11 +02:00
|
|
|
var err error
|
2016-05-13 17:18:00 +02:00
|
|
|
if token == "" {
|
2016-07-02 00:55:11 +02:00
|
|
|
err = errJWTEmptyToken
|
2016-05-13 17:18:00 +02:00
|
|
|
}
|
2016-07-02 00:55:11 +02:00
|
|
|
return token, err
|
2016-04-25 22:41:09 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-02 11:26:05 +02:00
|
|
|
|
|
|
|
// jwtFromCookie returns a `jwtExtractor` that extracts token from named cookie.
|
|
|
|
func jwtFromCookie(name string) jwtExtractor {
|
|
|
|
return func(c echo.Context) (string, error) {
|
|
|
|
cookie, err := c.Cookie(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", errJWTEmptyToken
|
|
|
|
}
|
|
|
|
return cookie.Value(), nil
|
|
|
|
}
|
|
|
|
}
|