Permalink
Browse files

Merge pull request #161 from netlify/handle-hook-conditionals-in-trig…

…ger-hook

Handle hook conditionals in trigger hook
  • Loading branch information...
brycekahle committed Jun 6, 2018
2 parents feb3c89 + 0f19e02 commit cadf765a1cbad60a0d85d21da5cf55ce061817c1
Showing with 24 additions and 34 deletions.
  1. +6 −12 api/external.go
  2. +2 −1 api/hook_test.go
  3. +6 −1 api/hooks.go
  4. +4 −8 api/signup.go
  5. +2 −4 api/token.go
  6. +4 −8 api/verify.go
@@ -172,10 +172,8 @@ func (a *API) internalExternalProviderCallback(w http.ResponseWriter, r *http.Re
if terr := models.NewAuditLogEntry(tx, instanceID, user, models.UserSignedUpAction, nil); terr != nil {
return terr
}
if config.Webhook.HasEvent("signup") {
if terr = triggerHook(ctx, tx, SignupEvent, user, instanceID, config); terr != nil {
return terr
}
if terr = triggerHook(ctx, tx, SignupEvent, user, instanceID, config); terr != nil {
return terr
}
// fall through to auto-confirm and issue token
@@ -186,10 +184,8 @@ func (a *API) internalExternalProviderCallback(w http.ResponseWriter, r *http.Re
if terr := models.NewAuditLogEntry(tx, instanceID, user, models.LoginAction, nil); terr != nil {
return terr
}
if config.Webhook.HasEvent("login") {
if terr = triggerHook(ctx, tx, LoginEvent, user, instanceID, config); terr != nil {
return terr
}
if terr = triggerHook(ctx, tx, LoginEvent, user, instanceID, config); terr != nil {
return terr
}
}
}
@@ -250,10 +246,8 @@ func (a *API) processInvite(ctx context.Context, tx *storage.Connection, userDat
if err := models.NewAuditLogEntry(tx, instanceID, user, models.InviteAcceptedAction, nil); err != nil {
return nil, err
}
if config.Webhook.HasEvent("signup") {
if err := triggerHook(ctx, tx, SignupEvent, user, instanceID, config); err != nil {
return nil, err
}
if err := triggerHook(ctx, tx, SignupEvent, user, instanceID, config); err != nil {
return nil, err
}
// confirm because they were able to respond to invite email
@@ -46,7 +46,8 @@ func TestSignupHookSendInstanceID(t *testing.T) {
config := &conf.Configuration{
Webhook: conf.WebhookConfig{
URL: svr.URL,
URL: svr.URL,
Events: []string{SignupEvent},
},
}
@@ -154,12 +154,16 @@ func closeBody(rsp *http.Response) {
func triggerHook(ctx context.Context, conn *storage.Connection, event HookEvent, user *models.User, instanceID uuid.UUID, config *conf.Configuration) error {
var hookURL *url.URL
secret := config.Webhook.Secret
if config.Webhook.URL != "" {
var err error
hookURL, err = url.Parse(config.Webhook.URL)
if err != nil {
return errors.Wrapf(err, "Failed to parse Webhook URL")
}
if !config.Webhook.HasEvent(string(event)) {
return nil
}
}
if hookURL == nil {
@@ -174,6 +178,7 @@ func triggerHook(ctx context.Context, conn *storage.Connection, event HookEvent,
if err != nil {
return errors.Wrapf(err, "Failed to parse Event Function Hook URL")
}
secret = config.JWT.Secret
} else {
// abort hook call if there are no functions for this event
return nil
@@ -220,7 +225,7 @@ func triggerHook(ctx context.Context, conn *storage.Connection, event HookEvent,
w := Webhook{
WebhookConfig: &config.Webhook,
jwtSecret: config.Webhook.Secret,
jwtSecret: secret,
instanceID: instanceID,
claims: claims,
payload: data,
@@ -70,10 +70,8 @@ func (a *API) Signup(w http.ResponseWriter, r *http.Request) error {
if terr = models.NewAuditLogEntry(tx, instanceID, user, models.UserSignedUpAction, nil); terr != nil {
return terr
}
if config.Webhook.HasEvent("signup") {
if terr = triggerHook(ctx, tx, SignupEvent, user, instanceID, config); terr != nil {
return terr
}
if terr = triggerHook(ctx, tx, SignupEvent, user, instanceID, config); terr != nil {
return terr
}
if terr = user.Confirm(tx); terr != nil {
return internalServerError("Database error updating user").WithInternalError(terr)
@@ -119,10 +117,8 @@ func (a *API) signupNewUser(conn *storage.Connection, ctx context.Context, param
if terr := user.SetRole(tx, config.JWT.DefaultGroupName); terr != nil {
return internalServerError("Database error updating user").WithInternalError(terr)
}
if config.Webhook.HasEvent("validate") {
if terr := triggerHook(ctx, tx, ValidateEvent, user, instanceID, config); terr != nil {
return terr
}
if terr := triggerHook(ctx, tx, ValidateEvent, user, instanceID, config); terr != nil {
return terr
}
return nil
})
@@ -77,10 +77,8 @@ func (a *API) ResourceOwnerPasswordGrant(ctx context.Context, w http.ResponseWri
if terr = models.NewAuditLogEntry(tx, instanceID, user, models.LoginAction, nil); terr != nil {
return terr
}
if config.Webhook.HasEvent("login") {
if terr = triggerHook(ctx, tx, LoginEvent, user, instanceID, config); terr != nil {
return terr
}
if terr = triggerHook(ctx, tx, LoginEvent, user, instanceID, config); terr != nil {
return terr
}
token, terr = a.issueRefreshToken(ctx, tx, user)
@@ -106,10 +106,8 @@ func (a *API) signupVerify(ctx context.Context, conn *storage.Connection, params
return terr
}
if config.Webhook.HasEvent("signup") {
if terr = triggerHook(ctx, tx, SignupEvent, user, instanceID, config); terr != nil {
return terr
}
if terr = triggerHook(ctx, tx, SignupEvent, user, instanceID, config); terr != nil {
return terr
}
if terr = user.Confirm(tx); terr != nil {
@@ -144,10 +142,8 @@ func (a *API) recoverVerify(ctx context.Context, conn *storage.Connection, param
return terr
}
if config.Webhook.HasEvent("signup") {
if terr = triggerHook(ctx, tx, SignupEvent, user, instanceID, config); terr != nil {
return terr
}
if terr = triggerHook(ctx, tx, SignupEvent, user, instanceID, config); terr != nil {
return terr
}
if terr = user.Confirm(tx); terr != nil {
return terr

0 comments on commit cadf765

Please sign in to comment.