forked from SherClockHolmes/webpush-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvapid_test.go
220 lines (197 loc) · 4.69 KB
/
vapid_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package webpush
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/json"
"fmt"
"strings"
"testing"
"time"
jwt "github.com/golang-jwt/jwt/v5"
)
func TestVAPID(t *testing.T) {
s := getStandardEncodedTestSubscription()
sub := "[email protected]"
// Generate vapid keys
vapidKeys, err := GenerateVAPIDKeys()
if err != nil {
t.Fatal(err)
}
// Get authentication header
vapidAuthHeader, err := getVAPIDAuthorizationHeader(
s.Endpoint,
sub,
vapidKeys,
time.Now().Add(time.Hour*12),
)
if err != nil {
t.Fatal(err)
}
// Validate the token in the Authorization header
tokenString := getTokenFromAuthorizationHeader(vapidAuthHeader, t)
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok {
t.Fatal("Wrong validation method need ECDSA!")
}
// To decode the token it needs the VAPID public key
return vapidKeys.privateKey.Public(), nil
})
// Check the claims on the token
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
expectedSub := fmt.Sprintf("mailto:%s", sub)
if expectedSub != claims["sub"] {
t.Fatalf(
"Incorreect mailto, expected=%s, got=%s",
expectedSub,
claims["sub"],
)
}
if claims["aud"] == "" {
t.Fatal("Audience should not be empty")
}
} else {
t.Fatal(err)
}
}
func TestVAPIDKeys(t *testing.T) {
vapidKeys, err := GenerateVAPIDKeys()
if err != nil {
t.Fatal(err)
}
j, err := json.Marshal(vapidKeys)
if err != nil {
t.Fatal(err)
}
vapidKeys2 := new(VAPIDKeys)
if err := json.Unmarshal(j, vapidKeys2); err != nil {
t.Fatal(err)
}
if !vapidKeys.privateKey.Equal(vapidKeys2.privateKey) {
t.Fatalf("could not round-trip private key")
}
if vapidKeys.publicKey != vapidKeys2.publicKey {
t.Fatalf("could not round-trip public key")
}
}
// Helper function for extracting the token from the Authorization header
func getTokenFromAuthorizationHeader(tokenHeader string, t *testing.T) string {
hsplit := strings.Split(tokenHeader, " ")
if len(hsplit) < 3 {
t.Fatal("Failed to auth split header")
}
tsplit := strings.Split(hsplit[1], "=")
if len(tsplit) < 2 {
t.Fatal("Failed to t split header on =")
}
return tsplit[1][:len(tsplit[1])-1]
}
func Test_ecdhPublicKeyToECDSA(t *testing.T) {
tests := [...]struct {
name string
curve elliptic.Curve
}{
// P224 not supported by ecdh
{
name: "P256",
curve: elliptic.P256(),
},
{
name: "P256",
curve: elliptic.P384(),
},
{
name: "P521",
curve: elliptic.P521(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pk, err := ecdsa.GenerateKey(tt.curve, rand.Reader)
if err != nil {
t.Fatalf("generating ecdsa.PrivateKey: %s", err)
}
original := &pk.PublicKey
converted, err := original.ECDH()
if err != nil {
t.Fatalf("converting ecdsa.PublicKey to ecdh.PublicKey: %s", err)
}
roundtrip, err := ecdhPublicKeyToECDSA(converted)
if err != nil {
t.Fatalf("converting ecdh.PublicKey back to ecdsa.PublicKey: %s", err)
}
if !roundtrip.Equal(original) {
t.Errorf("Roundtrip changed key from %v to %v", original, roundtrip)
}
})
}
}
func Test_ecdhPrivateKeyToECDSA(t *testing.T) {
tests := [...]struct {
name string
curve elliptic.Curve
}{
// P224 not supported by ecdh
{
name: "P256",
curve: elliptic.P256(),
},
{
name: "P256",
curve: elliptic.P384(),
},
{
name: "P521",
curve: elliptic.P521(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
original, err := ecdsa.GenerateKey(tt.curve, rand.Reader)
if err != nil {
t.Fatalf("generating ecdsa.PrivateKey: %s", err)
}
converted, err := original.ECDH()
if err != nil {
t.Fatalf("converting ecdsa.PrivateKey to ecdh.PrivateKey: %s", err)
}
roundtrip, err := ecdhPrivateKeyToECDSA(converted)
if err != nil {
t.Fatalf("converting ecdh.PrivateKey back to ecdsa.PrivateKey: %s", err)
}
if !roundtrip.Equal(original) {
t.Errorf("Roundtrip changed key from %v to %v", original, roundtrip)
}
})
}
}
func TestVAPIDKeyFromECDSA(t *testing.T) {
v, err := GenerateVAPIDKeys()
if err != nil {
t.Fatal(err)
}
privKey := v.PrivateKey()
v2, err := ECDSAToVAPIDKeys(privKey)
if err != nil {
t.Fatal(err)
}
if !v.Equal(v2) {
t.Fatal("ECDSAToVAPIDKeys failed round-trip")
}
}
func BenchmarkVAPIDSigning(b *testing.B) {
vapidKeys, err := GenerateVAPIDKeys()
if err != nil {
b.Fatal(err)
}
expiration := time.Now().Add(24 * time.Hour)
b.ResetTimer()
for i := 0; i < b.N; i++ {
getVAPIDAuthorizationHeader(
"https://test.push.service/v2/AOWJIDuOMDSo6uNnRXYNsw",
"https://application.server",
vapidKeys,
expiration,
)
}
}