Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return token even if ErrNotJWTType #148

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading