-
Notifications
You must be signed in to change notification settings - Fork 389
More consistent way of handling validation errors #274
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0c7de75
More consistent way of handling composite errors
oxisto 88bd064
Running in Go 1.20 only
oxisto a9047d5
All the error goodness even without Go 1.20
oxisto 3efe89d
Building again on Go 1.28
oxisto e237304
Removed old error bitfields
oxisto 84a369b
Verify functions now return errors instead of bool
oxisto 1cf3c71
More validation testing
oxisto 4b346ca
Added malformed claims JSON
oxisto 04260dc
Reverted go.mod
oxisto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//go:build go1.20 | ||
// +build go1.20 | ||
|
||
package jwt | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Unwrap implements the multiple error unwrapping for this error type, which is | ||
// possible in Go 1.20. | ||
func (je joinedError) Unwrap() []error { | ||
return je.errs | ||
} | ||
|
||
// newError creates a new error message with a detailed error message. The | ||
// message will be prefixed with the contents of the supplied error type. | ||
// Additionally, more errors, that provide more context can be supplied which | ||
// will be appended to the message. This makes use of Go 1.20's possibility to | ||
// include more than one %w formatting directive in [fmt.Errorf]. | ||
// | ||
// For example, | ||
// | ||
// newError("no keyfunc was provided", ErrTokenUnverifiable) | ||
// | ||
// will produce the error string | ||
// | ||
// "token is unverifiable: no keyfunc was provided" | ||
func newError(message string, err error, more ...error) error { | ||
oxisto marked this conversation as resolved.
Show resolved
Hide resolved
oxisto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var format string | ||
var args []any | ||
if message != "" { | ||
format = "%w: %s" | ||
args = []any{err, message} | ||
} else { | ||
format = "%w" | ||
args = []any{err} | ||
} | ||
|
||
for _, e := range more { | ||
format += ": %w" | ||
args = append(args, e) | ||
} | ||
|
||
err = fmt.Errorf(format, args...) | ||
return err | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//go:build !go1.20 | ||
// +build !go1.20 | ||
|
||
package jwt | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Is implements checking for multiple errors using [errors.Is], since multiple | ||
// error unwrapping is not possible in versions less than Go 1.20. | ||
func (je joinedError) Is(err error) bool { | ||
for _, e := range je.errs { | ||
if errors.Is(e, err) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// wrappedErrors is a workaround for wrapping multiple errors in environments | ||
// where Go 1.20 is not available. It basically uses the already implemented | ||
// functionatlity of joinedError to handle multiple errors with supplies a | ||
// custom error message that is identical to the one we produce in Go 1.20 using | ||
// multiple %w directives. | ||
type wrappedErrors struct { | ||
msg string | ||
joinedError | ||
} | ||
|
||
// Error returns the stored error string | ||
func (we wrappedErrors) Error() string { | ||
return we.msg | ||
} | ||
|
||
// newError creates a new error message with a detailed error message. The | ||
// message will be prefixed with the contents of the supplied error type. | ||
// Additionally, more errors, that provide more context can be supplied which | ||
// will be appended to the message. Since we cannot use of Go 1.20's possibility | ||
// to include more than one %w formatting directive in [fmt.Errorf], we have to | ||
// emulate that. | ||
// | ||
// For example, | ||
// | ||
// newError("no keyfunc was provided", ErrTokenUnverifiable) | ||
// | ||
// will produce the error string | ||
// | ||
// "token is unverifiable: no keyfunc was provided" | ||
func newError(message string, err error, more ...error) error { | ||
// We cannot wrap multiple errors here with %w, so we have to be a little | ||
// bit creative. Basically, we are using %s instead of %w to produce the | ||
// same error message and then throw the result into a custom error struct. | ||
var format string | ||
var args []any | ||
if message != "" { | ||
format = "%s: %s" | ||
args = []any{err, message} | ||
} else { | ||
format = "%s" | ||
args = []any{err} | ||
} | ||
errs := []error{err} | ||
|
||
for _, e := range more { | ||
format += ": %s" | ||
args = append(args, e) | ||
errs = append(errs, e) | ||
} | ||
|
||
err = &wrappedErrors{ | ||
msg: fmt.Sprintf(format, args...), | ||
joinedError: joinedError{errs: errs}, | ||
} | ||
return err | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package jwt | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"testing" | ||
) | ||
|
||
func Test_joinErrors(t *testing.T) { | ||
type args struct { | ||
errs []error | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErrors []error | ||
wantMessage string | ||
}{ | ||
{ | ||
name: "multiple errors", | ||
args: args{ | ||
errs: []error{ErrTokenNotValidYet, ErrTokenExpired}, | ||
}, | ||
wantErrors: []error{ErrTokenNotValidYet, ErrTokenExpired}, | ||
wantMessage: "token is not valid yet, token is expired", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := joinErrors(tt.args.errs...) | ||
for _, wantErr := range tt.wantErrors { | ||
if !errors.Is(err, wantErr) { | ||
t.Errorf("joinErrors() error = %v, does not contain %v", err, wantErr) | ||
} | ||
} | ||
|
||
if err.Error() != tt.wantMessage { | ||
t.Errorf("joinErrors() error.Error() = %v, wantMessage %v", err, tt.wantMessage) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_newError(t *testing.T) { | ||
type args struct { | ||
message string | ||
err error | ||
more []error | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErrors []error | ||
wantMessage string | ||
}{ | ||
{ | ||
name: "single error", | ||
args: args{message: "something is wrong", err: ErrTokenMalformed}, | ||
wantMessage: "token is malformed: something is wrong", | ||
wantErrors: []error{ErrTokenMalformed}, | ||
}, | ||
{ | ||
name: "two errors", | ||
args: args{message: "something is wrong", err: ErrTokenMalformed, more: []error{io.ErrUnexpectedEOF}}, | ||
wantMessage: "token is malformed: something is wrong: unexpected EOF", | ||
wantErrors: []error{ErrTokenMalformed}, | ||
}, | ||
{ | ||
name: "two errors, no detail", | ||
args: args{message: "", err: ErrTokenInvalidClaims, more: []error{ErrTokenExpired}}, | ||
wantMessage: "token has invalid claims: token is expired", | ||
wantErrors: []error{ErrTokenInvalidClaims, ErrTokenExpired}, | ||
}, | ||
{ | ||
name: "two errors, no detail and join error", | ||
args: args{message: "", err: ErrTokenInvalidClaims, more: []error{joinErrors(ErrTokenExpired, ErrTokenNotValidYet)}}, | ||
wantMessage: "token has invalid claims: token is expired, token is not valid yet", | ||
wantErrors: []error{ErrTokenInvalidClaims, ErrTokenExpired, ErrTokenNotValidYet}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := newError(tt.args.message, tt.args.err, tt.args.more...) | ||
for _, wantErr := range tt.wantErrors { | ||
if !errors.Is(err, wantErr) { | ||
t.Errorf("newError() error = %v, does not contain %v", err, wantErr) | ||
} | ||
} | ||
|
||
if err.Error() != tt.wantMessage { | ||
t.Errorf("newError() error.Error() = %v, wantMessage %v", err, tt.wantMessage) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/golang-jwt/jwt/v5 | ||
|
||
go 1.16 | ||
go 1.18 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.