signature

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package signature provides low-level helpers for optional signed ACP request headers.

HMACVerifier verifies a base64url HMAC-SHA256 signature over an RFC 3339 timestamp and canonical JSON body. Merchant-Signature on order webhooks uses a different wire format; use github.com/sumup/acp/acpwebhook to send those events.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsDuration

func AbsDuration(d time.Duration) time.Duration

AbsDuration returns the absolute value of the supplied duration.

func BuildSigningPayload

func BuildSigningPayload(ts time.Time, canonicalBody []byte) []byte

BuildSigningPayload constructs the canonical string that is HMAC-signed.

func CanonicalizeJSONBody

func CanonicalizeJSONBody(raw []byte) ([]byte, error)

CanonicalizeJSONBody normalizes arbitrary JSON into canonical form for signing.

func ParseTimestamp

func ParseTimestamp(value string) (time.Time, error)

ParseTimestamp accepts Timestamp header values in RFC3339 or RFC3339Nano format.

func ReadAndBufferBody

func ReadAndBufferBody(r *http.Request) ([]byte, error)

ReadAndBufferBody reads the request body while keeping it accessible for later handlers.

Types

type HMACVerifier

type HMACVerifier struct {
	Key []byte
}

HMACVerifier validates signatures that were produced by taking the base64url-encoded HMAC-SHA256 of `RFC3339(timestamp) + "." + canonicalJSON`.

Example
package main

import (
	"context"
	"crypto/hmac"
	"crypto/sha256"
	"encoding/base64"
	"fmt"
	"time"

	"github.com/sumup/acp/signature"
)

func main() {
	key := []byte("request-signing-secret")
	timestamp := time.Date(2026, 4, 17, 10, 30, 0, 0, time.UTC)
	body, err := signature.CanonicalizeJSONBody([]byte(`{"currency":"usd","line_items":[]}`))
	if err != nil {
		panic(err)
	}

	mac := hmac.New(sha256.New, key)
	_, _ = mac.Write(signature.BuildSigningPayload(timestamp, body))
	encoded := base64.RawURLEncoding.EncodeToString(mac.Sum(nil))

	err = (signature.HMACVerifier{Key: key}).Verify(context.Background(), signature.Material{
		Signature:     encoded,
		Timestamp:     timestamp,
		CanonicalBody: body,
	})
	fmt.Println(err)
}
Output:
<nil>

func (HMACVerifier) Verify

func (v HMACVerifier) Verify(_ context.Context, material Material) error

Verify implements Verifier by recomputing the expected HMAC signature.

type Material

type Material struct {
	Signature     string
	Timestamp     time.Time
	CanonicalBody []byte
	Method        string
	Path          string
	RawQuery      string
	Headers       http.Header
}

Material captures the inputs needed to validate a signed request.

type Verifier

type Verifier interface {
	Verify(ctx context.Context, material Material) error
}

Verifier validates the authenticity of incoming requests.

type VerifierFunc

type VerifierFunc func(ctx context.Context, material Material) error

VerifierFunc lifts bare functions into Verifier.

func (VerifierFunc) Verify

func (f VerifierFunc) Verify(ctx context.Context, material Material) error

Verify delegates to the wrapped function.

Jump to

Keyboard shortcuts

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