webhooks

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT 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 webhooks sends authama's event feed to endpoints you register, as Standard Webhooks (https://www.standardwebhooks.com, spec 1.0.0) describes: each event is a POST of the CloudEvent itself, in structured mode (Content-Type: application/cloudevents+json), with the headers webhook-id (the event's id), webhook-timestamp and webhook-signature, the signature being "v1," and the base64 HMAC-SHA256 of "<id>.<timestamp>.<body>" under the endpoint's whsec_ secret. The endpoint's name is in the signed body (authamasub), so a delivery cannot be replayed to another endpoint that checks it. This is sange's wire format, so one receiver reads both.

A webhook is a request authama makes to an address someone chose, so the sender refuses what would let it be aimed inward: only https, never a loopback, private, link-local, unspecified or multicast address (checked on the address actually dialled, so DNS cannot rebind around it) unless the owner allows private endpoints, and never a redirect.

Index

Constants

View Source
const ContentType = "application/cloudevents+json; charset=utf-8"

ContentType is a delivery's: a CloudEvent in structured mode.

View Source
const MaxFailures = 10

MaxFailures in a row disables an endpoint.

View Source
const SecretPrefix = "whsec_"

SecretPrefix starts every endpoint secret, as Standard Webhooks names it.

Variables

View Source
var ErrNotFound = errors.New("no webhook by that name")

ErrNotFound is a name no endpoint has.

Functions

func CheckURL

func CheckURL(rawURL string, allowPrivate bool) error

CheckURL says why rawURL cannot be an endpoint: it must be https with a host, and, unless allowPrivate, not name a loopback or private address literally (a name is checked again when it is dialled). With allowPrivate, plain http is allowed to a loopback address, for a receiver on this machine.

func Client

func Client(allowPrivate bool) *http.Client

Client is the sender's HTTP client: 10 s per request, no redirect followed (a 3xx is a failure, as the spec says), and, unless allowPrivate, a dialer that refuses any address that is not public, after the name is resolved.

func File

func File(p paths.Paths) string

File is where endpoints are kept: owner-only, as it holds their secrets.

func Remove

func Remove(p paths.Paths, name string) error

Remove forgets an endpoint and its secret.

func SetDisabled

func SetDisabled(p paths.Paths, name string, off bool) error

SetDisabled turns delivery to an endpoint off or back on. Turned on, its failures are forgotten, and it resumes from where it stopped.

func Sign

func Sign(secret, id string, ts int64, body []byte) (string, error)

Sign is the webhook-signature value for a message: "v1," and the base64 HMAC-SHA256, under the secret's key, of "<id>.<timestamp>.<body>".

func Test

func Test(ctx context.Context, p paths.Paths, client *http.Client, name string, now time.Time) (int, error)

Test sends one webhook.tested event to an endpoint now, whatever its state, and moves nothing: its cursor and failures stay as they were.

func Verify

func Verify(secret, id, tsHeader, sigHeader string, body []byte, now time.Time, tolerance time.Duration) bool

Verify checks a webhook-signature header as a receiver would: one of its space-separated signatures matches, compared in constant time, and the timestamp is within tolerance of now. Tests and `webhook test` use it.

Types

type Endpoint

type Endpoint struct {
	Name   string   `json:"name"`
	URL    string   `json:"url"`
	Secret string   `json:"secret"` // whsec_ and base64 of 32 random bytes
	Events []string `json:"events,omitempty"`
	// Cursor is the feed position of the last event delivered.
	Cursor   string    `json:"cursor"`
	Disabled bool      `json:"disabled,omitempty"`
	Created  time.Time `json:"created"`
	// Failures counts failed attempts in a row; NextAttempt is when the next
	// may be made after one.
	Failures    int       `json:"failures,omitempty"`
	NextAttempt time.Time `json:"next_attempt,omitzero"`
	LastStatus  int       `json:"last_status,omitempty"`
	LastError   string    `json:"last_error,omitempty"`
	LastSuccess time.Time `json:"last_success,omitzero"`
	// DisabledWhy says why delivery stopped by itself (a 410, too many
	// failures); "" when it was disabled by hand or is on.
	DisabledWhy string `json:"disabled_why,omitempty"`
}

Endpoint is one registered receiver and where its delivery has got to.

func Add

func Add(p paths.Paths, name, rawURL string, events []string, allowPrivate bool, now time.Time) (Endpoint, error)

Add registers an endpoint that is sent events whose type starts with one of events (all of them when there are none), from now on: it is not sent the history. It returns the endpoint, whose secret is shown only then.

func List

func List(p paths.Paths) ([]Endpoint, error)

List is every endpoint, in the order they were added.

type Result

type Result struct {
	Name      string
	Sent      int
	Err       error
	Disabled  bool
	Postponed bool // a failure's backoff has not run out
}

Result is one endpoint's outcome from a Deliver.

func Deliver

func Deliver(ctx context.Context, p paths.Paths, client *http.Client, now time.Time) ([]Result, error)

Deliver sends each enabled endpoint the events after its cursor, in order, stopping at its first failure: it is tried again after a backoff, from the same event, so an endpoint sees each event once and in order.

Jump to

Keyboard shortcuts

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