diff --git a/parse.go b/parse.go index fa575aa..a44f026 100644 --- a/parse.go +++ b/parse.go @@ -4,12 +4,17 @@ import ( "bytes" "encoding/base64" "encoding/json" + "errors" ) // Parse decodes a token and verifies it's signature. func Parse(raw []byte, verifier Verifier) (*Token, error) { token, err := ParseNoVerify(raw) if err != nil { + // See: https://github.com/cristalhq/jwt/issues/147 + if errors.Is(err, ErrNotJWTType) { + return token, ErrNotJWTType + } return nil, err } if err := verifier.Verify(token); err != nil { @@ -78,7 +83,8 @@ func parse(token []byte) (*Token, error) { claims: claims, } if !constTimeEqual(tk.header.Type, "JWT") { - return nil, ErrNotJWTType + // See: https://github.com/cristalhq/jwt/issues/147 + return tk, ErrNotJWTType } return tk, nil } diff --git a/parse_test.go b/parse_test.go index 4bdf9ef..1f2992f 100644 --- a/parse_test.go +++ b/parse_test.go @@ -53,8 +53,11 @@ func TestParseWrongType(t *testing.T) { const tokenHS256 = `eyJhbGciOiJIUzI1NiIsInR5cCI6IkJPTUJPTSJ9.eyJqdGkiOiJqdXN0IGFuIGlkIiwiYXVkIjoiYXVkaWVuY2UifQ.t5oEdZGp0Qbth7lo5fZlV_o4-r9gMoYBSktXbarjWoo` verifier := must(NewVerifierHS(HS256, []byte("key"))) - _, err := Parse([]byte(tokenHS256), verifier) + token, err := Parse([]byte(tokenHS256), verifier) mustEqual(t, err, ErrNotJWTType) + if token == nil { + t.Fatal() + } } func TestParseMalformed(t *testing.T) {