How to check json web token with jwt-go library?

I am using the jwt-go library in golang and using the HS512 algorithm to sign a token. I want to make sure that the token is valid, and the example in the docs looks like this:

token, err := jwt.Parse(myToken, func(token *jwt.Token) (interface{}, error) {
    return myLookupKey(token.Header["kid"])
})

if err == nil && token.Valid {
    fmt.Println("Your token is valid.  I like your style.")
} else {
    fmt.Println("This token is terrible!  I cannot accept this.")
}

I understand that myTokenthis is a string token, but keyFuncreceives the transferred token, but I don’t understand what the function myLookupKeyshould do ?, and it token.Headerdoesn’t kidmatter when I print it to the console and even thought that the token has all the data that I’m in I insert it, token.Validalways a lie. This is mistake? How to confirm that the token is valid?

+4
source share
1 answer

, keyFunc , . .

( RFC 7519) , jwt-go, kid (short ), , . ( ) ( , ).

, . keyFunc :

token, err := jwt.Parse(myToken, func(token *jwt.Token) (interface{}, error) {
    key, err := ioutil.ReadFile("your-private-key.pem")
    if err != nil {
        return nil, errors.New("private key could not be loaded")
    }
    return key, nil
})
+4

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


All Articles