From 87dbea59f786100efdd7f14b60829fc161a96cd1 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sat, 20 Aug 2016 09:12:17 -0700 Subject: [PATCH] Minor refactor and format Signed-off-by: Vishal Rana --- middleware/jwt.go | 8 ++++---- middleware/jwt_test.go | 15 +++++++-------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/middleware/jwt.go b/middleware/jwt.go index 7f811989..815db9e5 100644 --- a/middleware/jwt.go +++ b/middleware/jwt.go @@ -28,6 +28,10 @@ type ( // Optional. Default value "user". ContextKey string `json:"context_key"` + // Claims are extendable claims data defining token content. + // Optional. Default value jwt.MapClaims + Claims jwt.Claims + // TokenLookup is a string in the form of ":" that is used // to extract token from the request. // Optional. Default value "header:Authorization". @@ -35,10 +39,6 @@ type ( // - "header:" // - "query:" TokenLookup string `json:"token_lookup"` - - // Claims are extendable claims data defining token content. - // Optional. Default value jwt.MapClaims - Claims jwt.Claims } jwtExtractor func(echo.Context) (string, error) diff --git a/middleware/jwt_test.go b/middleware/jwt_test.go index cd2518c6..4c5af63f 100644 --- a/middleware/jwt_test.go +++ b/middleware/jwt_test.go @@ -10,16 +10,16 @@ import ( "github.com/stretchr/testify/assert" ) -// CustomInfo defines some custom types we're going to use within our tokens -type CustomInfo struct { +// jwtCustomInfo defines some custom types we're going to use within our tokens. +type jwtCustomInfo struct { Name string `json:"name"` Admin bool `json:"admin"` } -// MyCustomClaims are custom claims expanding default ones -type MyCustomClaims struct { +// jwtCustomClaims are custom claims expanding default ones. +type jwtCustomClaims struct { *jwt.StandardClaims - CustomInfo + jwtCustomInfo } func TestJWT(t *testing.T) { @@ -63,13 +63,13 @@ func TestJWT(t *testing.T) { // Valid JWT with custom claims config = JWTConfig{ - Claims: &MyCustomClaims{}, + Claims: &jwtCustomClaims{}, SigningKey: []byte("secret"), } h = JWTWithConfig(config)(handler) if assert.NoError(t, h(c)) { user := c.Get("user").(*jwt.Token) - claims := user.Claims.(*MyCustomClaims) + claims := user.Claims.(*jwtCustomClaims) assert.Equal(t, claims.Name, "John Doe") assert.Equal(t, claims.Admin, true) } @@ -79,5 +79,4 @@ func TestJWT(t *testing.T) { h = JWT([]byte("secret"))(handler) he = h(c).(*echo.HTTPError) assert.Equal(t, http.StatusBadRequest, he.Code) - }