hooks

package
v2.700.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package hooks provides shared Standard Webhooks construction helpers and wiring for go-service.

This package integrates the Standard Webhooks Go library (github.com/standard-webhooks/standard-webhooks/libraries/go) by providing:

Secrets and source strings

Each value in Config.Secrets is a "source string" and may be:

  • "env:NAME" to read the secret from an environment variable,
  • "file:/path/to/secret" to read the secret from a file, or
  • any other value treated as the literal secret.

The resolved secret bytes are passed to the Standard Webhooks library as a string. Empty resolved secrets are rejected by this package, while non-empty secrets must use a format accepted by the Standard Webhooks constructor. The Generator output is accepted directly as a literal secret. Keep this value private and avoid logging it.

Config.Key selects the active entry in Config.Secrets used for signing. Verification accepts signatures from every configured secret because Standard Webhooks signs a message id, timestamp, and payload, but does not include a signing key id that could select one secret directly.

Downstream integrations

Transport integrations (for example github.com/alexfalkowski/go-service/v2/transport/http/hooks) build on top of this package to verify incoming webhook signatures and/or sign outbound webhook requests.

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptySecret = errors.New("hooks: empty secret")

ErrEmptySecret indicates that webhook secret configuration resolved to empty bytes.

View Source
var ErrInvalidConfig = errors.New("hooks: invalid config")

ErrInvalidConfig indicates webhook secret rotation configuration is incomplete.

Module wires shared Standard Webhooks helpers into go.uber.org/fx/go.uber.org/dig.

It provides constructors for:

  • *Generator (via NewGenerator), which generates new secret values suitable for Standard Webhooks, and
  • *Hook (via NewHook), which constructs a signer/verifier from configuration.

Disabled behavior: if hooks configuration is disabled (nil *Config), NewHook returns (nil, nil) so downstream consumers can treat webhook verification/signing as optional.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Secrets contains named Standard Webhooks secrets trusted for verification.
	//
	// Key selects the active secret used for signing.
	// Verification accepts signatures from every configured secret.
	Secrets Secrets `yaml:"secrets,omitempty" json:"secrets,omitempty" toml:"secrets,omitempty"`

	// Key is the active secret id used for signing.
	//
	// The selected entry must exist in Secrets.
	Key string `yaml:"key,omitempty" json:"key,omitempty" toml:"key,omitempty"`

	// Leeway is the optional clock-skew tolerance applied to the Webhook-Timestamp
	// freshness check during verification.
	//
	// A zero value keeps the Standard Webhooks library's fixed 5-minute freshness
	// window. A non-zero value replaces that fixed window with this configured
	// tolerance, matching the clock-skew Leeway already exposed by the JWT,
	// PASETO, and SSH token verifiers.
	Leeway time.Duration `yaml:"leeway,omitempty" json:"leeway,omitempty" toml:"leeway,omitempty" validate:"omitempty,duration_second_precision"`
}

Config configures Standard Webhooks secret loading.

func (*Config) IsEnabled added in v2.115.0

func (c *Config) IsEnabled() bool

IsEnabled reports whether hooks configuration is present.

By convention across go-service config types, a nil *Config is treated as "disabled".

type Generator

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

Generator generates secret values suitable for Standard Webhooks.

func NewGenerator

func NewGenerator(gen *rand.Generator) *Generator

NewGenerator constructs a Generator for creating Standard Webhooks secrets.

The returned Generator uses a cryptographically-secure random generator.

func (*Generator) Generate

func (g *Generator) Generate() (string, error)

Generate returns a new base64-encoded Standard Webhooks secret string.

It generates 32 random bytes and encodes them using standard base64. The returned string can be used as a literal value in Config.Secrets. If the underlying random generator fails, Generate returns that error.

type Hook added in v2.636.0

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

Hook signs and verifies Standard Webhooks signatures.

A Hook has one active signer and one or more trusted verifiers. This supports secret rotation by signing with the active configured secret while accepting signatures from the full configured secret set.

func NewHook added in v2.73.0

func NewHook(fs *os.FS, cfg *Config) (*Hook, error)

NewHook constructs a Standard Webhooks hook from cfg.

Disabled behavior: if cfg is nil/disabled, NewHook returns (nil, nil).

Enabled behavior: NewHook resolves Config.Secrets and constructs Standard Webhooks instances using those secrets. Source-resolution errors and Standard Webhooks constructor errors are returned to the caller.

Secret encoding expectations: The secret bytes are converted to a string and passed to the Standard Webhooks library. In typical setups this string is the base64-encoded secret generated by Generator.Generate, with or without the Standard Webhooks `whsec_` prefix, but NewHook does not enforce a particular encoding beyond what the downstream library expects. Empty resolved secrets are rejected with ErrEmptySecret.

func (*Hook) Sign added in v2.636.0

func (h *Hook) Sign(id string, timestamp time.Time, payload []byte) (string, error)

Sign computes a Standard Webhooks signature with the active secret.

func (*Hook) Verify added in v2.636.0

func (h *Hook) Verify(payload []byte, headers http.Header) error

Verify verifies payload and headers against the configured trusted secrets.

When Config.Leeway is zero, the Webhook-Timestamp freshness check uses the Standard Webhooks library's fixed 5-minute window. When Config.Leeway is non-zero, Verify checks Webhook-Timestamp against that configured window itself, then verifies the signature ignoring the library's own timestamp check.

type Secrets added in v2.636.0

type Secrets map[string]string

Secrets maps secret ids to Standard Webhooks secret source strings.

func (Secrets) Get added in v2.636.0

func (s Secrets) Get(id string) string

Get returns the secret with id.

Jump to

Keyboard shortcuts

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