Skip to content
This repository was archived by the owner on Feb 27, 2023. It is now read-only.
This repository was archived by the owner on Feb 27, 2023. It is now read-only.

Builder serializes integers as floats, which subsequently fail to Decode. #142

Closed
@jacobstr

Description

@jacobstr

Assuming I'm doing this all using the blessed path. I've included a snippet at the bottom to demonstrate.

Relevant output produced:

serialized to eyJhbGciOiJIUzUxMiJ9.eyJudW1iZXIiOjFlKzA2fQ.oFRp6CQOyNEicaiXDZASuGZpAX00fWnCoMRd89ueIFqo2t48WUo4C3Ld0EkrSViLYk5FxoxmyxXRJaq6BYKWuA
parsing signed
parsing claims
json: cannot unmarshal number 1e+06 into Go value of type int64

If you base64 decode the 2nd component of the compact JWT you'll see "number": 1e+06.

I suspect the culprit is here: https://github.com/square/go-jose/blob/v2/jwt/builder.go#L132 - the Marshal/Unmarshal loses the type information that would otherwise cause go's marshaler to encode Number as an integer.

package main

import (
	"fmt"

	jose "gopkg.in/square/go-jose.v2"
	jwt "gopkg.in/square/go-jose.v2/jwt"
)

const SecretKey = "WoW"

type Claims struct {
	Number int64 `json:"number"`
}

func main() {
	fmt.Println("building signer")
	signer, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.HS512, Key: []byte(SecretKey)}, nil)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("building builder")
	token := jwt.Signed(signer)
	token = token.Claims(Claims{Number: 1000000})

	fmt.Println("serializing")
	enc, err := token.CompactSerialize()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("serialized to %s\n", enc)

	fmt.Println("parsing signed")
	tok, err := jwt.ParseSigned(enc)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("parsing claims")
	claims := Claims{}
	if err := tok.Claims([]byte(SecretKey), &claims); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%#v\n", claims)
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions