webpush

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
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.

View Source
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

func CheckEndpoint(endpoint string, custom bool) error

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 GenerateKey

func GenerateKey() (*ecdsa.PrivateKey, error)

GenerateKey makes a new VAPID key.

func KeyBytes

func KeyBytes(k *ecdsa.PrivateKey) ([]byte, error)

KeyBytes is the 32-byte private scalar of a VAPID key, for sealing.

func ParseKey

func ParseKey(raw []byte) (*ecdsa.PrivateKey, error)

ParseKey reads a VAPID key from its 32-byte private scalar.

func PublicKey

func PublicKey(k *ecdsa.PrivateKey) (string, error)

PublicKey is the key's public point, uncompressed and base64url: the applicationServerKey a browser subscribes with.

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.

func (*Sender) Send

func (s *Sender) Send(ctx context.Context, sub Subscription, m Message) error

Send encrypts m for sub and posts it. It returns ErrGone when the subscription is no more.

type Subscription

type Subscription struct {
	Endpoint string
	P256dh   string
	Auth     string
}

Subscription is what a browser's PushManager.subscribe returns: where to send, and the keys to encrypt for. P256dh and Auth are base64url.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL