The token was signed by RSA algorithm that uses a private key to sign and a public key to verify. Store your public key to the files system and use jwt.SigningMethodRS256.Verify()
to verify. As the following snippet:
package main
import (
"fmt"
"strings"
"log"
"io/ioutil"
jwt "github.com/dgrijalva/jwt-go"
)
func main() {
publicKeyPath := "~/public_key.key"
token := "your_jwt_token_here"
if isValid, err := verifyToken(token, publicKeyPath)
if err != nil {
log.Fatal(err)
}
if isValid {
fmt.Println("The token is valid")
} else {
fmt.Println("The token is invalid")
}
}
func verifyToken(token, publicKeyPath string) (bool, error) {
keyData, err := ioutil.ReadFile(publicKeyPath)
if err != nil {
return false, err
}
key, err := jwt.ParseRSAPublicKeyFromPEM(keyData)
if err != nil {
return false, err
}
parts := strings.Split(token, ".")
err = jwt.SigningMethodRS256.Verify(strings.Join(parts[0:2], "."), parts[2], key)
if err != nil {
return false, nil
}
return true, nil
}