webpush

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: BSD-3-Clause Imports: 25 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 to push services. It implements RFC 8291 message encryption (aes128gcm), RFC 8292 VAPID authorization, and an HTTPS sender that connects only to public addresses. It uses only the Go standard library.

Index

Constants

View Source
const PaddedLen = 1024

PaddedLen is the plaintext length of every message in bytes. It includes the delimiter byte and the padding. A fixed length makes every body the same size, so the size tells the push service nothing about the content.

Variables

View Source
var ErrEndpoint = errors.New("webpush: endpoint must be an https URL")

ErrEndpoint is returned for an endpoint that is not an https URL with a host and without credentials.

View Source
var ErrGone = errors.New("webpush: subscription is gone")

ErrGone is returned when the push service answers 404 or 410. The subscription no longer exists. The caller must delete it.

View Source
var ErrPayloadTooLarge = errors.New("webpush: payload too large")

ErrPayloadTooLarge is returned for a payload that is longer than PaddedLen minus one byte, because the delimiter needs that byte.

Functions

func Encrypt

func Encrypt(payload, p256dh, auth []byte) ([]byte, error)

Encrypt returns the aes128gcm body for payload, encrypted to the subscription keys p256dh (65 bytes) and auth (16 bytes). The body has the same length for every payload. If payload is longer than PaddedLen minus one byte, Encrypt returns ErrPayloadTooLarge.

func GenerateVAPIDKey

func GenerateVAPIDKey() (private, public string, err error)

GenerateVAPIDKey returns a new key pair as base64url strings: the 32-byte private scalar and the 65-byte uncompressed public point.

func NewHTTP2Client added in v0.12.5

func NewHTTP2Client(timeout time.Duration, opts ...SenderOption) *http.Client

NewHTTP2Client returns the HTTP client of a Sender, but with HTTP/2 on. It connects only to public addresses, uses no proxy, follows no redirect, and gives up after timeout. The options change how the client dials, for example WithIPv4Only. Go turns HTTP/2 off for a transport with a custom dial function, and APNs speaks only HTTP/2, so the transport sets ForceAttemptHTTP2. The sender of this package keeps HTTP/1.1.

func ReasonFrom added in v0.12.5

func ReasonFrom(body []byte) string

ReasonFrom returns the reason text in the response body of a push service. It is the exported form of reasonFrom, for the APNs sender in package apns.

func ValidateEndpoint

func ValidateEndpoint(endpoint string) (origin string, err error)

ValidateEndpoint returns ErrEndpoint unless endpoint is an https URL with a host and without credentials. It returns the origin, which is the VAPID audience.

func ValidateSubject

func ValidateSubject(subject string) error

ValidateSubject reports an error unless subject is a mailto: URL with an address or an https: URL with a host.

Types

type Options

type Options struct {
	TTL     time.Duration // how long the push service keeps an undelivered push
	Topic   string        // a later push with the same topic replaces this one
	Urgency Urgency       // empty means the service default, normal
}

Options are the delivery headers of one push. Send writes TTL in whole seconds, also when TTL is zero.

type Sender

type Sender struct {
	// contains filtered or unexported fields
}

A Sender posts encrypted pushes to push services. It is safe for concurrent use.

func NewSender

func NewSender(key *VAPIDKey, timeout time.Duration, opts ...SenderOption) *Sender

NewSender returns a Sender that signs with key and gives up on one push after timeout. Its HTTP client connects only to public addresses and follows no redirect. The options (65-11) change how the client dials, for example WithIPv4Only.

func (*Sender) PublicKey

func (s *Sender) PublicKey() string

PublicKey returns the VAPID public key of the Sender.

func (*Sender) Send

func (s *Sender) Send(ctx context.Context, sub Subscription, payload []byte, opts Options) error

Send encrypts payload to sub and posts it to the endpoint.

It returns ErrGone if the subscription no longer exists, ErrEndpoint or ErrPayloadTooLarge for bad input, and a *StatusError for every other answer that is not a success. Send reads at most 4096 bytes of the response body. Only a *StatusError uses the body, for its Reason.

type SenderOption added in v0.12.2

type SenderOption func(*senderOptions)

A SenderOption changes one setting of a Sender at construction.

func WithIPv4Only added in v0.12.2

func WithIPv4Only() SenderOption

WithIPv4Only returns a SenderOption that makes the Sender dial with network "tcp4". The Sender then tries no IPv6 address.

type StatusError

type StatusError struct {
	Code       int
	RetryAfter string // the Retry-After header, if the service sent one
	Reason     string // 65-12: the reason text of the service, cleaned by reasonFrom
}

A StatusError is an answer of the push service that is neither a success (status 200 to 299) nor ErrGone. A redirect is a StatusError, because the Sender does not follow redirects. Reason holds the text that the service gives for the rejection, if the response body has any.

func (*StatusError) Error

func (e *StatusError) Error() string

Error returns the message of e, with the status code. If Reason is not empty, the message ends with Reason in parentheses.

type Subscription

type Subscription struct {
	Endpoint string // the https URL of the push service
	P256dh   []byte // the browser public key, 65 bytes
	Auth     []byte // the authentication secret, 16 bytes
}

A Subscription is the part of a browser's push registration that a send needs.

type Urgency

type Urgency string

Urgency is the Urgency header of RFC 8030.

const (
	UrgencyVeryLow Urgency = "very-low"
	UrgencyLow     Urgency = "low"
	UrgencyNormal  Urgency = "normal"
	UrgencyHigh    Urgency = "high"
)

The values of the Urgency header.

type VAPIDKey

type VAPIDKey struct {
	// contains filtered or unexported fields
}

A VAPIDKey is the P-256 key pair that identifies this server to the push services, with the contact URL that goes into every token. It is safe for concurrent use. Create it with ParseVAPIDKey. The zero value is not usable.

func ParseVAPIDKey

func ParseVAPIDKey(private, subject string) (*VAPIDKey, error)

ParseVAPIDKey builds a VAPIDKey from the base64url private scalar and the subject. The subject must be a mailto: or https: URL, because the push services reject a token without one.

func (*VAPIDKey) PublicKey

func (k *VAPIDKey) PublicKey() string

PublicKey returns the base64url public point. The browser needs it as the applicationServerKey of a subscription.

Jump to

Keyboard shortcuts

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