auth
auth copied to clipboard
Problem with jwt.ParseWithClaims
I Implemented the Jwt-Middleware (JwtVerify(...)) the same way as you did:
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{}
_, 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.
Have you come across this problem before?
Thank you & best regards