mirror of
https://github.com/labstack/echo.git
synced 2025-07-05 00:58:47 +02:00
@ -28,6 +28,10 @@ type (
|
|||||||
// Optional. Default value "user".
|
// Optional. Default value "user".
|
||||||
ContextKey string `json:"context_key"`
|
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 "<source>:<name>" that is used
|
// TokenLookup is a string in the form of "<source>:<name>" that is used
|
||||||
// to extract token from the request.
|
// to extract token from the request.
|
||||||
// Optional. Default value "header:Authorization".
|
// Optional. Default value "header:Authorization".
|
||||||
@ -35,10 +39,6 @@ type (
|
|||||||
// - "header:<name>"
|
// - "header:<name>"
|
||||||
// - "query:<name>"
|
// - "query:<name>"
|
||||||
TokenLookup string `json:"token_lookup"`
|
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)
|
jwtExtractor func(echo.Context) (string, error)
|
||||||
|
@ -10,16 +10,16 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CustomInfo defines some custom types we're going to use within our tokens
|
// jwtCustomInfo defines some custom types we're going to use within our tokens.
|
||||||
type CustomInfo struct {
|
type jwtCustomInfo struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Admin bool `json:"admin"`
|
Admin bool `json:"admin"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MyCustomClaims are custom claims expanding default ones
|
// jwtCustomClaims are custom claims expanding default ones.
|
||||||
type MyCustomClaims struct {
|
type jwtCustomClaims struct {
|
||||||
*jwt.StandardClaims
|
*jwt.StandardClaims
|
||||||
CustomInfo
|
jwtCustomInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJWT(t *testing.T) {
|
func TestJWT(t *testing.T) {
|
||||||
@ -63,13 +63,13 @@ func TestJWT(t *testing.T) {
|
|||||||
|
|
||||||
// Valid JWT with custom claims
|
// Valid JWT with custom claims
|
||||||
config = JWTConfig{
|
config = JWTConfig{
|
||||||
Claims: &MyCustomClaims{},
|
Claims: &jwtCustomClaims{},
|
||||||
SigningKey: []byte("secret"),
|
SigningKey: []byte("secret"),
|
||||||
}
|
}
|
||||||
h = JWTWithConfig(config)(handler)
|
h = JWTWithConfig(config)(handler)
|
||||||
if assert.NoError(t, h(c)) {
|
if assert.NoError(t, h(c)) {
|
||||||
user := c.Get("user").(*jwt.Token)
|
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.Name, "John Doe")
|
||||||
assert.Equal(t, claims.Admin, true)
|
assert.Equal(t, claims.Admin, true)
|
||||||
}
|
}
|
||||||
@ -79,5 +79,4 @@ func TestJWT(t *testing.T) {
|
|||||||
h = JWT([]byte("secret"))(handler)
|
h = JWT([]byte("secret"))(handler)
|
||||||
he = h(c).(*echo.HTTPError)
|
he = h(c).(*echo.HTTPError)
|
||||||
assert.Equal(t, http.StatusBadRequest, he.Code)
|
assert.Equal(t, http.StatusBadRequest, he.Code)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user