JWT key is invalid

7.9k Views Asked by At

I am following this example https://www.youtube.com/watch?v=eVlxuST7dCA to make a jwt auth. When I run the code below I get "Key is invalid" error. When I try printing tokenString it is empty. The GitHub to this sample is https://github.com/potatogopher/jwt-go-example/blob/master/server.go Why am I getting invalid error?

var privateKey []byte
privateKey, err := ioutil.ReadFile("demo.rsa")

token := jwt.New(jwt.GetSigningMethod("RS256"))
tokenString, err := token.SignedString(privateKey)

fmt.Println("TOKEN:", tokenString)
2

There are 2 best solutions below

2
On BEST ANSWER

I think the example code you're referring to uses an outdated API of jwt-go. The RS256 signing method requires the key to be a rsa.PrivateKey and not a byte buffer. This means, that the private key first has to be parsed using the jwt.ParseRSAPrivateKeyFromPEMfunction.

I've updated your example below:

func main() {
    tokenString, err := createSignedTokenString()
    if err != nil {
        panic(err)
    }
    fmt.Printf("Signed token string:\n%v\n", tokenString)

    token, err := parseTokenFromSignedTokenString(tokenString)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Parsed token valid = %v, raw token:\n%v\n", token.Valid, token.Raw)
}

func createSignedTokenString() (string, error) {
    privateKey, err := ioutil.ReadFile("demo.rsa")
    if err != nil {
        return "", fmt.Errorf("error reading private key file: %v\n", err)
    }

    key, err := jwt.ParseRSAPrivateKeyFromPEM(privateKey)
    if err != nil {
        return "", fmt.Errorf("error parsing RSA private key: %v\n", err)
    }

    token := jwt.New(jwt.SigningMethodRS256)
    tokenString, err := token.SignedString(key)
    if err != nil {
        return "", fmt.Errorf("error signing token: %v\n", err)
    }

    return tokenString, nil
}

func parseTokenFromSignedTokenString(tokenString string) (*jwt.Token, error) {
    publicKey, err := ioutil.ReadFile("demo.rsa.pub")
    if err != nil {
        return nil, fmt.Errorf("error reading public key file: %v\n", err)
    }

    key, err := jwt.ParseRSAPublicKeyFromPEM(publicKey)
    if err != nil {
        return nil, fmt.Errorf("error parsing RSA public key: %v\n", err)
    }

    parsedToken, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
        if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
            return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
        }
        return key, nil
    })
    if err != nil {
        return nil, fmt.Errorf("error parsing token: %v", err)
    }

    return parsedToken, nil
}
2
On

You need to create the private key with this command: openssl genrsa -out demo.rsa

If you dont want to do that, you can also use the hmac signing method where you only have to supply a secret key/string.

Example:

key := []byte("test")

token := jwt.New(jwt.SigningMethodHS256)
tokenString, err := token.SignedString(key)

fmt.Println("TOKEN:", tokenString)