Skip to content
This repository has been archived by the owner on May 21, 2022. It is now read-only.

key is invalid error with RS256 generated from https://mkjwk.org/ #488

Open
prakharmishra opened this issue Apr 1, 2022 · 0 comments
Open

Comments

@prakharmishra
Copy link

I am using the library like this:

package main

import (
	"fmt"
	"time"

	"github.com/dgrijalva/jwt-go"
)

var jwtKey = []byte(`
{
    "p": "7KKWYO2Mdmjm1eIAjpMOtcjfYlslYVthVVecxJyq4sr9v9HUxmN-VWCJaDBBb9iTm8zCoRNauiNhgk4vViK56T6Ooo93GHiHsVtpIB9MgL1NZuHoHZtQL6iq1faRp2A5Xca8TrcxQ4k8snHpndjjnrQj1XtvEESzL23lu-S-280",
    "kty": "RSA",
    "q": "rr-eu9klZeYH8D3DZ6OmBlVFCi7dFHx9uepcrHOlxeZJBg9y5882X2Uw_z4OfIBUHNux0yKXkkHhehiM7gSxb7m75ap54LWG35q9JGU2qI_DvlvEgMObqz-YER4satI7LPbIMISHUyqkcN86SA9olVLNK3kDEsukx_LcnKs7bxc",
    "d": "QxO-9QTZtZ7oHIifMBlFInIstI8CYdVobLhbl_OFA8vTygX_ZG5-i-REYaYUJyx_WwHYYI8HsWmbZsrf2rc7Y76E2OCRGuyEK4lA-YeP2FYX09hwuIvYTXc_KErenp6rbo8W0EU49FIYdTk7Szlx9RF-gatOZXVJqM-nHnh3sHm3facnBba94_2L91eIKezJ1VeuWwxpR2sRZzbQdrOI_sPYurIdLZGeTGYF7kKIcgyMydxchRpiKKapXm--sPewNlzw7NMMU5PLXKIc9YA8rKU2G1OgRxVOrleeKiK4zLZY46ry60Lu2zzwLTQL6uyiGIG0f9hp0TYZY8qedShS-Q",
    "e": "AQAB",
    "use": "sig",
    "kid": "sdfasda",
    "qi": "dsVrwVQnK9WigTluBTq5DTHkbRoeKhp67y8i4LaE5hst3vSd_VrFB1aYhJuHD-c0B8cBt-QQbqN7sUit7OiXS8u7iIoIuTTsS0vBexpJpPMv-GGOQnBrc0pyFvLX_HjJqZRBH4ZsBmI4Zfn6E0NCjEuSPfksxYYz4Df62T9jrgM",
    "dp": "tE7bCPZYziz2n0i7JehWEBwEYrySyhFIJDBDCulZqMAGA_COEbDkJYgOi24hnmjHaLLoJrZJroWhGhobJaYGRPze0G1C0UmeE31UqB5RO9OCs_80z8J1oisCKVDdAU1nyNXSzKP4DL74mfwEh7spDdezakrIgPvoER7LK5WL_I0",
    "alg": "RS256",
    "dq": "K79vJsoDEdKX1C4yOEUA8H7ybM00rcdI1n10u_ur2bKAP5Moih4XF6TGsm-_wq2B4UOi7h-v4H67ywxQY9oq7bSK9MFMB9SKMnqTtYPdPi_XqlVhCXdvBl1CXa63IfsFs4kIrxTOqCR5zIQmHBo3bYKwOJzwBwmSdDg5wMMhevs",
    "n": "oYeiPhASwtqH9RhjfaEPxaw3j-iWIwwDvrnIaPgUT9A175fJ10mKWJ4butQPXQJKcGHcOJ88pftcXe_KBW2sGj5VYyFiPzVpU3C5TFITbtseTEo0VCNDisow2no4BVFDj2h95EA9WGwbktyqE0C4Dc2-_iBXJlQuOY_RBlOlEOxRzxEBxMOiolN9X2CfqJ6DpFKR3wr58pbaYGgdzTL6dRA_aJ7P-V7x8_IYFGYqwYjThIXvWOGwWaSR6Q4iA8pwuNOPegJeFxPom0u67vMZ65EU_c0WH4kv8ucqQq7g_GgL2IDmWaRYykiy6OE8G-hHAGIW898IoyHpkW3K5bKiaw"
}
`)

type Claims struct {
	Username string `json:"username"`
	jwt.StandardClaims
}

func main() {
	expirationTime := time.Now().Add(5 * time.Minute)

	claims := &Claims{
		Username: "prakhar",
		StandardClaims: jwt.StandardClaims{
			// In JWT, the expiry time is expressed as unix milliseconds
			ExpiresAt: expirationTime.Unix(),
		},
	}

	// Declare the token with the algorithm used for signing, and the claims
	token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
	// Create the JWT string
	tokenString, err := token.SignedString(jwtKey)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(tokenString)

	tkn, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
		return jwtKey, nil
	})
	if err != nil {
		if err == jwt.ErrSignatureInvalid {
			fmt.Println(err)
			return
		}
		fmt.Println(err)
	}
	if !tkn.Valid {
		fmt.Println(err)
		return
	}

	// Finally, return the welcome message to the user, along with their
	// username given in the token
	fmt.Printf("Welcome %s!", claims.Username)
}

And its not able to use the key. It fails saying key is invalid. Could you please look into this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant