golang-jwt / jwt

Community maintained clone of https://github.com/dgrijalva/jwt-go

Home Page:https://golang-jwt.github.io/jwt/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Request for ValidationError Support in Version 5

LieLieLiekey opened this issue · comments

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

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?

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
}

oh, i got it, thanks very much!