Skip to content

Commit

Permalink
tweak jwt error
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisuke Maki committed Oct 24, 2024
1 parent ca69c8b commit 5cf5e3a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Changes-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ These are changes that are incompatible with the v2.x.x version.
* Validation used to work for `iat`, `nbf`, `exp` fields where these fields were
set to the explicit time.Time{} zero value, but now the _presence_ of these fields matter.

* `jwt.ErrInvalidJWT` has been renamed to `jwt.UnknownPayloadTypeError` to better reflect
what the error means

## JWS

* Iterators have been completely removed.
Expand Down
10 changes: 10 additions & 0 deletions jwt/errors.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package jwt

import (
"errors"
"fmt"
)

var errUnknownPayloadType = errors.New(`unknown payload type (payload is not JWT?)`)

// UnknownPayloadTypeError returns the opaque error value that is returned when
// `jwt.Parse` fails due to not being able to deduce the format of
// the incoming buffer
func UnknownPayloadTypeError() error {
return errUnknownPayloadType
}

type parseError struct {
error
}
Expand Down
12 changes: 1 addition & 11 deletions jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package jwt

import (
"bytes"
"errors"
"fmt"
"io"
"sync/atomic"
Expand All @@ -17,15 +16,6 @@ import (
"github.com/lestrrat-go/jwx/v3/jwt/internal/types"
)

var errInvalidJWT = errors.New(`invalid JWT`)

// ErrInvalidJWT returns the opaque error value that is returned when
// `jwt.Parse` fails due to not being able to deduce the format of
// the incoming buffer
func ErrInvalidJWT() error {
return errInvalidJWT
}

// Settings controls global settings that are specific to JWTs.
func Settings(options ...GlobalOption) {
var flattenAudience bool
Expand Down Expand Up @@ -295,7 +285,7 @@ OUTER:

break OUTER
case jwx.InvalidFormat:
return nil, ErrInvalidJWT()
return nil, UnknownPayloadTypeError()
case jwx.UnknownFormat:
// "Unknown" may include invalid JWTs, for example, those who lack "aud"
// claim. We could be pedantic and reject these
Expand Down
2 changes: 1 addition & 1 deletion jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,7 @@ func TestGH850(t *testing.T) {
var testToken = `eyJhbGciOiJFUzI1NiJ9.eyJzdWIiOiJ0ZXN0IiwiaWF0IjoxNjY2MDkxMzczLCJmb28iOiJiYXIifQ.3GWevx1z2_uCBB9Vj-D0rsT_CMsMeP9GP2rEqGDWpesoG8nHEjAXJOEQV1jOVkkCtTnS18JhcQdb7dW4i-zmqg.trailing-rubbish`

_, err := jwt.Parse([]byte(testToken), jwt.WithVerify(false))
require.True(t, errors.Is(err, jwt.ErrInvalidJWT()))
require.True(t, errors.Is(err, jwt.UnknownPayloadTypeError()))
}

func TestGH888(t *testing.T) {
Expand Down

0 comments on commit 5cf5e3a

Please sign in to comment.