webhook

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultTransferAction = "default"
)
View Source
const Timeout = 30 * time.Second

Variables

View Source
var (
	ErrIdentityRequired    = errors.New("identity payload is required in webhook response")
	ErrTransactionRequired = errors.New("pending or transaction payload is required in webhook response")
)
View Source
var (
	ErrInvalidHMACToken = errors.New("invalid authorization hmac token")
)
View Source
var MockConfig = config.WebhookConfig{
	URL: mockURL,
}

Functions

This section is empty.

Types

type HMAC added in v1.0.0

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

HMAC implements an authorization header to authenticate webhook requests using a similar shared secret mechanism as AWS4-HMAC-SHA256 as defined here: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html

Create an HMAC object with a 32 byte key and a string that represents the key ID. Headers then must be appended to the HMAC in order to generate the data to sign. Once signed, an HMAC-SHA256 signature is generated and can be used to create the Authorization header.

func NewHMAC added in v1.0.0

func NewHMAC(keyID string, key []byte) *HMAC

func (*HMAC) Append added in v1.0.0

func (h *HMAC) Append(header, value string)

Add the header and the header value to the HMAC object. Note that the order that the headers are added is important as the values will be appended without modification to the data to be signed. If the header is already present, it will not be added again. Headers are stored as lowercase values.

func (*HMAC) Authorization added in v1.0.0

func (h *HMAC) Authorization() (_ string, err error)

Authorization returns the full Authorization header that can be added to the request headers of an outgoing webhook request.

func (*HMAC) Headers added in v1.0.0

func (h *HMAC) Headers() string

Headers returns the semicolon list of request headers used to compute the signature. The list includes header names only, and the header names must be in lowercase. The order of the headers indicates the order the data was appended to create the signature.

func (*HMAC) Key added in v1.0.0

func (h *HMAC) Key() string

Key returns the key ID used to create the HMAC signature.

func (*HMAC) Nonce added in v1.0.0

func (h *HMAC) Nonce() string

Nonce returns the base64 encoded nonce that is used to prevent replay attacks.

func (*HMAC) Signature added in v1.0.0

func (h *HMAC) Signature() (_ string, err error)

Signature returns the base64 encoded HMAC-SHA256 signature of the data.

type HMACToken added in v1.0.0

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

HMACToken is parsed from an Authorization or Server-Authorization header and is used to verify the signature of a webhook request.

func ParseHMAC added in v1.0.0

func ParseHMAC(token string) (tok *HMACToken, err error)

func (*HMACToken) Collect added in v1.0.0

func (h *HMACToken) Collect(headers http.Header)

func (*HMACToken) Headers added in v1.0.0

func (h *HMACToken) Headers() []string

func (*HMACToken) KeyID added in v1.0.0

func (h *HMACToken) KeyID() string

func (*HMACToken) Verify added in v1.0.0

func (h *HMACToken) Verify(key []byte) (bool, error)

type Handler

type Handler interface {
	Callback(context.Context, *Request) (*Reply, error)
}

func New

func New(conf config.WebhookConfig) (_ Handler, err error)

New returns a webhook handler that will POST callbacks to the webhook specified by the given URL. If the "mock" scheme is specified for the URL, then a MockCallback handler will be returned for external testing purposes.

type Mock

type Mock struct {
	OnCallback func(context.Context, *Request) (*Reply, error)
	Callbacks  int
}

Mock implements the webhook Handler and is used for testing webhook interactions.

func NewMock added in v1.0.0

func NewMock() *Mock

func (*Mock) Callback

func (m *Mock) Callback(ctx context.Context, req *Request) (*Reply, error)

func (*Mock) Reset

func (m *Mock) Reset()

func (*Mock) UseError

func (m *Mock) UseError(err error)

func (*Mock) UseFixture

func (m *Mock) UseFixture(path string) (err error)

func (*Mock) UseReply

func (m *Mock) UseReply(rep *Reply)

type Payload

type Payload struct {
	Identity    *ivms101.IdentityPayload `json:"identity"`
	Pending     *generic.Pending         `json:"pending,omitempty"`
	Transaction *generic.Transaction     `json:"transaction,omitempty"`
	Sunrise     *generic.Sunrise         `json:"sunrise,omitempty"`
	SentAt      string                   `json:"sent_at"`
	ReceivedAt  string                   `json:"received_at,omitempty"`
}

Payload is a denormalized representation of a TRISA payload that includes type-specific data structures. The payload should always have an identity IVMS101 payload and a sent at timestamp. It will have either a pending message or a transaction but not both. If payload is in an envelope with an accepted or completed transfer state it will have a received at timestamp as well.

func (*Payload) IsZero

func (p *Payload) IsZero() bool

func (*Payload) Proto

func (p *Payload) Proto() (payload *trisa.Payload, err error)

Convert payload (usually from a reply) into a TRISA protocol buffer struct.

type Reply

type Reply struct {
	TransactionID  uuid.UUID    `json:"transaction_id"`
	Error          *trisa.Error `json:"error,omitempty"`
	Payload        *Payload     `json:"payload,omitempty"`
	TransferAction string       `json:"transfer_action,omitempty"`
}

Reply represents the expected response from the callback webhook to the Envoy node. Either an error or a pending message is returned in the common case, though Envoy will also handle synchronous compliance responses.

func MockErrorReply

func MockErrorReply(_ context.Context, req *Request) (*Reply, error)

func MockPendingReply

func MockPendingReply(_ context.Context, req *Request) (*Reply, error)

func (*Reply) TransferState

func (r *Reply) TransferState() trisa.TransferState

Determine the API transfer state based on the reply

type Request

type Request struct {
	TransactionID uuid.UUID         `json:"transaction_id"`
	Timestamp     string            `json:"timestamp"`
	Counterparty  *api.Counterparty `json:"counterparty"`
	HMAC          string            `json:"hmac_signature,omitempty"`
	PKS           string            `json:"public_key_signature,omitempty"`
	TransferState string            `json:"transfer_state,omitempty"`
	Error         *trisa.Error      `json:"error,omitempty"`
	Payload       *Payload          `json:"payload,omitempty"`
}

The Request object is sent to the webhook via a POST http call. The request represents an incoming message to the server as an unsealed, decrypted secure envelope (whether the request came from a TRISA or TRP remote client). The request is guaranteed to have a transaction ID, timestamp, and counterparty. If it has a payload, then it will also have an HMAC signature and public key signature. Requests will have either errors or payloads, but not both.

func (*Request) AddPayload

func (r *Request) AddPayload(payload *trisa.Payload) (err error)

Add a TRISA protocol buffer payload to the webhook request, unmarshaling it into its denormalized JSON representation to conduct the request.

type Webhook

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

Webhook implements the Handler to make POST requests to the webhook URL.

func (*Webhook) Callback

func (h *Webhook) Callback(ctx context.Context, out *Request) (in *Reply, err error)

Jump to

Keyboard shortcuts

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