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

Commit af13e1f

Browse files
authored
Merge pull request #143 from square/cs/fix-142
Preserve integers when normalizing JWT claims (fixes #142)
2 parents d5683d9 + b367cc5 commit af13e1f

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

jwt/builder.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package jwt
1919

2020
import (
21+
"bytes"
2122
"reflect"
2223

2324
"gopkg.in/square/go-jose.v2/json"
@@ -136,7 +137,11 @@ func normalize(i interface{}) (map[string]interface{}, error) {
136137
if err != nil {
137138
return nil, err
138139
}
139-
if err := json.Unmarshal(raw, &m); err != nil {
140+
141+
d := json.NewDecoder(bytes.NewReader(raw))
142+
d.UseNumber()
143+
144+
if err := d.Decode(&m); err != nil {
140145
return nil, err
141146
}
142147

jwt/builder_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"encoding/hex"
2525
"encoding/pem"
2626
"errors"
27+
"fmt"
2728
"io"
2829
"reflect"
2930
"sort"
@@ -34,6 +35,7 @@ import (
3435
"github.com/stretchr/testify/require"
3536

3637
"gopkg.in/square/go-jose.v2"
38+
"gopkg.in/square/go-jose.v2/json"
3739
)
3840

3941
type testClaims struct {
@@ -56,6 +58,30 @@ var sampleClaims = Claims{
5658
Audience: Audience{"a1", "a2"},
5759
}
5860

61+
type numberClaims struct {
62+
Int int64 `json:"int"`
63+
Float float64 `json:"float"`
64+
}
65+
66+
func TestIntegerAndFloatsNormalize(t *testing.T) {
67+
c := numberClaims{1 << 60, 12345.6789}
68+
69+
normalized, err := normalize(c)
70+
if err != nil {
71+
t.Fatal(err)
72+
}
73+
74+
ni, err := (normalized["int"].(json.Number)).Int64()
75+
nf, err := (normalized["float"].(json.Number)).Float64()
76+
77+
if ni != c.Int {
78+
t.Error(fmt.Sprintf("normalize failed to preserve int64 (got %v, wanted %v, type %s)", normalized["int"], c.Int, reflect.TypeOf(normalized["int"])))
79+
}
80+
if nf != c.Float {
81+
t.Error(fmt.Sprintf("normalize failed to preserve float64 (got %v, wanted %v, type %s)", normalized["float"], c.Float, reflect.TypeOf(normalized["float"])))
82+
}
83+
}
84+
5985
func TestBuilderCustomClaimsNonPointer(t *testing.T) {
6086
jwt, err := Signed(rsaSigner).Claims(testClaims{"foo"}).CompactSerialize()
6187
require.NoError(t, err, "Error creating JWT.")

jwt/example_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"crypto/rsa"
2626
"crypto/x509"
2727
"encoding/pem"
28+
2829
"gopkg.in/square/go-jose.v2"
2930
"gopkg.in/square/go-jose.v2/jwt"
3031
)
@@ -149,7 +150,7 @@ func ExampleSigned() {
149150
}
150151

151152
fmt.Println(raw)
152-
// Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibGVlbGEiLCJmcnkiXSwiaXNzIjoiaXNzdWVyIiwibmJmIjoxLjQ1MTYwNjRlKzA5LCJzdWIiOiJzdWJqZWN0In0.mI6U-xUdttpOPIDUAI2uyg9lFgoqaAb-hwmz8L6L3fo
153+
// Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibGVlbGEiLCJmcnkiXSwiaXNzIjoiaXNzdWVyIiwibmJmIjoxNDUxNjA2NDAwLCJzdWIiOiJzdWJqZWN0In0.4PgCj0VO-uG_cb1mNA38NjJyp0N-NdGIDLoYelEkciw
153154
}
154155

155156
func ExampleEncrypted() {

0 commit comments

Comments
 (0)