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
- Variables
- func Encrypt(payload, p256dh, auth []byte) ([]byte, error)
- func GenerateVAPIDKey() (private, public string, err error)
- func NewHTTP2Client(timeout time.Duration, opts ...SenderOption) *http.Client
- func ReasonFrom(body []byte) string
- func ValidateEndpoint(endpoint string) (origin string, err error)
- func ValidateSubject(subject string) error
- type Options
- type Sender
- type SenderOption
- type StatusError
- type Subscription
- type Urgency
- type VAPIDKey
Constants ¶
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 ¶
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.
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.
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 ¶
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 ¶
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
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 ¶
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 ¶
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) Send ¶
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 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 ¶
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.