func TestRSAKeyParsing(t *testing.T) { key, _ := ioutil.ReadFile("test/sample_key") pubKey, _ := ioutil.ReadFile("test/sample_key.pub") badKey := []byte("All your base are belong to key") // Test parsePrivateKey if _, e := jwt.ParseRSAPrivateKeyFromPEM(key); e != nil { t.Errorf("Failed to parse valid private key: %v", e) } if k, e := jwt.ParseRSAPrivateKeyFromPEM(pubKey); e == nil { t.Errorf("Parsed public key as valid private key: %v", k) } if k, e := jwt.ParseRSAPrivateKeyFromPEM(badKey); e == nil { t.Errorf("Parsed invalid key as valid private key: %v", k) } // Test parsePublicKey if _, e := jwt.ParseRSAPublicKeyFromPEM(pubKey); e != nil { t.Errorf("Failed to parse valid public key: %v", e) } if k, e := jwt.ParseRSAPublicKeyFromPEM(key); e == nil { t.Errorf("Parsed private key as valid public key: %v", k) } if k, e := jwt.ParseRSAPublicKeyFromPEM(badKey); e == nil { t.Errorf("Parsed invalid key as valid private key: %v", k) } }
func LoadRSAPublicKeyFromDisk(location string) *rsa.PublicKey { keyData, e := ioutil.ReadFile(location) if e != nil { panic(e.Error()) } key, e := jwt.ParseRSAPublicKeyFromPEM(keyData) if e != nil { panic(e.Error()) } return key }
func TestRSAVerifyWithPreParsedPrivateKey(t *testing.T) { key, _ := ioutil.ReadFile("test/sample_key.pub") parsedKey, err := jwt.ParseRSAPublicKeyFromPEM(key) if err != nil { t.Fatal(err) } testData := rsaTestData[0] parts := strings.Split(testData.tokenString, ".") err = jwt.SigningMethodRS256.Verify(strings.Join(parts[0:2], "."), parts[2], parsedKey) if err != nil { t.Errorf("[%v] Error while verifying key: %v", testData.name, err) } }
func TestRSAVerify(t *testing.T) { keyData, _ := ioutil.ReadFile("test/sample_key.pub") key, _ := jwt.ParseRSAPublicKeyFromPEM(keyData) for _, data := range rsaTestData { parts := strings.Split(data.tokenString, ".") method := jwt.GetSigningMethod(data.alg) err := method.Verify(strings.Join(parts[0:2], "."), parts[2], key) if data.valid && err != nil { t.Errorf("[%v] Error while verifying key: %v", data.name, err) } if !data.valid && err == nil { t.Errorf("[%v] Invalid key passed validation", data.name) } } }