webpush

package
v1.5.9 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package webpush sends browser push notifications (RFC 8030/8291) to every subscription this control plane's dashboard has registered, backing the "webpush" notification-channel kind in internal/alerting: an operator gets a deploy/alert notification even with no dashboard tab focused or open, something none of the other 17 channel kinds (all webhooks or email) can do on their own.

Index

Constants

View Source
const (
	// EnvSendTimeout bounds one Send call, so a slow push service cannot stall the caller.
	EnvSendTimeout = "APP_WEBPUSH_SEND_TIMEOUT"
	// EnvAllowedHosts replaces the default push service host list (comma separated, "*.example.com" matches subdomains).
	EnvAllowedHosts = "APP_WEBPUSH_ALLOWED_HOSTS"
	// EnvMaxPerUser caps the browsers one account may register.
	EnvMaxPerUser = "APP_WEBPUSH_MAX_SUBSCRIPTIONS_PER_USER"
)
View Source
const (
	MaxEndpointLen  = 2048
	MaxKeyLen       = 128
	MaxUserAgentLen = 512
	// MaxRequestBytes bounds the registration body.
	MaxRequestBytes = 8 << 10
)

Field limits for a registration request.

Variables

View Source
var ErrInvalidEndpoint = errors.New("webpush: endpoint is not an allowed push service address")

ErrInvalidEndpoint is returned for an endpoint that is not an https URL on an allowed push service.

View Source
var ErrInvalidKeys = errors.New("webpush: invalid subscription keys")

ErrInvalidKeys is returned for subscription keys of the wrong shape.

Functions

func AllowedHosts

func AllowedHosts(getenv func(string) string) []string

AllowedHosts reads EnvAllowedHosts, falling back to the real push services.

func EnsureVAPIDKeys

func EnsureVAPIDKeys(ctx context.Context, keys KeyStore) (publicKey, privateKey string, err error)

EnsureVAPIDKeys returns this control plane's VAPID keypair, generating and persisting one through keys the first time it's called. Call once at startup, before anything can register a subscription against the public half: regenerating the pair later would silently orphan every subscription already registered against the old one (the browser's applicationServerKey would no longer match what this control plane signs with).

func MaxPerUser

func MaxPerUser(getenv func(string) string) int

MaxPerUser reads EnvMaxPerUser, falling back to the default.

func SendTimeout

func SendTimeout(getenv func(string) string) time.Duration

SendTimeout reads EnvSendTimeout, falling back to the default.

func ValidateEndpoint

func ValidateEndpoint(endpoint string, allowed []string) error

ValidateEndpoint accepts only https URLs without credentials whose host matches allowed.

func ValidateKeys

func ValidateKeys(p256dh, auth string) error

ValidateKeys checks p256dh is an uncompressed P-256 point and auth is 16 bytes.

Types

type KeyStore

type KeyStore interface {
	Exists(ctx context.Context, serviceName, envKey string) (bool, error)
	SetValue(ctx context.Context, serviceName, envKey, plaintext string) error
	Resolve(ctx context.Context, serviceName, envKey string) (string, error)
}

KeyStore is the narrow internal/secrets surface EnsureVAPIDKeys needs. *secrets.Manager satisfies it structurally, the same "narrow, consumer-defined interface" shape VaultSecrets (internal/api) already establishes.

type RecipientPolicy

type RecipientPolicy interface {
	AllowPush(ctx context.Context, userID string, audience alerting.PushAudience) bool
}

RecipientPolicy decides whether a user may receive a push for an audience.

type Sender

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

Sender sends one notification to every registered browser subscription. It satisfies alerting.PushSender structurally (that package takes a narrow Send(ctx, title, body) interface rather than importing this one, the same "consumer defines the interface it needs" shape email.Sender's own consumers already follow).

func NewSender

func NewSender(subs Subscriptions, publicKey, privateKey, subscriber string, client *http.Client, logger *slog.Logger) *Sender

NewSender builds a Sender. subscriber is the VAPID JWT "sub" claim a push service may contact if this control plane's server is sending too aggressively; an https:// URL or an email address, per RFC 8292.

func (*Sender) Send

func (s *Sender) Send(ctx context.Context, title, body string, audience alerting.PushAudience) error

Send pushes one notification to the subscriptions of users the audience allows. A per-subscription failure never aborts the others, and the whole call stops at the send timeout. Returns an error only when no eligible subscription exists, or every send attempt failed.

func (*Sender) WithEndpointValidator

func (s *Sender) WithEndpointValidator(fn func(endpoint string) error) *Sender

WithEndpointValidator replaces the endpoint check run before every send.

func (*Sender) WithPolicy

func (s *Sender) WithPolicy(p RecipientPolicy) *Sender

WithPolicy sets the recipient policy. Without one nobody receives a push.

type Subscriptions

type Subscriptions interface {
	ListPushSubscriptions(ctx context.Context) ([]store.PushSubscription, error)
	DeletePushSubscriptionByID(ctx context.Context, id string) error
}

Subscriptions is the store surface Sender needs: every registered subscription, and a way to prune one a push service has reported permanently gone. *store.DB satisfies this structurally.

Jump to

Keyboard shortcuts

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