Skip to content

Commit 333e196

Browse files
author
Gabriel Sanches
committed
Change RawToken.Decode signature
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.
1 parent c280ec7 commit 333e196

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

benchmark_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,8 @@ func BenchmarkVerify(b *testing.B) {
5252
if err = raw.Verify(hs256); err != nil {
5353
b.Fatal(err)
5454
}
55-
var (
56-
h Header
57-
p benchPayload
58-
)
59-
if err = raw.Decode(&h, &p); err != nil {
55+
var p benchPayload
56+
if _, err = raw.Decode(&p); err != nil {
6057
b.Fatal(err)
6158
}
6259
}

jwt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func testJWT(t *testing.T, testCases []testCase) {
9393
h2 Header
9494
tp2 testPayload
9595
)
96-
err = raw.Decode(&h2, &tp2)
96+
h2, err = raw.Decode(&tp2)
9797
if want, got := tc.decodingErr, err; want != got {
9898
t.Errorf("want %v, got %v", want, got)
9999
}

raw_token.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,32 @@ func Parse(token []byte) (RawToken, error) {
3939
}
4040

4141
// Decode decodes a raw JWT into a header and a payload.
42-
func (r RawToken) Decode(h *Header, payload interface{}) error {
42+
func (r RawToken) Decode(payload interface{}) (h Header, err error) {
4343
// Next, unmarshal the token accordingly.
4444
var (
4545
enc []byte // encoded header/payload
4646
dec []byte // decoded header/payload
4747
encoding = base64.RawURLEncoding
48-
err error
4948
)
5049
// Header.
5150
enc = r.header()
5251
dec = make([]byte, encoding.DecodedLen(len(enc)))
5352
if _, err = encoding.Decode(dec, enc); err != nil {
54-
return err
53+
return
5554
}
56-
if err = json.Unmarshal(dec, h); err != nil {
57-
return err
55+
if err = json.Unmarshal(dec, &h); err != nil {
56+
return
5857
}
5958
// Claims.
6059
enc = r.claims()
6160
dec = make([]byte, encoding.DecodedLen(len(enc)))
6261
if _, err = encoding.Decode(dec, enc); err != nil {
63-
return err
62+
return
6463
}
6564
if err = json.Unmarshal(dec, payload); err != nil {
66-
return err
65+
return
6766
}
68-
return nil
67+
return
6968
}
7069

7170
// Verify verifies a JWT signature with a verifying method that

0 commit comments

Comments
 (0)