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

Request for ValidationError Support in Version 5 #321

Closed
LieLieLiekey opened this issue Jun 21, 2023 · 3 comments
Closed

Request for ValidationError Support in Version 5 #321

LieLieLiekey opened this issue Jun 21, 2023 · 3 comments

Comments

@LieLieLiekey
Copy link

LieLieLiekey commented Jun 21, 2023

Hello golang-jwt maintainers,

I am currently using your open-source golang-jwt library and it has been a great help in my projects. However, I have noticed that the latest version, v5, does not support ValidationError. This has been inconvenient for the callers to identify the errors or handle error codes.

Considering the practical utility that this feature offers, I would like to propose the addition of ValidationError support to this version. I believe this would greatly improve the error handling capabilities of your library and would be beneficial to many developers.

I see that this has been discussed in a previous issue: #125. I kindly urge you to reconsider the implementation of this feature for the betterment of this library.

Thank you for your time and for considering this request. I appreciate your efforts in maintaining this library.

Best regards

@LieLieLiekey
Copy link
Author

version v5 used the joinedError type

Alternatively, is there any way to identify whether a certain error, such as ErrTokenExpired, is included within the joinedError?

@oxisto
Copy link
Collaborator

oxisto commented Jun 21, 2023

You should be able to use errors.Is with the specific error that you want. This should work independently whether a joined error or the old validation error is used. See the following example:

jwt/example_test.go

Lines 158 to 181 in 33d62b4

func ExampleParse_errorChecking() {
// Token from another example. This token is expired
var tokenString = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJleHAiOjE1MDAwLCJpc3MiOiJ0ZXN0In0.HE7fK0xOQwFEr4WDgRWj4teRPZ6i3GLwD5YCm6Pwu_c"
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return []byte("AllYourBase"), nil
})
if token.Valid {
fmt.Println("You look nice today")
} else if errors.Is(err, jwt.ErrTokenMalformed) {
fmt.Println("That's not even a token")
} else if errors.Is(err, jwt.ErrTokenSignatureInvalid) {
// Invalid signature
fmt.Println("Invalid signature")
} else if errors.Is(err, jwt.ErrTokenExpired) || errors.Is(err, jwt.ErrTokenNotValidYet) {
// Token is either expired or not active yet
fmt.Println("Timing is everything")
} else {
fmt.Println("Couldn't handle this token:", err)
}
// Output: Timing is everything
}

@LieLieLiekey
Copy link
Author

oh, i got it, thanks very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants