# README
webpush-go
Web Push API Encryption with VAPID support.
This library is a fork of SherClockHolmes/webpush-go. See CHANGELOG.md for details on migrating from the upstream library.
go get -u github.com/ergochat/webpush-go/v2
Example
For a full example, refer to the code in the example directory.
package main
import (
"encoding/json"
webpush "github.com/ergochat/webpush-go/v2"
)
func main() {
// Decode subscription
s := &webpush.Subscription{}
json.Unmarshal([]byte("<YOUR_SUBSCRIPTION>"), s)
vapidKeys := new(webpush.VAPIDKeys)
json.Unmarshal([]byte("<YOUR_VAPID_KEYS">), vapidKeys)
// Send Notification
resp, err := webpush.SendNotification([]byte("Test"), s, &webpush.Options{
Subscriber: "[email protected]",
VAPIDKeys: vapidKeys,
TTL: 3600, // seconds
})
if err != nil {
// TODO: Handle error
}
defer resp.Body.Close()
}
Generating VAPID Keys
Use the helper method GenerateVAPIDKeys
to generate the VAPID key pair.
vapidKeys, err := webpush.GenerateVAPIDKeys()
if err != nil {
// TODO: Handle error
}
Development
- Install Go 1.20+
go mod vendor
go test
For other language implementations visit:
# Packages
No description provided by the author
# Functions
DecodeLegacyVAPIDPrivateKey decodes the legacy string private key format returned by GenerateVAPIDKeys in v1.
DecodeSubscriptionKeys decodes and validates a base64-encoded pair of subscription keys (the authentication secret and ECDH public key).
ECDSAToVAPIDKeys wraps an existing ecdsa.PrivateKey in VAPIDKeys for use in VAPID header signing.
EncryptNotification implements the encryption algorithm specified by RFC 8291 for web push (RFC 8188's aes128gcm content-encoding, with the key material derived from elliptic curve Diffie-Hellman over the P-256 curve).
GenerateVAPIDKeys generates a VAPID keypair (an ECDSA keypair on the P-256 curve).
SendNotification sends a push notification to a subscription's endpoint, applying encryption (RFC 8291) and adding a VAPID header (RFC 8292).
# Constants
No description provided by the author
UrgencyHigh admits device state: low battery.
UrgencyLow requires device state: on either power or Wi-Fi.
UrgencyNormal excludes device state: low battery.
UrgencyVeryLow requires device state: on power and Wi-Fi.
# Variables
No description provided by the author
# Structs
Keys represents a subscription's keys (its ECDH public key on the P-256 curve and its 16-byte authentication secret).
Options are config and extra params needed to send a notification.
Subscription represents a PushSubscription object from the Push API.
VAPIDKeys is a public-private keypair for use in VAPID.
# Interfaces
HTTPClient is an interface for sending the notification HTTP request / testing.
# Type aliases
Urgency indicates to the push service how important a message is to the user.