Documentation
¶
Overview ¶
Package webpush sends Web Push messages: the payload encrypted for one browser subscription (RFC 8291, aes128gcm of RFC 8188) and the request signed with the server's VAPID key (RFC 8292). It uses the standard library only.
Index ¶
- Constants
- Variables
- func CheckEndpoint(endpoint string, custom bool) error
- func CheckSubscription(sub Subscription) error
- func Encrypt(sub Subscription, plaintext []byte) ([]byte, error)
- func GenerateKey() (*ecdsa.PrivateKey, error)
- func KeyBytes(k *ecdsa.PrivateKey) ([]byte, error)
- func ParseKey(raw []byte) (*ecdsa.PrivateKey, error)
- func PublicKey(k *ecdsa.PrivateKey) (string, error)
- type Message
- type Sender
- type Subscription
Constants ¶
const MaxPayload = 3000
MaxPayload is the largest plaintext a message may carry: 4096-byte records minus the header, padding delimiter and tag leave about 3993, and push services accept 4096 bytes of body at most.
Variables ¶
var ErrGone = errors.New("the push subscription expired or was revoked")
ErrGone means the push service no longer knows the subscription (404 or 410): the browser unsubscribed or the person revoked permission. Drop it.
var PublicClient = &http.Client{ Timeout: 15 * time.Second, Transport: &http.Transport{ Proxy: nil, DialContext: (&net.Dialer{Timeout: 10 * time.Second, Control: publicOnly}).DialContext, TLSHandshakeTimeout: 10 * time.Second, MaxIdleConns: 16, IdleConnTimeout: 90 * time.Second, }, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }, }
PublicClient only connects to public addresses: a subscription names any URL, and the server must not post to its own network on a person's say-so.
Functions ¶
func CheckEndpoint ¶
CheckEndpoint refuses endpoints that are not https URLs; custom is set when the caller brought its own client (tests), which may use http.
func CheckSubscription ¶
func CheckSubscription(sub Subscription) error
CheckSubscription reports why a subscription cannot be used, or nil. The endpoint must be https on a public host name: the server posts to it, so it must not reach into the server's own network.
func Encrypt ¶
func Encrypt(sub Subscription, plaintext []byte) ([]byte, error)
Encrypt encrypts plaintext for a subscription with a fresh key and salt.
func KeyBytes ¶
func KeyBytes(k *ecdsa.PrivateKey) ([]byte, error)
KeyBytes is the 32-byte private scalar of a VAPID key, for sealing.
Types ¶
type Message ¶
type Message struct {
Payload []byte
TTL time.Duration // how long the push service keeps it for an offline device
Topic string // a newer message with the same topic replaces an undelivered one (optional, base64url, <= 32 chars)
Urgent bool
}
Message is one notification.
type Sender ¶
type Sender struct {
Key *ecdsa.PrivateKey
// Subject is how the push service can reach the sender: the server's
// https URL or a mailto: address.
Subject string
// Client sends the requests; nil uses PublicClient.
Client *http.Client
Now func() time.Time
}
Sender posts messages to push services.
type Subscription ¶
Subscription is what a browser's PushManager.subscribe returns: where to send, and the keys to encrypt for. P256dh and Auth are base64url.