api.go107 lines, 3.6 KB
| 1 | // Package webhook delivers events to our users' servers: signed, retried, and |
| 2 | // logged, so they never have to poll us. |
| 3 | package webhook |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/fiston-user/payments/internal/platform/apperr" |
| 12 | "github.com/fiston-user/payments/internal/platform/authn" |
| 13 | "github.com/fiston-user/payments/internal/platform/httpx" |
| 14 | "github.com/fiston-user/payments/internal/transfer/transferevents" |
| 15 | "github.com/fiston-user/payments/internal/webhook/signature" |
| 16 | "github.com/fiston-user/payments/internal/webhook/webhookdb" |
| 17 | "github.com/google/uuid" |
| 18 | ) |
| 19 | |
| 20 | // EventTypes are the events an endpoint can subscribe to. |
| 21 | var EventTypes = []string{transferevents.Completed, transferevents.Failed, transferevents.ReviewRequired} |
| 22 | |
| 23 | var ( |
| 24 | ErrEndpointNotFound = fmt.Errorf("webhook endpoint %w", apperr.ErrNotFound) |
| 25 | ErrDeliveryNotFound = fmt.Errorf("webhook delivery %w", apperr.ErrNotFound) |
| 26 | ) |
| 27 | |
| 28 | // API lets users manage their endpoints, and read and replay their deliveries. |
| 29 | type API struct { |
| 30 | q *webhookdb.Queries |
| 31 | production bool |
| 32 | wake func() // tells the worker there's something to deliver |
| 33 | } |
| 34 | |
| 35 | // NewAPI returns an API on q. |
| 36 | func NewAPI(q *webhookdb.Queries, production bool, wake func()) *API { |
| 37 | return &API{q: q, production: production, wake: wake} |
| 38 | } |
| 39 | |
| 40 | // Register adds the webhook routes to mux. |
| 41 | func (a *API) Register(mux *http.ServeMux) { |
| 42 | mux.Handle("POST /v1/webhooks/endpoints", httpx.HandlerFunc(a.create)) |
| 43 | mux.Handle("GET /v1/webhooks/endpoints", httpx.HandlerFunc(a.list)) |
| 44 | mux.Handle("DELETE /v1/webhooks/endpoints/{id}", httpx.HandlerFunc(a.delete)) |
| 45 | mux.Handle("GET /v1/webhooks/deliveries", httpx.HandlerFunc(a.deliveries)) |
| 46 | mux.Handle("POST /v1/webhooks/deliveries/{id}/replay", httpx.HandlerFunc(a.replay)) |
| 47 | } |
| 48 | |
| 49 | func (a *API) create(w http.ResponseWriter, r *http.Request) error { |
| 50 | var req struct { |
| 51 | URL string `json:"url"` |
| 52 | EventTypes []string `json:"event_types"` |
| 53 | } |
| 54 | if err := httpx.DecodeJSON(w, r, &req); err != nil { |
| 55 | return err |
| 56 | } |
| 57 | var v apperr.ValidationError |
| 58 | if err := checkURL(req.URL, a.production); err != nil { |
| 59 | v.Add("url", err.Error()) |
| 60 | } |
| 61 | unknown := func(t string) bool { return !slices.Contains(EventTypes, t) } |
| 62 | if len(req.EventTypes) == 0 || slices.ContainsFunc(req.EventTypes, unknown) { |
| 63 | v.Add("event_types", "must list one or more of "+strings.Join(EventTypes, ", ")) |
| 64 | } |
| 65 | if err := v.Err(); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | user, _ := authn.UserID(r.Context()) |
| 69 | endpoint, err := a.q.CreateEndpoint(r.Context(), webhookdb.CreateEndpointParams{ |
| 70 | ID: uuid.Must(uuid.NewV7()), UserID: user, URL: req.URL, |
| 71 | EventTypes: req.EventTypes, Secret: signature.NewSecret(), |
| 72 | }) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | // The one response with the secret in it: the user keeps it, to verify |
| 77 | // what we send. Listing endpoints never shows it again. |
| 78 | httpx.WriteJSON(w, r, http.StatusCreated, endpoint) |
| 79 | return nil |
| 80 | } |
| 81 | |
| 82 | func (a *API) list(w http.ResponseWriter, r *http.Request) error { |
| 83 | user, _ := authn.UserID(r.Context()) |
| 84 | endpoints, err := a.q.ListEndpoints(r.Context(), user) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | httpx.WriteJSON(w, r, http.StatusOK, map[string]any{"endpoints": endpoints}) |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | func (a *API) delete(w http.ResponseWriter, r *http.Request) error { |
| 93 | id, err := uuid.Parse(r.PathValue("id")) |
| 94 | if err != nil { |
| 95 | return ErrEndpointNotFound |
| 96 | } |
| 97 | user, _ := authn.UserID(r.Context()) |
| 98 | n, err := a.q.DeleteEndpoint(r.Context(), webhookdb.DeleteEndpointParams{ID: id, UserID: user}) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | if n == 0 { |
| 103 | return ErrEndpointNotFound // or someone else's: the same, to them |
| 104 | } |
| 105 | w.WriteHeader(http.StatusNoContent) |
| 106 | return nil |
| 107 | } |