Skip to content

Commit

Permalink
Change RawToken.Decode signature
Browse files Browse the repository at this point in the history
Passing a Header address as first argument doesn't look natural, and
header information is not always used. Forcing the user to unmarshal a
header structure is counterintuitive and therefore it is now an optional
value returned by RawToken.Decode.
  • Loading branch information
Gabriel Sanches committed Mar 6, 2019
1 parent c280ec7 commit 333e196
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
7 changes: 2 additions & 5 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ func BenchmarkVerify(b *testing.B) {
if err = raw.Verify(hs256); err != nil {
b.Fatal(err)
}
var (
h Header
p benchPayload
)
if err = raw.Decode(&h, &p); err != nil {
var p benchPayload
if _, err = raw.Decode(&p); err != nil {
b.Fatal(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func testJWT(t *testing.T, testCases []testCase) {
h2 Header
tp2 testPayload
)
err = raw.Decode(&h2, &tp2)
h2, err = raw.Decode(&tp2)
if want, got := tc.decodingErr, err; want != got {
t.Errorf("want %v, got %v", want, got)
}
Expand Down
15 changes: 7 additions & 8 deletions raw_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,32 @@ func Parse(token []byte) (RawToken, error) {
}

// Decode decodes a raw JWT into a header and a payload.
func (r RawToken) Decode(h *Header, payload interface{}) error {
func (r RawToken) Decode(payload interface{}) (h Header, err error) {
// Next, unmarshal the token accordingly.
var (
enc []byte // encoded header/payload
dec []byte // decoded header/payload
encoding = base64.RawURLEncoding
err error
)
// Header.
enc = r.header()
dec = make([]byte, encoding.DecodedLen(len(enc)))
if _, err = encoding.Decode(dec, enc); err != nil {
return err
return
}
if err = json.Unmarshal(dec, h); err != nil {
return err
if err = json.Unmarshal(dec, &h); err != nil {
return
}
// Claims.
enc = r.claims()
dec = make([]byte, encoding.DecodedLen(len(enc)))
if _, err = encoding.Decode(dec, enc); err != nil {
return err
return
}
if err = json.Unmarshal(dec, payload); err != nil {
return err
return
}
return nil
return
}

// Verify verifies a JWT signature with a verifying method that
Expand Down

0 comments on commit 333e196

Please sign in to comment.