webpush

package
v0.0.0-...-0569425 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: Apache-2.0 Imports: 22 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRequest

func NewRequest(dest string, key, authK []byte,
	message string, ttlSec int, v TokenProvider) (*http.Request, error)

NewVapidRequest creates a valid Web Push HTTP request for sending a message to a subscriber, using Vapid authentication.

You can add more headers to configure collapsing, TTL.

func RawKeyToPrivateKey

func RawKeyToPrivateKey(key, pub string) *ecdsa.PrivateKey

func SendMessage

func SendMessage(hc *http.Client, subs string, show bool, msg string, v TokenProvider)

Send an encrypted message to a node.

Types

type Subscription

type Subscription struct {
	// Endpoint is the URL to send the Web Push message to. Comes from the
	// endpoint field of the PushSubscription.
	Endpoint string `json:"endpoint"`

	Keys struct {
		P256dh string `json:"p256Dh"`
		Auth   string `json:"auth"`
	} `json:"keys"`

	// Key is the client's public key. From the getKey("p256dh") or keys.p256dh field.
	Key []byte `json:"-"`

	// Auth is a value used by the client to validate the encryption. From the
	// keys.auth field.
	// The encrypted aes128gcm will have 16 bytes authentication tag derived from this.
	// This is the pre-shared authentication secret.
	Auth []byte `json:"-"`

	// Used by the UA to receive messages, as PUSH promises
	Location string `json:"-"`
}

Subscription holds the values for encrypting messages

func SubscriptionFromJSON

func SubscriptionFromJSON(b []byte) (*Subscription, error)

Subscription holds the useful values from a PushSubscription object acquired from the browser.

https://w3c.github.io/push-api/

Returned as result of /subscribe

func (*Subscription) Encrypt

func (s *Subscription) Encrypt(data []byte) []byte

type TokenProvider

type TokenProvider interface {
	GetToken(context.Context, string) (string, error)
}

type WebpushEncryption

type WebpushEncryption struct {
	// Full body of the encrypted message, including header (salt, server pub).
	//
	// Format:
	// 16 B Salt
	// 4B rs {0,0, 16, 0} - 4k
	// 1B WorkloadID-Size {65}
	// 65B SendPublicKey
	// Up to 4k encrypted text - with 0x02 appended at the end before encryption
	// Wasted: 7 const.
	// Overhead: 16 salt, 16 sig, 64 pub. Total: 103 (64+32+7)
	Ciphertext []byte

	// 16B For encryption: must be a random generated by sender.
	Salt []byte

	// UA Public bytes - from subscription, it's the public key of the
	// receiver ( user agent, browser ).
	UAPublic []byte

	// Only used for encrypt - the private key of the sender
	SendPrivate []byte

	// Temp EC key for encryption, 65B
	SendPublic []byte

	// Only used for decrypt
	UAPrivate []byte

	// Auth - from subscription. If missing, a default value can be used - either zero or
	// a hash of the recipinent URL or public key.
	Auth []byte
	// contains filtered or unexported fields
}

WebpushEncruption is used for encryption and decryption of messages using webpush protocol.

It stores the source and result of encrypting a message and associated parameters.

func NewWebpushDecryption

func NewWebpushDecryption(uapriv string, uapub, auth []byte) *WebpushEncryption

NewWebpushDecryption creates a context for decrypting message by a UA. The private key is base64URL encoded.

func NewWebpushEncryption

func NewWebpushEncryption(uapub, auth []byte) *WebpushEncryption

NewWebpushEncryption creates a new encryption context for sending, based on the subscription pub key and auth.

func (*WebpushEncryption) Decrypt

func (er *WebpushEncryption) Decrypt(cypher []byte) ([]byte, error)

func (*WebpushEncryption) Encrypt

func (er *WebpushEncryption) Encrypt(plaintext []byte) ([]byte, error)

Encrypt a message such that it can be sent using the Web Push protocol.

RFC8030 - message RFC8291 - encryption

Jump to

Keyboard shortcuts

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