From 333e19651a66ad6b31accf72113b74f7f9180495 Mon Sep 17 00:00:00 2001 From: Gabriel Sanches Date: Wed, 6 Mar 2019 17:31:23 -0300 Subject: [PATCH] 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. --- benchmark_test.go | 7 ++----- jwt_test.go | 2 +- raw_token.go | 15 +++++++-------- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/benchmark_test.go b/benchmark_test.go index 33250e3..f3231dd 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -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) } } diff --git a/jwt_test.go b/jwt_test.go index fa526b7..19a6897 100644 --- a/jwt_test.go +++ b/jwt_test.go @@ -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) } diff --git a/raw_token.go b/raw_token.go index 9c63438..7263b4b 100644 --- a/raw_token.go +++ b/raw_token.go @@ -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