jwt-go icon indicating copy to clipboard operation
jwt-go copied to clipboard

Problem with jwt.ParseWithClaims

Open bonjourclaudio opened this issue 4 years ago • 8 comments

Hi!

I Implemented an JWT-Middleware according to a GitHub Repo I have found which is based on the official documentation:

func JwtVerify(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var tk = r.Header.Get("x-access-token") // Grab the token from the header

		tk = strings.TrimSpace(tk)

		if tk == "" {
			// Token is missing, returns with error code 403 Unauthorized
			w.WriteHeader(http.StatusForbidden)
			json.NewEncoder(w).Encode(Exception{Message: "Missing auth token"})
			return
		}
		claims := &models.Token{} // models.Token{} is my custom claims struct

		_, err := jwt.ParseWithClaims(tk, claims, func(token *jwt.Token) (interface{}, error) {
			return []byte("secret"), nil
		})

		if err != nil {
			w.WriteHeader(http.StatusForbidden)
			json.NewEncoder(w).Encode(Exception{Message: err.Error()})
			return
		}

		ctx := context.WithValue(r.Context(), "user", claims)
		next.ServeHTTP(w, r.WithContext(ctx))
	})
}

When I run the program, the following error occurs:

auth/middleware.go:30:45: cannot use func literal (type func(*jwt.Token) (interface {}, error)) as type jwt.Keyfunc in argument to jwt.ParseWithClaims

My IDE also shows an error:

Cannot use 'func(token *jwt.Token) (interface{}, error)' (type func(token *jwt.Token) (interface{}, error)) as type Keyfunc

I have imported the JWT (and all other packages) via Dep.

Has anyone come across this problem before?

Thanks & best regards

bonjourclaudio avatar Sep 25 '19 13:09 bonjourclaudio

I just implemented a similar thing. The docs are pretty straightforward. https://godoc.org/github.com/dgrijalva/jwt-go#example-ParseWithClaims--CustomClaimsType

It's a little unclear what var claims is in your code. Looking at the source, I'm guessing you can only pass a pointer of the required object to ParseWithClaims, e.g.: jwt.ParseWithClaims(tk, &models.Token{}, func(...){...} and then use type assertion to assign the final var.

vpmv avatar Oct 07 '19 11:10 vpmv

I just implemented a similar thing. The docs are pretty straightforward. https://godoc.org/github.com/dgrijalva/jwt-go#example-ParseWithClaims--CustomClaimsType

It's a little unclear what var claims is in your code. Looking at the source, I'm guessing you can only pass a pointer of the required object to ParseWithClaims, e.g.: jwt.ParseWithClaims(tk, &models.Token{}, func(...){...} and then use type assertion to assign the final var.

var claims is just a variable of the pointer I am passing in. It consists of the following struct (models.Token{}):

type Token struct { UserID uint Firstname string Lastname string Email string *jwt.StandardClaims } The naming is confusing, sorry for that.

I am pretty sure that the implementation itself is correct.

I assume that this problem has something to do with the packages / imports or something...

bonjourclaudio avatar Oct 07 '19 11:10 bonjourclaudio

Perhaps. I'm on go 1.12 using modules; It grabbed github.com/dgrijalva/jwt-go v3.2.0+incompatible Hope this helps.

vpmv avatar Oct 07 '19 12:10 vpmv

Perhaps. I'm on go 1.12 using modules; It grabbed github.com/dgrijalva/jwt-go v3.2.0+incompatible Hope this helps.

I was using Go V1.13 and jwt-go V3.2.0... Problem still persists on 1.12.

bonjourclaudio avatar Oct 08 '19 06:10 bonjourclaudio

This just works with or without modules, go1.12 or go1.13. Any error here is likely to be from the calling program.

fredbi avatar Oct 12 '19 15:10 fredbi

I am also getting same error. token, err := jwt.ParseWithClaims(tokenString, &Model.Token{}, func(token *jwt.Token) (interface{}, error) { return key, nil }) get an error saying "cannot use func literal (type func(*jwt.Token) (interface {}, error)) as type jwt.Keyfunc in argument to jwt.ParseWithClaims" what should i do?

sanjushahgupta avatar Dec 27 '20 14:12 sanjushahgupta

For me the solution was to use github.com/form3tech-oss/jwt-go instead of github.com/dgrijalva/jwt-go

koszonetdoktor avatar Jan 08 '21 11:01 koszonetdoktor

For me the solution was to use github.com/form3tech-oss/jwt-go instead of github.com/dgrijalva/jwt-go

Yep! That worked. Thanks

hughsheehy avatar Jun 07 '21 13:06 hughsheehy