1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-24 03:16:14 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-09-14 15:35:10 -07:00
parent 322b375a37
commit 862bee4ebd
2 changed files with 20 additions and 10 deletions

View File

@ -111,6 +111,7 @@ func (s *Server) Start() error {
}
if s.config.TLSCertFile != "" && s.config.TLSKeyFile != "" {
// TODO: https://github.com/golang/go/commit/d24f446a90ea94b87591bf16228d7d871fec3d92
config := &tls.Config{
NextProtos: []string{"http/1.1"},
}

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"reflect"
"strings"
"github.com/dgrijalva/jwt-go"
@ -40,6 +41,8 @@ type (
// - "query:<name>"
// - "cookie:<name>"
TokenLookup string `json:"token_lookup"`
keyFunc jwt.Keyfunc
}
jwtExtractor func(echo.Context) (string, error)
@ -76,7 +79,6 @@ var (
func JWT(key []byte) echo.MiddlewareFunc {
c := DefaultJWTConfig
c.SigningKey = key
c.Claims = jwt.MapClaims{}
return JWTWithConfig(c)
}
@ -97,11 +99,18 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
config.ContextKey = DefaultJWTConfig.ContextKey
}
if config.Claims == nil {
config.Claims = jwt.MapClaims{}
config.Claims = DefaultJWTConfig.Claims
}
if config.TokenLookup == "" {
config.TokenLookup = DefaultJWTConfig.TokenLookup
}
config.keyFunc = 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"])
}
return config.SigningKey, nil
}
// Initialize
parts := strings.Split(config.TokenLookup, ":")
@ -123,14 +132,14 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
token, err := jwt.ParseWithClaims(auth, config.Claims, 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"])
}
return config.SigningKey, nil
})
token := new(jwt.Token)
// Issue #647, #656
if _, ok := config.Claims.(jwt.MapClaims); ok {
token, err = jwt.Parse(auth, config.keyFunc)
} else {
claims := reflect.ValueOf(config.Claims).Interface().(jwt.Claims)
token, err = jwt.ParseWithClaims(auth, claims, config.keyFunc)
}
if err == nil && token.Valid {
// Store user information from token into context.
c.Set(config.ContextKey, token)