Go and parsing a marker with jwt-go

Can someone tell me why the following (from https://github.com/dgrijalva/jwt-go ) example does not work?

token, err := jwt.Parse(myToken, func(token *jwt.Token) ([]byte, error) { return myLookupKey(token.Header["kid"]) }) if err == nil && token.Valid { deliverGoodness("!") } else { deliverUtterRejection(":(") } 

I get the error message "I can not use the func literal (type func (* jwt.Token) ([] byte, error)) as the type jwt.Keyfunc in the jwt.Parse argument"

I tried to use the code from several different jwt-go examples, but always got the same error.

+5
source share
1 answer

Parse function expects

 type Keyfunc func(*Token) (interface{}, error) 

You need to return interface{} , not byte[] in your function literal.
(perhaps using byte.Buffer to wrap byte[] , which you can then read as in Converting an arbitrary Golang interface to a byte array ")

Gert Kuykens points to comments on issue 36 : commit e1571c8 should update the example.
Other examples, such as this gist , also need to be updated.

+6
source

Source: https://habr.com/ru/post/1202590/


All Articles