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>"
|
|
|
|
TokenLookup string `json:"token_lookup"`
|
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-04-25 19:58:11 +02:00
|
|
|
}
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize
|
2016-05-27 04:23:46 +02:00
|
|
|
parts := strings.Split(config.TokenLookup, ":")
|
2016-05-26 23:06:30 +02:00
|
|
|
extractor := jwtFromHeader(parts[1])
|
|
|
|
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-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 {
|
|
|
|
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)
|
|
|
|
l := len(bearer)
|
|
|
|
if len(auth) > l+1 && auth[:l] == bearer {
|
|
|
|
return auth[l+1:], nil
|
|
|
|
}
|
|
|
|
return "", errors.New("empty or 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)
|
|
|
|
if token == "" {
|
|
|
|
return "", errors.New("empty jwt in query param")
|
|
|
|
}
|
|
|
|
return token, nil
|
2016-04-25 22:41:09 +02:00
|
|
|
}
|
|
|
|
}
|