Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward request context inside webhook handler #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions webhooks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocardless

import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
Expand All @@ -12,15 +13,15 @@ import (

// EventHandler is the interface that must be implemented to handle events from a webhook.
type EventHandler interface {
HandleEvent(Event) error
HandleEvent(context.Context, Event) error
}

// EventHandlerFunc can be used to convert a function into an EventHandler
type EventHandlerFunc func(Event) error
type EventHandlerFunc func(context.Context, Event) error

// HandleEvent will call the EventHandlerFunc function
func (h EventHandlerFunc) HandleEvent(e Event) error {
return h(e)
func (h EventHandlerFunc) HandleEvent(ctx context.Context, e Event) error {
return h(ctx, e)
}

// WebhookHandler allows you to process incoming events from webhooks.
Expand All @@ -43,7 +44,7 @@ func NewWebhookHandler(secret string, h EventHandler) (*WebhookHandler, error) {
// ServeHTTP processes incoming webhooks and dispatches events to the corresponsing handlers.
func (h *WebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sig, err := hex.DecodeString(r.Header.Get("Webhook-Signature"))
if len(sig) == 0 {
if len(sig) == 0 || err != nil {
http.Error(w, "invalid signature", 498)
return
}
Expand All @@ -65,7 +66,7 @@ func (h *WebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

for _, event := range events.Events {
err := h.HandleEvent(event)
err := h.HandleEvent(r.Context(), event)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
7 changes: 4 additions & 3 deletions webhooks_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocardless

import (
"context"
"errors"
"net/http"
"net/http/httptest"
Expand All @@ -9,7 +10,7 @@ import (
)

func TestWebhookFailsWithInvalidSignature(t *testing.T) {
wh, err := NewWebhookHandler("testing", EventHandlerFunc(func(e Event) error {
wh, err := NewWebhookHandler("testing", EventHandlerFunc(func(ctx context.Context, e Event) error {
t.Error("unexpected call")
return nil
}))
Expand All @@ -36,7 +37,7 @@ func TestWebhookFailsWithInvalidSignature(t *testing.T) {
func TestWebhookFailsWithValidSignature(t *testing.T) {
var called int

wh, err := NewWebhookHandler("testing", EventHandlerFunc(func(e Event) error {
wh, err := NewWebhookHandler("testing", EventHandlerFunc(func(ctx context.Context, e Event) error {
called++
expectedID := "EVTESTNE86TNZS"
if e.Id != expectedID {
Expand Down Expand Up @@ -72,7 +73,7 @@ func TestWebhookFailsWithValidSignature(t *testing.T) {
func TestWebhookWhenHandlerFails(t *testing.T) {
var called int

wh, err := NewWebhookHandler("testing", EventHandlerFunc(func(e Event) error {
wh, err := NewWebhookHandler("testing", EventHandlerFunc(func(ctx context.Context, e Event) error {
called++
expectedID := "EVTESTNE86TNZS"
if e.Id != expectedID {
Expand Down