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
- Variables
- func CheckURL(rawURL string, allowPrivate bool) error
- func Client(allowPrivate bool) *http.Client
- func File(p paths.Paths) string
- func Remove(p paths.Paths, name string) error
- func SetDisabled(p paths.Paths, name string, off bool) error
- func Sign(secret, id string, ts int64, body []byte) (string, error)
- func Test(ctx context.Context, p paths.Paths, client *http.Client, name string, ...) (int, error)
- func Verify(secret, id, tsHeader, sigHeader string, body []byte, now time.Time, ...) bool
- type Endpoint
- type Result
Constants ¶
const ContentType = "application/cloudevents+json; charset=utf-8"
ContentType is a delivery's: a CloudEvent in structured mode.
const MaxFailures = 10
MaxFailures in a row disables an endpoint.
const SecretPrefix = "whsec_"
SecretPrefix starts every endpoint secret, as Standard Webhooks names it.
Variables ¶
var ErrNotFound = errors.New("no webhook by that name")
ErrNotFound is a name no endpoint has.
Functions ¶
func CheckURL ¶
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 ¶
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 SetDisabled ¶
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 ¶
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.
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.