jwt-go
jwt-go copied to clipboard
Problem with jwt.ParseWithClaims
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
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.
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...
Perhaps. I'm on go 1.12 using modules; It grabbed github.com/dgrijalva/jwt-go v3.2.0+incompatible Hope this helps.
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.
This just works with or without modules, go1.12 or go1.13. Any error here is likely to be from the calling program.
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?
For me the solution was to use github.com/form3tech-oss/jwt-go
instead of github.com/dgrijalva/jwt-go
For me the solution was to use
github.com/form3tech-oss/jwt-go
instead ofgithub.com/dgrijalva/jwt-go
Yep! That worked. Thanks