Skip to content

Commit

Permalink
Fix private key base64 decode (#30)
Browse files Browse the repository at this point in the history
* Decode the vapid private key in URL or Raw base64 encoding

* Decode and encode VAPID public key
  • Loading branch information
SherClockHolmes authored Oct 27, 2019
1 parent 7ed4780 commit 358c5ab
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
json.Unmarshal([]byte("<YOUR_SUBSCRIPTION>"), s)

// Send Notification
_, err := webpush.SendNotification([]byte("Test"), s, &webpush.Options{
resp, err := webpush.SendNotification([]byte("Test"), s, &webpush.Options{
Subscriber: "[email protected]",
VAPIDPublicKey: "<YOUR_VAPID_PUBLIC_KEY>",
VAPIDPrivateKey: "<YOUR_VAPID_PRIVATE_KEY>",
Expand All @@ -37,6 +37,7 @@ func main() {
if err != nil {
// TODO: Handle error
}
defer resp.Body.Close()
}
```

Expand Down
3 changes: 2 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
json.Unmarshal([]byte(subscription), s)

// Send Notification
_, err := webpush.SendNotification([]byte("Test"), s, &webpush.Options{
resp, err := webpush.SendNotification([]byte("Test"), s, &webpush.Options{
Subscriber: "[email protected]", // Do not include "mailto:"
VAPIDPublicKey: vapidPublicKey,
VAPIDPrivateKey: vapidPrivateKey,
Expand All @@ -27,4 +27,5 @@ func main() {
if err != nil {
// TODO: Handle error
}
defer resp.Body.Close()
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613
)

go 1.13
23 changes: 20 additions & 3 deletions vapid.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func getVAPIDAuthorizationHeader(
"sub": fmt.Sprintf("mailto:%s", subscriber),
})

// ECDSA
decodedVapidPrivateKey, err := base64.RawURLEncoding.DecodeString(vapidPrivateKey)
// Decode the VAPID private key
decodedVapidPrivateKey, err := decodeVapidKey(vapidPrivateKey)
if err != nil {
return "", err
}
Expand All @@ -91,9 +91,26 @@ func getVAPIDAuthorizationHeader(
return "", err
}

// Decode the VAPID public key
pubKey, err := decodeVapidKey(vapidPublicKey)
if err != nil {
return "", err
}

return fmt.Sprintf(
"vapid t=%s, k=%s",
jwtString,
vapidPublicKey,
base64.RawURLEncoding.EncodeToString(pubKey),
), nil
}

// Need to decode the vapid private key in multiple base64 formats
// Solution from: https://github.com/SherClockHolmes/webpush-go/issues/29
func decodeVapidKey(key string) ([]byte, error) {
bytes, err := base64.URLEncoding.DecodeString(key)
if err == nil {
return bytes, nil
}

return base64.RawURLEncoding.DecodeString(key)
}
1 change: 1 addition & 0 deletions webpush.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func decodeSubscriptionKey(key string) ([]byte, error) {
if err == nil {
return bytes, nil
}

return base64.URLEncoding.DecodeString(buf.String())
}

Expand Down

0 comments on commit 358c5ab

Please sign in to comment.