webhook

package
v1.6.5 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package webhook manages workspace webhooks and the signing/delivery of their JSON payloads. Signing secrets are encrypted at rest.

Index

Constants

View Source
const EventTest = "webhook.test"

EventTest is the synthetic event used by the test endpoint.

View Source
const SignatureHeader = "X-Miabi-Signature"

SignatureHeader carries the HMAC-SHA256 signature of the request body.

Variables

View Source
var (
	ErrNotFound     = errors.New("webhook not found")
	ErrURLRequired  = errors.New("webhook url is required")
	ErrURLInvalid   = errors.New("webhook url must be a valid http(s) url")
	ErrURLBlocked   = errors.New("webhook url targets a disallowed (internal) address")
	ErrInvalidEvent = errors.New("unknown or non-notifiable event")
)

Functions

func BuildPayload

func BuildPayload(e *models.AppEvent) ([]byte, error)

BuildPayload serializes an application event into a webhook payload.

func Deliver

func Deliver(ctx context.Context, client *http.Client, url, secret string, headers map[string]string, body []byte) (int, error)

Deliver POSTs a signed JSON body to url and returns the HTTP status code. A transport error returns (0, err); a non-2xx/3xx status is reported via the returned code (the caller decides whether to retry). User-supplied headers are applied first; Content-Type, User-Agent, and the signature cannot be overridden.

func GenerateSecret

func GenerateSecret() (string, error)

GenerateSecret returns a 64-char hex string from 32 random bytes.

func Sign

func Sign(secret string, body []byte) string

Sign returns the hex HMAC-SHA256 of body keyed by secret.

Types

type Enqueuer

type Enqueuer interface {
	EnqueueWebhookDeliver(webhookID, eventID uint) error
}

Enqueuer schedules an asynchronous webhook delivery. Implemented by the worker producer; an interface here avoids a webhook->worker import cycle.

type Input

type Input struct {
	Name    string
	URL     string
	Events  []string
	Headers map[string]string
	Enabled bool
}

Input describes a webhook to create or update.

type Payload

type Payload struct {
	Event           string            `json:"event"`
	WorkspaceID     uint              `json:"workspace_id"`
	ApplicationID   uint              `json:"application_id,omitempty"`
	ApplicationName string            `json:"application_name,omitempty"`
	ApplicationSlug string            `json:"application_slug,omitempty"`
	Severity        string            `json:"severity,omitempty"`
	Message         string            `json:"message,omitempty"`
	Metadata        map[string]string `json:"metadata,omitempty"`
	Timestamp       string            `json:"timestamp"`
}

Payload is the JSON body POSTed to a webhook endpoint.

type Service

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

Service manages webhook configuration and synchronous test delivery.

func (*Service) Create

func (s *Service) Create(workspaceID uint, in Input) (*models.Webhook, error)

func (*Service) Delete

func (s *Service) Delete(workspaceID, id uint) error

func (*Service) Deliveries

func (s *Service) Deliveries(workspaceID, id uint, limit int) ([]models.WebhookDelivery, error)

Deliveries returns a webhook's recent delivery log.

func (*Service) Get

func (s *Service) Get(workspaceID, id uint) (*models.Webhook, error)

func (*Service) List

func (s *Service) List(workspaceID uint) ([]models.Webhook, error)

func (*Service) Redeliver

func (s *Service) Redeliver(workspaceID, webhookID, deliveryID uint) error

Redeliver re-sends a past delivery's source event to its webhook (async). If the delivery was a test (no source event), a fresh test delivery is sent.

func (*Service) Reencrypt

func (s *Service) Reencrypt(ctx context.Context, workspaceID uint) (int, error)

Reencrypt re-encrypts this owner's workspace secrets under the workspace's active DEK (key rotation). Idempotent; returns the number rewritten.

func (*Service) Test

func (s *Service) Test(ctx context.Context, workspaceID, id uint) error

Test synchronously delivers a signed test payload and records the outcome, returning any delivery error to the caller for immediate feedback.

func (*Service) Update

func (s *Service) Update(workspaceID, id uint, in Input) (*models.Webhook, error)

Jump to

Keyboard shortcuts

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