Permalink
Browse files

Merge pull request #104 from netlify/relative-webhook

Make relative webhooks work in multi tennant mode
  • Loading branch information...
biilmann committed Nov 14, 2017
2 parents 70eb276 + b5c5910 commit 5ad5fdd220a377ee27d3fbc34746feaf4720d5c0
Showing with 36 additions and 7 deletions.
  1. +8 −2 api/order.go
  2. +8 −2 api/payments.go
  3. +20 −3 models/hook.go
View
@@ -401,7 +401,10 @@ func (a *API) OrderCreate(w http.ResponseWriter, r *http.Request) error {
tx.Create(order)
models.LogEvent(tx, r.RemoteAddr, order.UserID, order.ID, models.EventCreated, nil)
if config.Webhooks.Order != "" {
hook := models.NewHook("order", config.Webhooks.Order, order.UserID, config.Webhooks.Secret, order)
hook, err := models.NewHook("order", config.SiteURL, config.Webhooks.Order, order.UserID, config.Webhooks.Secret, order)
if err != nil {
log.WithError(err).Error("Failed to process webhook")
}
tx.Save(hook)
}
tx.Commit()
@@ -565,7 +568,10 @@ func (a *API) OrderUpdate(w http.ResponseWriter, r *http.Request) error {
models.LogEvent(tx, r.RemoteAddr, claims.Subject, existingOrder.ID, models.EventUpdated, changes)
if config.Webhooks.Update != "" {
// TODO should this be claims.Subject or existingOrder.UserID ?
hook := models.NewHook("update", config.Webhooks.Update, claims.Subject, config.Webhooks.Secret, existingOrder)
hook, err := models.NewHook("update", config.SiteURL, config.Webhooks.Update, claims.Subject, config.Webhooks.Secret, existingOrder)
if err != nil {
log.WithError(err).Error("Failed to process web hook")
}
tx.Save(hook)
}
if rsp := tx.Commit(); rsp.Error != nil {
View
@@ -180,7 +180,10 @@ func (a *API) PaymentCreate(w http.ResponseWriter, r *http.Request) error {
tx.Save(order)
if config.Webhooks.Payment != "" {
hook := models.NewHook("payment", config.Webhooks.Payment, order.UserID, config.Webhooks.Secret, order)
hook, err := models.NewHook("payment", config.SiteURL, config.Webhooks.Payment, order.UserID, config.Webhooks.Secret, order)
if err != nil {
log.WithError(err).Error("Failed to process webhook")
}
tx.Save(hook)
}
@@ -307,7 +310,10 @@ func (a *API) PaymentRefund(w http.ResponseWriter, r *http.Request) error {
log.Infof("Finished transaction with %s: %s", provID, m.ProcessorID)
tx.Save(m)
if config.Webhooks.Refund != "" {
hook := models.NewHook("refund", config.Webhooks.Refund, m.UserID, config.Webhooks.Secret, m)
hook, err := models.NewHook("refund", config.SiteURL, config.Webhooks.Refund, m.UserID, config.Webhooks.Secret, m)
if err != nil {
log.WithError(err).Error("Failed to process webhook")
}
tx.Save(hook)
}
tx.Commit()
View
@@ -5,12 +5,14 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"sync"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/jinzhu/gorm"
"github.com/pborman/uuid"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@@ -54,15 +56,30 @@ func (Hook) TableName() string {
}
// NewHook creates a Hook model.
func NewHook(hookType, url, userID, secret string, payload interface{}) *Hook {
func NewHook(hookType, siteURL, hookURL, userID, secret string, payload interface{}) (*Hook, error) {
fullHookURL, err := url.Parse(hookURL)
if err != nil {
return nil, errors.Wrapf(err, "Failed to parse Webhook URL")
}
if !fullHookURL.IsAbs() {
fullSiteURL, err := url.Parse(siteURL)
if err != nil {
return nil, errors.Wrapf(err, "Failed to parse Site URL")
}
fullHookURL.Scheme = fullSiteURL.Scheme
fullHookURL.Host = fullSiteURL.Host
fullHookURL.User = fullSiteURL.User
}
json, _ := json.Marshal(payload)
return &Hook{
Type: hookType,
UserID: userID,
URL: url,
URL: fullHookURL.String(),
Secret: secret,
Payload: string(json),
}
}, nil
}
// Trigger creates and executes the HTTP request for a Hook.

0 comments on commit 5ad5fdd

Please sign in to comment.