webpush

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 18 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 holds the client-side building blocks of Web Push that do not depend on any push service: message encryption (RFC 8188 aes128gcm content coding as profiled by RFC 8291) and VAPID application server identification (RFC 8292). The transports that talk to a push service live in subpackages (autopush) and the self-hostable relay in internal/pushd.

Index

Constants

View Source
const (
	DefaultRecordSize = 4096
	MaxBodySize       = 4096
	MaxPlaintext      = MaxBodySize - 86 - 16 - 1
)

Limits. RFC 8030 section 7.2 requires a push service to accept bodies of at least 4096 octets. With the 86 octet header (16 salt + 4 record size + 1 id length + 65 key), one delimiter octet and the 16 octet tag that leaves 3993 octets of plaintext in the default single record.

View Source
const MaxVAPIDLifetime = 24 * time.Hour

MaxVAPIDLifetime is how far in the future a VAPID token may expire (RFC 8292 section 2: 24 hours).

Variables

View Source
var (
	// ErrUnavailable is returned when no connection to the push service could be established.
	ErrUnavailable = errors.New("push service unavailable")
	// ErrNoService is what subscribe() reports when no push service is configured.
	ErrNoService = errors.New("push service not available")
)

Errors common to transports.

View Source
var ErrDecrypt = errors.New("webpush: cannot decrypt the message")

ErrDecrypt is wrapped by every failure of Decrypt.

Functions

func B64

func B64(b []byte) string

B64 encodes with the unpadded URL-safe alphabet used throughout Web Push.

func DecodeB64

func DecodeB64(s string) ([]byte, error)

DecodeB64 decodes unpadded (or padded) URL-safe base64.

func Decrypt

func Decrypt(uaPriv *ecdh.PrivateKey, auth, body []byte) ([]byte, error)

Decrypt decodes an RFC 8291 message with the receiver's private key and auth secret. The sender's public key is taken from the keyid of the RFC 8188 header, as RFC 8291 section 4 prescribes.

func Encrypt

func Encrypt(uaPublic, auth, plaintext []byte, o EncryptOptions) ([]byte, error)

Encrypt encodes a message for a subscription (RFC 8291 section 3). uaPublic is the p256dh value and auth the 16 octet authentication secret of the subscription. It exists for the relay's tests and for tools; a browser only decrypts.

func ParsePublicKey

func ParsePublicKey(b []byte) (*ecdsa.PublicKey, error)

ParsePublicKey checks that b is an uncompressed P-256 point (an applicationServerKey).

Types

type AckCode

type AckCode int

AckCode is the result a client reports for a message (autopush codes).

const (
	AckDelivered    AckCode = 100
	AckDecryptError AckCode = 101
	AckNoSubscriber AckCode = 102 // the channel is not known to the client
)

type EncryptOptions

type EncryptOptions struct {
	RecordSize uint32 // default 4096
	Padding    int    // octets of zero padding after the delimiter of the last record (must fit the record)
	// Fixed values for known-answer tests; random when nil.
	Salt          []byte
	SenderPrivate *ecdh.PrivateKey
}

EncryptOptions are knobs the RFC leaves to the sender; the zero value is the usual choice.

type Handler

type Handler interface {
	// OnMessage delivers one message. The transport sends no acknowledgement by itself: the owner calls Transport.Ack.
	OnMessage(Message)
	// OnReset says the service no longer knows this client (its identity was reissued): every subscription
	// made before is gone and must be recreated (pushsubscriptionchange).
	OnReset(newUAID string)
	// OnIdentity reports the client identity the service assigned, to be stored and presented again next time.
	OnIdentity(uaid string)
	// Channels lists the channel ids the owner holds subscriptions for (sent in the hello).
	Channels() []string
}

Handler receives what the service sends. Calls come from the transport's goroutine and must not block for long.

type Keys

type Keys struct {
	Private *ecdh.PrivateKey
	Auth    []byte
}

Keys is a subscription's receiving key material: the P-256 key pair (p256dh is the public half) and the 16 octet authentication secret that the application server also receives.

func NewKeys

func NewKeys() (*Keys, error)

NewKeys makes fresh receiver keys.

func (*Keys) Decrypt

func (k *Keys) Decrypt(body []byte) ([]byte, error)

Decrypt decodes a push message body (the aes128gcm coded body, header included).

func (*Keys) P256dh

func (k *Keys) P256dh() []byte

P256dh is the uncompressed public key (65 octets).

type Message

type Message struct {
	ChannelID string
	ID        string // service's message id (autopush "version"); echoed in the acknowledgement
	TTL       int
	Data      []byte // the aes128gcm coded body exactly as the application server sent it; nil for an empty push
}

Message is a push message as the service delivers it.

type Transport

type Transport interface {
	// Register creates a subscription for channelID and returns the push endpoint an application server
	// posts to. appServerKey is the VAPID public key the subscription is restricted to (nil = none).
	// It waits for the connection to the service, up to ctx.
	Register(ctx context.Context, channelID string, appServerKey []byte) (endpoint string, err error)
	// Unregister deletes a subscription at the service. The endpoint answers 410 afterwards.
	Unregister(ctx context.Context, channelID string) error
	// Ack tells the service that a message was handled, so that it stops storing it.
	Ack(channelID, messageID string, code AckCode)
	// Close ends the connection and every goroutine of the transport.
	Close() error
}

Transport is the client side of a push service connection: what the browser needs from a service to create subscriptions and receive messages. The default implementation speaks the open autopush WebSocket protocol (package autopush); another service can be plugged in by implementing this.

type VAPIDClaims

type VAPIDClaims struct {
	Audience string
	Subject  string
	Expires  time.Time
	Key      []byte // the public key that signed it
}

VAPIDClaims are the verified contents of a token.

func VerifyVAPID

func VerifyVAPID(authorization, cryptoKey, wantAudience string, now time.Time) (*VAPIDClaims, error)

VerifyVAPID checks an Authorization header (and, for the draft-era "WebPush" scheme, the Crypto-Key header) the way a push service does: signature, algorithm, audience and expiry (not more than 24 hours ahead). It returns the claims and the signing key. wantAudience may be empty to skip the audience check.

type VAPIDKey

type VAPIDKey struct{ Private *ecdsa.PrivateKey }

VAPIDKey is an application server's signing key (RFC 8292).

func NewVAPIDKey

func NewVAPIDKey() (*VAPIDKey, error)

NewVAPIDKey makes a fresh P-256 signing key.

func (*VAPIDKey) Authorization

func (k *VAPIDKey) Authorization(audience, subject string, exp time.Time) (string, error)

Authorization builds the value of the Authorization header for a request to a push service whose origin is audience ("https://push.example.net"): "vapid t=<jwt>, k=<key>".

func (*VAPIDKey) Public

func (k *VAPIDKey) Public() []byte

Public is the uncompressed public key: the applicationServerKey that goes to PushManager.subscribe().

Directories

Path Synopsis
Package autopush is a client of the open autopush WebSocket protocol (the one Mozilla's push service speaks), implementing webpush.Transport.
Package autopush is a client of the open autopush WebSocket protocol (the one Mozilla's push service speaks), implementing webpush.Transport.

Jump to

Keyboard shortcuts

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