Permalink
Browse files

Merge pull request #123 from netlify/hook-fixes

Hook fixes
  • Loading branch information...
biilmann committed Nov 3, 2017
2 parents 74844fc + dc92db4 commit d578de1f6ed2ae8cd72b9b67de0bef7d23a0d786
Showing with 34 additions and 15 deletions.
  1. +3 −3 api/external.go
  2. +5 −3 api/hook_test.go
  3. +21 −4 api/hooks.go
  4. +1 −1 api/signup.go
  5. +1 −1 api/token.go
  6. +2 −2 api/verify.go
  7. +1 −1 conf/configuration.go
View
@@ -145,7 +145,7 @@ func (a *API) internalExternalProviderCallback(w http.ResponseWriter, r *http.Re
}
if config.Webhook.HasEvent("signup") {
if err := triggerHook(SignupEvent, user, instanceID, config.Webhook.Secret, &config.Webhook); err != nil {
if err := triggerHook(SignupEvent, user, instanceID, config); err != nil {
return err
}
a.db.UpdateUser(user)
@@ -199,7 +199,7 @@ func (a *API) internalExternalProviderCallback(w http.ResponseWriter, r *http.Re
}
if config.Webhook.HasEvent("signup") {
if err := triggerHook(SignupEvent, user, instanceID, config.Webhook.Secret, &config.Webhook); err != nil {
if err := triggerHook(SignupEvent, user, instanceID, config); err != nil {
return err
}
a.db.UpdateUser(user)
@@ -209,7 +209,7 @@ func (a *API) internalExternalProviderCallback(w http.ResponseWriter, r *http.Re
user.Confirm()
} else {
if config.Webhook.HasEvent("login") {
if err := triggerHook(LoginEvent, user, instanceID, config.Webhook.Secret, &config.Webhook); err != nil {
if err := triggerHook(LoginEvent, user, instanceID, config); err != nil {
return err
}
a.db.UpdateUser(user)
View
@@ -34,11 +34,13 @@ func TestSignupHookSendInstanceID(t *testing.T) {
}))
defer svr.Close()
config := &conf.WebhookConfig{
URL: svr.URL,
config := &conf.Configuration{
Webhook: conf.WebhookConfig{
URL: svr.URL,
},
}
require.NoError(t, triggerHook(SignupEvent, user, "myinstance", "", config))
require.NoError(t, triggerHook(SignupEvent, user, "myinstance", config))
assert.Equal(t, 1, callCount)
}
View
@@ -7,9 +7,11 @@ import (
"net"
"net/http"
"net/http/httptrace"
"net/url"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/netlify/gotrue/conf"
@@ -140,10 +142,24 @@ func closeBody(rsp *http.Response) {
}
}
func triggerHook(event HookEvent, user *models.User, instanceID, jwtSecret string, hconfig *conf.WebhookConfig) error {
if hconfig.URL == "" {
func triggerHook(event HookEvent, user *models.User, instanceID string, config *conf.Configuration) error {
if config.Webhook.URL == "" {
return nil
}
hookURL, err := url.Parse(config.Webhook.URL)
if err != nil {
return errors.Wrapf(err, "Failed to parse Webhook URL")
}
if !hookURL.IsAbs() {
siteURL, err := url.Parse(config.SiteURL)
if err != nil {
return errors.Wrapf(err, "Failed to parse Site URL")
}
hookURL.Scheme = siteURL.Scheme
hookURL.Host = siteURL.Host
hookURL.User = siteURL.User
}
payload := struct {
Event HookEvent `json:"event"`
@@ -159,8 +175,8 @@ func triggerHook(event HookEvent, user *models.User, instanceID, jwtSecret strin
return internalServerError("Failed to serialize the data for signup webhook").WithInternalError(err)
}
w := Webhook{
WebhookConfig: hconfig,
jwtSecret: jwtSecret,
WebhookConfig: &config.Webhook,
jwtSecret: config.Webhook.Secret,
instanceID: instanceID,
claims: &jwt.StandardClaims{
IssuedAt: time.Now().Unix(),
@@ -169,6 +185,7 @@ func triggerHook(event HookEvent, user *models.User, instanceID, jwtSecret strin
},
payload: data,
}
w.URL = hookURL.String()
body, err := w.trigger()
defer func() {
View
@@ -100,7 +100,7 @@ func (a *API) signupNewUser(ctx context.Context, params *SignupParams, aud strin
user.SetRole(config.JWT.DefaultGroupName)
if config.Webhook.HasEvent("validate") {
if err := triggerHook(ValidateEvent, user, instanceID, config.Webhook.Secret, &config.Webhook); err != nil {
if err := triggerHook(ValidateEvent, user, instanceID, config); err != nil {
return nil, err
}
}
View
@@ -76,7 +76,7 @@ func (a *API) ResourceOwnerPasswordGrant(ctx context.Context, w http.ResponseWri
}
if config.Webhook.HasEvent("login") {
if err := triggerHook(LoginEvent, user, instanceID, config.Webhook.Secret, &config.Webhook); err != nil {
if err := triggerHook(LoginEvent, user, instanceID, config); err != nil {
return err
}
a.db.UpdateUser(user)
View
@@ -87,7 +87,7 @@ func (a *API) signupVerify(ctx context.Context, params *VerifyParams) (*models.U
}
if config.Webhook.HasEvent("signup") {
if err := triggerHook(SignupEvent, user, instanceID, config.Webhook.Secret, &config.Webhook); err != nil {
if err := triggerHook(SignupEvent, user, instanceID, config); err != nil {
return nil, err
}
a.db.UpdateUser(user)
@@ -111,7 +111,7 @@ func (a *API) recoverVerify(ctx context.Context, params *VerifyParams) (*models.
user.Recover()
if !user.IsConfirmed() {
if config.Webhook.HasEvent("signup") {
if err := triggerHook(SignupEvent, user, instanceID, config.Webhook.Secret, &config.Webhook); err != nil {
if err := triggerHook(SignupEvent, user, instanceID, config); err != nil {
return nil, err
}
a.db.UpdateUser(user)
View
@@ -116,7 +116,7 @@ type WebhookConfig struct {
URL string `json:"url"`
Retries int `json:"retries"`
TimeoutSec int `json:"timeout_sec"`
Secret string `json:"jwt_secret"`
Secret string `json:"secret"`
Events []string `json:"events"`
}

0 comments on commit d578de1

Please sign in to comment.