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

Commit

Permalink
Merge pull request #143 from square/cs/fix-142
Browse files Browse the repository at this point in the history
Preserve integers when normalizing JWT claims (fixes #142)
  • Loading branch information
csstaub committed Mar 24, 2017
2 parents d5683d9 + b367cc5 commit af13e1f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion jwt/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package jwt

import (
"bytes"
"reflect"

"gopkg.in/square/go-jose.v2/json"
Expand Down Expand Up @@ -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
}

Expand Down
26 changes: 26 additions & 0 deletions jwt/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"encoding/hex"
"encoding/pem"
"errors"
"fmt"
"io"
"reflect"
"sort"
Expand All @@ -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 {
Expand All @@ -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.")
Expand Down
3 changes: 2 additions & 1 deletion jwt/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"

"gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
)
Expand Down Expand Up @@ -149,7 +150,7 @@ func ExampleSigned() {
}

fmt.Println(raw)
// Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibGVlbGEiLCJmcnkiXSwiaXNzIjoiaXNzdWVyIiwibmJmIjoxLjQ1MTYwNjRlKzA5LCJzdWIiOiJzdWJqZWN0In0.mI6U-xUdttpOPIDUAI2uyg9lFgoqaAb-hwmz8L6L3fo
// Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibGVlbGEiLCJmcnkiXSwiaXNzIjoiaXNzdWVyIiwibmJmIjoxNDUxNjA2NDAwLCJzdWIiOiJzdWJqZWN0In0.4PgCj0VO-uG_cb1mNA38NjJyp0N-NdGIDLoYelEkciw
}

func ExampleEncrypted() {
Expand Down

0 comments on commit af13e1f

Please sign in to comment.