Skip to content

Commit

Permalink
Return token even if ErrNotJWTType (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Nov 8, 2023
1 parent 76a776d commit a8f09b7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 7 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a8f09b7

Please sign in to comment.