diff --git a/jwt/builder.go b/jwt/builder.go index fa177988..11ee09e6 100644 --- a/jwt/builder.go +++ b/jwt/builder.go @@ -18,6 +18,7 @@ package jwt import ( + "bytes" "reflect" "gopkg.in/square/go-jose.v2/json" @@ -136,7 +137,11 @@ func normalize(i interface{}) (map[string]interface{}, error) { if err != nil { return nil, err } - if err := json.Unmarshal(raw, &m); err != nil { + + d := json.NewDecoder(bytes.NewReader(raw)) + d.UseNumber() + + if err := d.Decode(&m); err != nil { return nil, err } diff --git a/jwt/builder_test.go b/jwt/builder_test.go index 25a656f7..8213c47f 100644 --- a/jwt/builder_test.go +++ b/jwt/builder_test.go @@ -24,6 +24,7 @@ import ( "encoding/hex" "encoding/pem" "errors" + "fmt" "io" "reflect" "sort" @@ -34,6 +35,7 @@ import ( "github.com/stretchr/testify/require" "gopkg.in/square/go-jose.v2" + "gopkg.in/square/go-jose.v2/json" ) type testClaims struct { @@ -56,6 +58,30 @@ var sampleClaims = Claims{ Audience: Audience{"a1", "a2"}, } +type numberClaims struct { + Int int64 `json:"int"` + Float float64 `json:"float"` +} + +func TestIntegerAndFloatsNormalize(t *testing.T) { + c := numberClaims{1 << 60, 12345.6789} + + normalized, err := normalize(c) + if err != nil { + t.Fatal(err) + } + + ni, err := (normalized["int"].(json.Number)).Int64() + nf, err := (normalized["float"].(json.Number)).Float64() + + if ni != c.Int { + t.Error(fmt.Sprintf("normalize failed to preserve int64 (got %v, wanted %v, type %s)", normalized["int"], c.Int, reflect.TypeOf(normalized["int"]))) + } + if nf != c.Float { + t.Error(fmt.Sprintf("normalize failed to preserve float64 (got %v, wanted %v, type %s)", normalized["float"], c.Float, reflect.TypeOf(normalized["float"]))) + } +} + func TestBuilderCustomClaimsNonPointer(t *testing.T) { jwt, err := Signed(rsaSigner).Claims(testClaims{"foo"}).CompactSerialize() require.NoError(t, err, "Error creating JWT.") diff --git a/jwt/example_test.go b/jwt/example_test.go index 17595395..fa65a025 100644 --- a/jwt/example_test.go +++ b/jwt/example_test.go @@ -25,6 +25,7 @@ import ( "crypto/rsa" "crypto/x509" "encoding/pem" + "gopkg.in/square/go-jose.v2" "gopkg.in/square/go-jose.v2/jwt" ) @@ -149,7 +150,7 @@ func ExampleSigned() { } fmt.Println(raw) - // Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibGVlbGEiLCJmcnkiXSwiaXNzIjoiaXNzdWVyIiwibmJmIjoxLjQ1MTYwNjRlKzA5LCJzdWIiOiJzdWJqZWN0In0.mI6U-xUdttpOPIDUAI2uyg9lFgoqaAb-hwmz8L6L3fo + // Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibGVlbGEiLCJmcnkiXSwiaXNzIjoiaXNzdWVyIiwibmJmIjoxNDUxNjA2NDAwLCJzdWIiOiJzdWJqZWN0In0.4PgCj0VO-uG_cb1mNA38NjJyp0N-NdGIDLoYelEkciw } func ExampleEncrypted() {