hmacsig

package
v1.138.3 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package hmacsig is the one definition of a webhook HMAC signature: which bytes are signed, how the signature is written into its header, and how a header is read back into the signatures it carries (#1996).

The platform verifies these signatures on its inbound webhook sources (internal/webhook/whauth) and writes them on an outbound connection whose auth_mode is hmac (internal/upstreamauth). Both build on this package, so a connection and a source configured with the same values agree by construction rather than by two implementations being kept in step.

It knows nothing about requests, sources or connections: a caller hands it a Scheme, the secret, the delivery id and timestamp it read or chose, and the body.

Index

Constants

View Source
const (
	AlgorithmSHA256 = "sha256"
	AlgorithmSHA1   = "sha1"
	AlgorithmSHA512 = "sha512"
)

Algorithms a scheme signs with.

View Source
const (
	EncodingHex    = "hex"
	EncodingBase64 = "base64"
)

Encodings a signature is written in.

View Source
const (
	// SignedBody signs the raw body.
	SignedBody = "body"
	// SignedTimestampBody signs the timestamp, a ".", and the raw body,
	// which is what makes a replayed request with an old timestamp fail
	// even though its signature once verified.
	SignedTimestampBody = "timestamp.body"
	// SignedIDTimestampBody signs the delivery id, a ".", the timestamp, a
	// ".", and the raw body: the Standard Webhooks form.
	SignedIDTimestampBody = "id.timestamp.body"
)

What a scheme signs.

View Source
const (
	// FormatPlain writes the prefix and the signature. A header may carry
	// several, separated by spaces, which is how a sender rotating its
	// secret signs with both (Standard Webhooks' "v1,<a> v1,<b>").
	FormatPlain = ""
	// FormatStripe writes "t=<timestamp>,v1=<signature>": the timestamp is
	// in the signature header rather than a header of its own, and the
	// signed bytes are always the timestamp, a ".", and the body.
	FormatStripe = "stripe"
)

How the signature header is written.

View Source
const (
	PresetStandardWebhooks = "standard_webhooks"
	PresetGitHub           = "github"
	PresetStripe           = "stripe"
	// PresetPlatform is what the platform's own inbound webhook
	// documentation configures a source with, so a deployment's script can
	// deliver to another deployment's source with one setting.
	PresetPlatform = "platform"
)

Presets by name.

Variables

View Source
var ErrMalformed = errors.New("hmacsig: the signature header is malformed")

ErrMalformed is returned for a signature header that cannot be read as the scheme writes one.

Functions

func ValidAlgorithm

func ValidAlgorithm(a string) bool

ValidAlgorithm reports whether a is an algorithm a scheme signs with.

func ValidEncoding

func ValidEncoding(e string) bool

ValidEncoding reports whether e is an encoding a signature is written in.

func ValidFormat

func ValidFormat(f string) bool

ValidFormat reports whether f names a signature header format.

func ValidSigned

func ValidSigned(v string) bool

ValidSigned reports whether v names what a scheme signs.

Types

type Preset

type Preset struct {
	Scheme
	SignatureHeader string
	TimestampHeader string
	IDHeader        string
}

Preset names a sender's whole signing convention in one setting.

func LookupPreset

func LookupPreset(name string) (Preset, bool)

LookupPreset returns the named convention.

type Scheme

type Scheme struct {
	Algorithm string
	Encoding  string
	Prefix    string
	Signed    string
	Format    string
}

Scheme is how one sender signs.

func (Scheme) Header

func (s Scheme) Header(secret, id, timestamp string, body []byte) string

Header is the signature header's value for one request.

func (Scheme) MAC

func (s Scheme) MAC(secret, id, timestamp string, body []byte) []byte

MAC computes the signature of the payload under secret.

func (Scheme) Matches

func (s Scheme) Matches(sig Signature, secrets []string, id, timestamp string, body []byte) bool

Matches reports whether any of the signatures is the scheme's signature of the payload under any of the secrets. Every comparison is constant-time.

func (Scheme) Parse

func (s Scheme) Parse(value string) (Signature, error)

Parse reads a signature header's value. A value none of whose signatures can be read is ErrMalformed.

func (Scheme) Payload

func (s Scheme) Payload(id, timestamp string, body []byte) []byte

Payload is the bytes the scheme signs for one request.

func (Scheme) UsesID

func (s Scheme) UsesID() bool

UsesID reports whether the signed bytes include a delivery id.

func (Scheme) UsesTimestamp

func (s Scheme) UsesTimestamp() bool

UsesTimestamp reports whether the signed bytes include a timestamp.

type Signature

type Signature struct {
	MACs      [][]byte
	Timestamp string
}

Signature is what a signature header carries: the signatures, decoded, and, for a format that writes it there, the timestamp.

Jump to

Keyboard shortcuts

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