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

Problem with jwt.ParseWithClaims #1

Open
bonjourclaudio opened this issue Sep 25, 2019 · 0 comments
Open

Problem with jwt.ParseWithClaims #1

bonjourclaudio opened this issue Sep 25, 2019 · 0 comments

Comments

@bonjourclaudio
Copy link

bonjourclaudio commented Sep 25, 2019

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

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

1 participant