forked from SherClockHolmes/webpush-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvapid_test.go
95 lines (76 loc) · 2.31 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
package webpush
import (
"encoding/base64"
"fmt"
"net/http"
"strings"
"testing"
jwt "github.com/dgrijalva/jwt-go"
"github.com/stretchr/testify/assert"
)
func TestVAPID(t *testing.T) {
assert := assert.New(t)
s := getStandardEncodedTestSubscription()
sub := "test@test.com"
vapidPrivateKey := "MHcCAQEEIHF7ijDrb8gwj_9o7UuSx9t_oGlPMyOsG9YQLp3qJwLuoAoGCCqGSM49AwEHoUQDQgAEhB-nJdg0d5oOkdTYsKqbbuQ06ZUYkS0H-ELXsShIkpmcIVIO16Sj15YMBouesMbY4xPdepwF4Pj3QfaALRAG5Q"
// Create the request
req, err := http.NewRequest("POST", s.Endpoint, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
// Run the request through vapid()
err = vapid(req, s, &Options{
Subscriber: sub,
VAPIDPrivateKey: vapidPrivateKey,
})
assert.Nil(err)
// Validate the token in the Authorization header
tokenString := getTokenFromAuthorizationHeader(req.Header.Get("Authorization"), 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
b64 := base64.RawURLEncoding
decodedVapidPrivateKey, err := b64.DecodeString(vapidPrivateKey)
if err != nil {
t.Fatal("Could not decode VAPID private key")
}
pubKey, _ := generateVAPIDHeaderKeys(decodedVapidPrivateKey)
return pubKey, nil
})
// Check the claims on the token
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
assert.Equal(fmt.Sprintf("mailto:%s", sub), claims["sub"])
assert.NotEmpty(claims["aud"], "Audience should not be empty")
} else {
t.Fatal(err)
}
}
func TestVAPIDKeys(t *testing.T) {
privateKey, publicKey, err := GenerateVAPIDKeys()
if err != nil {
t.Fatal(err)
}
if len(privateKey) != 43 {
t.Fatal("Generated incorrect VAPID private key")
}
if len(publicKey) != 87 {
t.Fatal("Generated incorrect VAPID public key")
}
}
// Helper function for extracting the token from the Authorization header
func getTokenFromAuthorizationHeader(tokenHeader string, t *testing.T) string {
split := strings.Split(tokenHeader, " ")
if len(split) < 2 {
t.Fatal("Failed to split header")
}
switch split[0] {
case "WebPush":
fallthrough
case "Bearer":
return split[1]
}
t.Fatal("Something wrong happened")
return ""
}