forked from golang-jwt/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ed25519_test.go
84 lines (70 loc) · 2.2 KB
/
ed25519_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package jwt_test
import (
"os"
"strings"
"testing"
"github.com/golang-jwt/jwt/v5"
)
var ed25519TestData = []struct {
name string
keys map[string]string
tokenString string
alg string
claims map[string]interface{}
valid bool
}{
{
"Basic Ed25519",
map[string]string{"private": "test/ed25519-private.pem", "public": "test/ed25519-public.pem"},
"eyJhbGciOiJFRDI1NTE5IiwidHlwIjoiSldUIn0.eyJmb28iOiJiYXIifQ.ESuVzZq1cECrt9Od_gLPVG-_6uRP_8Nq-ajx6CtmlDqRJZqdejro2ilkqaQgSL-siE_3JMTUW7UwAorLaTyFCw",
"EdDSA",
map[string]interface{}{"foo": "bar"},
true,
},
{
"Basic Ed25519",
map[string]string{"private": "test/ed25519-private.pem", "public": "test/ed25519-public.pem"},
"eyJhbGciOiJFRDI1NTE5IiwidHlwIjoiSldUIn0.eyJmb28iOiJiYXoifQ.ESuVzZq1cECrt9Od_gLPVG-_6uRP_8Nq-ajx6CtmlDqRJZqdejro2ilkqaQgSL-siE_3JMTUW7UwAorLaTyFCw",
"EdDSA",
map[string]interface{}{"foo": "bar"},
false,
},
}
func TestEd25519Verify(t *testing.T) {
for _, data := range ed25519TestData {
var err error
key, _ := os.ReadFile(data.keys["public"])
ed25519Key, err := jwt.ParseEdPublicKeyFromPEM(key)
if err != nil {
t.Errorf("Unable to parse Ed25519 public key: %v", err)
}
parts := strings.Split(data.tokenString, ".")
method := jwt.GetSigningMethod(data.alg)
err = method.Verify(strings.Join(parts[0:2], "."), parts[2], ed25519Key)
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)
}
}
}
func TestEd25519Sign(t *testing.T) {
for _, data := range ed25519TestData {
var err error
key, _ := os.ReadFile(data.keys["private"])
ed25519Key, err := jwt.ParseEdPrivateKeyFromPEM(key)
if err != nil {
t.Errorf("Unable to parse Ed25519 private key: %v", err)
}
parts := strings.Split(data.tokenString, ".")
method := jwt.GetSigningMethod(data.alg)
sig, err := method.Sign(strings.Join(parts[0:2], "."), ed25519Key)
if err != nil {
t.Errorf("[%v] Error signing token: %v", data.name, err)
}
if sig == parts[2] && !data.valid {
t.Errorf("[%v] Identical signatures\nbefore:\n%v\nafter:\n%v", data.name, parts[2], sig)
}
}
}