-
Notifications
You must be signed in to change notification settings - Fork 426
Closed
Description
An error occurs for this simple code:
import (
"testing"
"github.com/golang-jwt/jwt/v5"
"github.com/stretchr/testify/assert"
)
type cc struct {
Foo string `json:"foo"`
jwt.RegisteredClaims
}
func TestParseWClaims(t *testing.T) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, cc{Foo: "value"})
// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString([]byte("xxxx"))
assert.Nil(t, err, "Sign failed")
parser := &jwt.Parser{}
var c cc
ttoken, err := parser.ParseWithClaims(tokenString, c, func(t *jwt.Token) (any, error) { return []byte("xxxx"), nil })
t.Logf("%+v", ttoken.Claims)
assert.Nil(t, err, "Parse claims")
assert.Equal(t, ttoken.Claims.(cc).Foo, "value")
}Produces:
&fmt.wrapErrors{msg:"token is malformed: could not JSON decode claim: json: cannot unmarshal object into Go value of type jwt.Claims", errs:[]error{(*errors.errorString)(0xc0000588d0), (*json.UnmarshalTypeError)(0xc0000743c0)}}
go version
go version go1.20.4 linux/amd64
The error occurs calling Decode of a json decoder. Here is a simplified example of the call stack in the code.
Note the func call which 'simulates' the call to ParseWithClaims.
type cc struct {
Foo string `json:"foo"`
jwt.RegisteredClaims
}
func TestJsonDec(t *testing.T) {
bs := []byte(`{"Foo":"value"}`)
func(claim jwt.Claims) {
c := claim
dec := json.NewDecoder(bytes.NewBuffer(bs))
e := dec.Decode(&c)
assert.Nil(t, e)
}(c)
}Apart from using the map not clear how to solve this.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels