From 8a6056d4f5cea7145ee433d50115e133886bb8a8 Mon Sep 17 00:00:00 2001 From: apasternak Date: Tue, 20 Feb 2024 10:58:22 +0100 Subject: [PATCH] PLATFORM-9035 Fix webhook triggering conditions --- selfservice/hook/web_hook.go | 29 ++++++++++--------- selfservice/hook/web_hook_integration_test.go | 16 ++++++++-- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/selfservice/hook/web_hook.go b/selfservice/hook/web_hook.go index f4011cf2e314..c5bb5820a5bd 100644 --- a/selfservice/hook/web_hook.go +++ b/selfservice/hook/web_hook.go @@ -245,10 +245,11 @@ func (e *WebHook) ExecuteRegistrationPreHook(_ http.ResponseWriter, req *http.Re } func (e *WebHook) ExecutePostRegistrationPrePersistHook(_ http.ResponseWriter, req *http.Request, flow *registration.Flow, id *identity.Identity, ct identity.CredentialsType) error { - if !(gjson.GetBytes(e.conf, "can_interrupt").Bool() || gjson.GetBytes(e.conf, "response.parse").Bool()) { - return nil - } // fandom-start + // we use a different approach to decide which hook to trigger + //if !(gjson.GetBytes(e.conf, "can_interrupt").Bool() || gjson.GetBytes(e.conf, "response.parse").Bool()) { + // return nil + //} credentials, _ := id.GetCredentials(ct) if req.Body != nil { if err := req.ParseForm(); err != nil { @@ -274,10 +275,11 @@ func (e *WebHook) ExecutePostRegistrationPrePersistHook(_ http.ResponseWriter, r } func (e *WebHook) ExecutePostRegistrationPostPersistHook(_ http.ResponseWriter, req *http.Request, flow *registration.Flow, session *session.Session, ct identity.CredentialsType) error { - if gjson.GetBytes(e.conf, "can_interrupt").Bool() || gjson.GetBytes(e.conf, "response.parse").Bool() { - return nil - } // fandom-start + // we use a different approach to decide which hook to trigger + //if gjson.GetBytes(e.conf, "can_interrupt").Bool() || gjson.GetBytes(e.conf, "response.parse").Bool() { + // return nil + //} credentials, _ := session.Identity.GetCredentials(ct) if req.Body != nil { if err := req.ParseForm(); err != nil { @@ -367,10 +369,11 @@ func (e *WebHook) ExecuteSettingsPreHook(_ http.ResponseWriter, req *http.Reques } func (e *WebHook) ExecuteSettingsPostPersistHook(_ http.ResponseWriter, req *http.Request, flow *settings.Flow, id *identity.Identity, settingsType string) error { - if gjson.GetBytes(e.conf, "can_interrupt").Bool() || gjson.GetBytes(e.conf, "response.parse").Bool() { - return nil - } // fandom-start + // we use a different approach to decide which hook to trigger + //if gjson.GetBytes(e.conf, "can_interrupt").Bool() || gjson.GetBytes(e.conf, "response.parse").Bool() { + // return nil + //} var credentials *identity.Credentials if settingsType == "password" { credentials, _ = id.GetCredentials(identity.CredentialsTypePassword) @@ -393,11 +396,11 @@ func (e *WebHook) ExecuteSettingsPostPersistHook(_ http.ResponseWriter, req *htt } func (e *WebHook) ExecuteSettingsPrePersistHook(_ http.ResponseWriter, req *http.Request, flow *settings.Flow, id *identity.Identity, settingsType string) error { - if !(gjson.GetBytes(e.conf, "can_interrupt").Bool() || gjson.GetBytes(e.conf, "response.parse").Bool()) { - return nil - } - // fandom-start + // we use a different approach to decide which hook to trigger + //if !(gjson.GetBytes(e.conf, "can_interrupt").Bool() || gjson.GetBytes(e.conf, "response.parse").Bool()) { + // return nil + //} var credentials *identity.Credentials if settingsType == "password" { credentials, _ = id.GetCredentials(identity.CredentialsTypePassword) diff --git a/selfservice/hook/web_hook_integration_test.go b/selfservice/hook/web_hook_integration_test.go index cb5d5102a888..83ae06a70700 100644 --- a/selfservice/hook/web_hook_integration_test.go +++ b/selfservice/hook/web_hook_integration_test.go @@ -522,7 +522,9 @@ func TestWebHooks(t *testing.T) { webHookResponse: func() (int, []byte) { return http.StatusBadRequest, webHookResponse }, - expectedError: nil, + // fandom-start + expectedError: webhookError, + // fandom-end }, { uc: "Post Registration Pre Persist Hook - no block", @@ -621,7 +623,9 @@ func TestWebHooks(t *testing.T) { webHookResponse: func() (int, []byte) { return http.StatusBadRequest, webHookResponse }, - expectedError: nil, + // fandom-start + expectedError: webhookError, + // fandom-end }, } { tc := tc @@ -818,7 +822,13 @@ func TestWebHooks(t *testing.T) { postPersistErr := wh.ExecuteSettingsPostPersistHook(nil, req, f, in, "PostSettings") assert.NoError(t, postPersistErr) - assert.Equal(t, in, &identity.Identity{ID: uuid}) + // fandom-start + if tc.parse == true { + assert.Equal(t, in, &identity.Identity{ID: uuid, Traits: identity.Traits(`{"email":"some@other-example.org"}`)}) + } else { + assert.Equal(t, in, &identity.Identity{ID: uuid}) + } + // fandom-end prePersistErr := wh.ExecuteSettingsPrePersistHook(nil, req, f, in, "PostSettings") assert.NoError(t, prePersistErr)