2014-12-28 23:24:41 +03:00
|
|
|
package jwt_test
|
2012-04-18 05:41:12 +04:00
|
|
|
|
|
|
|
import (
|
2022-05-28 02:11:16 +03:00
|
|
|
"os"
|
2012-04-18 05:41:12 +04:00
|
|
|
"strings"
|
2012-04-18 23:59:37 +04:00
|
|
|
"testing"
|
2021-05-29 04:26:41 +03:00
|
|
|
|
2021-08-03 16:51:01 +03:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
2012-04-18 05:41:12 +04:00
|
|
|
)
|
|
|
|
|
2012-04-18 23:59:37 +04:00
|
|
|
var rsaTestData = []struct {
|
|
|
|
name string
|
2012-04-18 05:41:12 +04:00
|
|
|
tokenString string
|
2014-07-06 02:25:29 +04:00
|
|
|
alg string
|
2012-04-18 23:59:37 +04:00
|
|
|
claims map[string]interface{}
|
|
|
|
valid bool
|
2012-04-18 05:41:12 +04:00
|
|
|
}{
|
|
|
|
{
|
2014-07-06 02:34:31 +04:00
|
|
|
"Basic RS256",
|
2012-04-18 05:41:12 +04:00
|
|
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.FhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
|
2014-07-06 02:25:29 +04:00
|
|
|
"RS256",
|
2012-04-18 05:41:12 +04:00
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
true,
|
|
|
|
},
|
2014-07-06 02:34:31 +04:00
|
|
|
{
|
|
|
|
"Basic RS384",
|
|
|
|
"eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIifQ.W-jEzRfBigtCWsinvVVuldiuilzVdU5ty0MvpLaSaqK9PlAWWlDQ1VIQ_qSKzwL5IXaZkvZFJXT3yL3n7OUVu7zCNJzdwznbC8Z-b0z2lYvcklJYi2VOFRcGbJtXUqgjk2oGsiqUMUMOLP70TTefkpsgqDxbRh9CDUfpOJgW-dU7cmgaoswe3wjUAUi6B6G2YEaiuXC0XScQYSYVKIzgKXJV8Zw-7AN_DBUI4GkTpsvQ9fVVjZM9csQiEXhYekyrKu1nu_POpQonGd8yqkIyXPECNmmqH5jH4sFiF67XhD7_JpkvLziBpI-uh86evBUadmHhb9Otqw3uV3NTaXLzJw",
|
|
|
|
"RS384",
|
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Basic RS512",
|
|
|
|
"eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIifQ.zBlLlmRrUxx4SJPUbV37Q1joRcI9EW13grnKduK3wtYKmDXbgDpF1cZ6B-2Jsm5RB8REmMiLpGms-EjXhgnyh2TSHE-9W2gA_jvshegLWtwRVDX40ODSkTb7OVuaWgiy9y7llvcknFBTIg-FnVPVpXMmeV_pvwQyhaz1SSwSPrDyxEmksz1hq7YONXhXPpGaNbMMeDTNP_1oj8DZaqTIL9TwV8_1wb2Odt_Fy58Ke2RVFijsOLdnyEAjt2n9Mxihu9i3PhNBkkxa2GbnXBfq3kzvZ_xxGGopLdHhJjcGWXO-NiwI9_tiu14NRv4L2xC0ItD9Yz68v2ZIZEp_DuzwRQ",
|
|
|
|
"RS512",
|
2012-04-18 05:41:12 +04:00
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"basic invalid: foo => bar",
|
|
|
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.EhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
|
2014-07-06 02:25:29 +04:00
|
|
|
"RS256",
|
2012-04-18 05:41:12 +04:00
|
|
|
map[string]interface{}{"foo": "bar"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-07-06 02:25:29 +04:00
|
|
|
func TestRSAVerify(t *testing.T) {
|
2022-05-28 02:11:16 +03:00
|
|
|
keyData, _ := os.ReadFile("test/sample_key.pub")
|
2015-07-20 20:23:11 +03:00
|
|
|
key, _ := jwt.ParseRSAPublicKeyFromPEM(keyData)
|
2012-04-18 23:59:37 +04:00
|
|
|
|
2012-04-18 23:18:31 +04:00
|
|
|
for _, data := range rsaTestData {
|
2012-04-18 05:41:12 +04:00
|
|
|
parts := strings.Split(data.tokenString, ".")
|
2012-04-18 23:59:37 +04:00
|
|
|
|
2014-12-28 23:24:41 +03:00
|
|
|
method := jwt.GetSigningMethod(data.alg)
|
2012-04-18 09:50:26 +04:00
|
|
|
err := method.Verify(strings.Join(parts[0:2], "."), parts[2], key)
|
2012-04-18 05:41:12 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2012-04-18 23:59:37 +04:00
|
|
|
}
|
2012-07-07 02:43:17 +04:00
|
|
|
|
2014-07-06 02:25:29 +04:00
|
|
|
func TestRSASign(t *testing.T) {
|
2022-05-28 02:11:16 +03:00
|
|
|
keyData, _ := os.ReadFile("test/sample_key")
|
2015-07-20 20:23:11 +03:00
|
|
|
key, _ := jwt.ParseRSAPrivateKeyFromPEM(keyData)
|
2012-07-07 02:43:17 +04:00
|
|
|
|
|
|
|
for _, data := range rsaTestData {
|
|
|
|
if data.valid {
|
|
|
|
parts := strings.Split(data.tokenString, ".")
|
2014-12-28 23:24:41 +03:00
|
|
|
method := jwt.GetSigningMethod(data.alg)
|
2012-07-07 02:43:17 +04:00
|
|
|
sig, err := method.Sign(strings.Join(parts[0:2], "."), key)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%v] Error signing token: %v", data.name, err)
|
|
|
|
}
|
|
|
|
if sig != parts[2] {
|
|
|
|
t.Errorf("[%v] Incorrect signature.\nwas:\n%v\nexpecting:\n%v", data.name, sig, parts[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-07 04:02:20 +04:00
|
|
|
}
|
2014-06-28 22:29:32 +04:00
|
|
|
|
2014-08-07 11:45:21 +04:00
|
|
|
func TestRSAVerifyWithPreParsedPrivateKey(t *testing.T) {
|
2022-05-28 02:11:16 +03:00
|
|
|
key, _ := os.ReadFile("test/sample_key.pub")
|
2014-12-28 23:24:41 +03:00
|
|
|
parsedKey, err := jwt.ParseRSAPublicKeyFromPEM(key)
|
2014-08-07 11:45:21 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testData := rsaTestData[0]
|
|
|
|
parts := strings.Split(testData.tokenString, ".")
|
2014-12-28 23:24:41 +03:00
|
|
|
err = jwt.SigningMethodRS256.Verify(strings.Join(parts[0:2], "."), parts[2], parsedKey)
|
2014-08-07 11:45:21 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%v] Error while verifying key: %v", testData.name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-06 14:13:23 +04:00
|
|
|
func TestRSAWithPreParsedPrivateKey(t *testing.T) {
|
2022-05-28 02:11:16 +03:00
|
|
|
key, _ := os.ReadFile("test/sample_key")
|
2014-12-28 23:24:41 +03:00
|
|
|
parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
|
2014-08-06 14:13:23 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testData := rsaTestData[0]
|
|
|
|
parts := strings.Split(testData.tokenString, ".")
|
2014-12-28 23:24:41 +03:00
|
|
|
sig, err := jwt.SigningMethodRS256.Sign(strings.Join(parts[0:2], "."), parsedKey)
|
2014-08-06 14:13:23 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%v] Error signing token: %v", testData.name, err)
|
|
|
|
}
|
|
|
|
if sig != parts[2] {
|
|
|
|
t.Errorf("[%v] Incorrect signature.\nwas:\n%v\nexpecting:\n%v", testData.name, sig, parts[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 02:50:46 +04:00
|
|
|
func TestRSAKeyParsing(t *testing.T) {
|
2022-05-28 02:11:16 +03:00
|
|
|
key, _ := os.ReadFile("test/sample_key")
|
|
|
|
secureKey, _ := os.ReadFile("test/privateSecure.pem")
|
|
|
|
pubKey, _ := os.ReadFile("test/sample_key.pub")
|
2014-06-28 22:29:32 +04:00
|
|
|
badKey := []byte("All your base are belong to key")
|
|
|
|
|
2014-07-06 02:58:07 +04:00
|
|
|
// Test parsePrivateKey
|
2014-12-28 23:24:41 +03:00
|
|
|
if _, e := jwt.ParseRSAPrivateKeyFromPEM(key); e != nil {
|
2014-06-28 22:29:32 +04:00
|
|
|
t.Errorf("Failed to parse valid private key: %v", e)
|
|
|
|
}
|
|
|
|
|
2014-12-28 23:24:41 +03:00
|
|
|
if k, e := jwt.ParseRSAPrivateKeyFromPEM(pubKey); e == nil {
|
2014-06-28 22:29:32 +04:00
|
|
|
t.Errorf("Parsed public key as valid private key: %v", k)
|
|
|
|
}
|
|
|
|
|
2014-12-28 23:24:41 +03:00
|
|
|
if k, e := jwt.ParseRSAPrivateKeyFromPEM(badKey); e == nil {
|
2014-06-28 22:29:32 +04:00
|
|
|
t.Errorf("Parsed invalid key as valid private key: %v", k)
|
|
|
|
}
|
2014-07-06 02:58:07 +04:00
|
|
|
|
2016-11-03 20:49:46 +03:00
|
|
|
if _, e := jwt.ParseRSAPrivateKeyFromPEMWithPassword(secureKey, "password"); e != nil {
|
|
|
|
t.Errorf("Failed to parse valid private key with password: %v", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
if k, e := jwt.ParseRSAPrivateKeyFromPEMWithPassword(secureKey, "123132"); e == nil {
|
|
|
|
t.Errorf("Parsed private key with invalid password %v", k)
|
|
|
|
}
|
|
|
|
|
2014-07-06 02:58:07 +04:00
|
|
|
// Test parsePublicKey
|
2014-12-28 23:24:41 +03:00
|
|
|
if _, e := jwt.ParseRSAPublicKeyFromPEM(pubKey); e != nil {
|
2014-07-06 02:58:07 +04:00
|
|
|
t.Errorf("Failed to parse valid public key: %v", e)
|
|
|
|
}
|
|
|
|
|
2014-12-28 23:24:41 +03:00
|
|
|
if k, e := jwt.ParseRSAPublicKeyFromPEM(key); e == nil {
|
2014-07-06 02:58:07 +04:00
|
|
|
t.Errorf("Parsed private key as valid public key: %v", k)
|
|
|
|
}
|
|
|
|
|
2014-12-28 23:24:41 +03:00
|
|
|
if k, e := jwt.ParseRSAPublicKeyFromPEM(badKey); e == nil {
|
2014-07-06 02:58:07 +04:00
|
|
|
t.Errorf("Parsed invalid key as valid private key: %v", k)
|
|
|
|
}
|
|
|
|
|
2014-06-28 22:29:32 +04:00
|
|
|
}
|
2015-04-11 23:53:09 +03:00
|
|
|
|
2021-08-03 23:57:36 +03:00
|
|
|
func BenchmarkRSAParsing(b *testing.B) {
|
2022-05-28 02:11:16 +03:00
|
|
|
key, _ := os.ReadFile("test/sample_key")
|
2021-08-03 23:57:36 +03:00
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
if _, err := jwt.ParseRSAPrivateKeyFromPEM(key); err != nil {
|
|
|
|
b.Fatalf("Unable to parse RSA private key: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-11 23:53:09 +03:00
|
|
|
func BenchmarkRS256Signing(b *testing.B) {
|
2022-05-28 02:11:16 +03:00
|
|
|
key, _ := os.ReadFile("test/sample_key")
|
2015-04-12 00:04:22 +03:00
|
|
|
parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
benchmarkSigning(b, jwt.SigningMethodRS256, parsedKey)
|
2015-04-11 23:53:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRS384Signing(b *testing.B) {
|
2022-05-28 02:11:16 +03:00
|
|
|
key, _ := os.ReadFile("test/sample_key")
|
2015-04-12 00:04:22 +03:00
|
|
|
parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
benchmarkSigning(b, jwt.SigningMethodRS384, parsedKey)
|
2015-04-11 23:53:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRS512Signing(b *testing.B) {
|
2022-05-28 02:11:16 +03:00
|
|
|
key, _ := os.ReadFile("test/sample_key")
|
2015-04-12 00:04:22 +03:00
|
|
|
parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
benchmarkSigning(b, jwt.SigningMethodRS512, parsedKey)
|
2015-04-11 23:53:09 +03:00
|
|
|
}
|