1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00

jwt-authscheme: support for custom jwt auth scheme

- added "AuthScheme" in the JWTConfig and set default value to
	  "Bearer".
	- added test case for validating JWT Auth with a custom auth
	  scheme.
This commit is contained in:
Vikram Sreekumar 2016-12-18 16:08:46 +05:30 committed by Vishal Rana
parent 6009a3d08a
commit 23176c639e
2 changed files with 17 additions and 4 deletions

View File

@ -14,6 +14,10 @@ import (
type (
// JWTConfig defines the config for JWT middleware.
JWTConfig struct {
// AuthScheme to define custom bearer variable in the Authorization header.
// Optional. Default value "Bearer"
AuthScheme string
// Skipper defines a function to skip middleware.
Skipper Skipper
@ -60,6 +64,7 @@ const (
var (
// DefaultJWTConfig is the default JWT auth middleware config.
DefaultJWTConfig = JWTConfig{
AuthScheme: bearer,
Skipper: defaultSkipper,
SigningMethod: AlgorithmHS256,
ContextKey: "user",
@ -86,6 +91,9 @@ func JWT(key []byte) echo.MiddlewareFunc {
// See: `JWT()`.
func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
// Defaults
if config.AuthScheme == "" {
config.AuthScheme = DefaultJWTConfig.AuthScheme
}
if config.Skipper == nil {
config.Skipper = DefaultJWTConfig.Skipper
}
@ -114,7 +122,7 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
// Initialize
parts := strings.Split(config.TokenLookup, ":")
extractor := jwtFromHeader(parts[1])
extractor := jwtFromHeader(parts[1], config.AuthScheme)
switch parts[0] {
case "query":
extractor = jwtFromQuery(parts[1])
@ -151,11 +159,11 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
}
// jwtFromHeader returns a `jwtExtractor` that extracts token from request header.
func jwtFromHeader(header string) jwtExtractor {
func jwtFromHeader(header string, authScheme 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 {
l := len(authScheme)
if len(auth) > l+1 && auth[:l] == authScheme {
return auth[l+1:], nil
}
return "", errors.New("empty or invalid jwt in request header")

View File

@ -64,6 +64,11 @@ func TestJWT(t *testing.T) {
config: JWTConfig{SigningKey: validKey},
info: "Valid JWT",
},
{
hdrAuth: "Token" + " " + token,
config: JWTConfig{AuthScheme: "Token", SigningKey: validKey},
info: "Valid JWT with custom AuthScheme",
},
{
hdrAuth: validAuth,
config: JWTConfig{