Skip to content

Commit

Permalink
PLATFORM-9035 Fix webhook triggering conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
adpaste committed Feb 20, 2024
1 parent 5a7b3d8 commit 8a6056d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
29 changes: 16 additions & 13 deletions selfservice/hook/web_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions selfservice/hook/web_hook_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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":"[email protected]"}`)})
} else {
assert.Equal(t, in, &identity.Identity{ID: uuid})
}
// fandom-end

prePersistErr := wh.ExecuteSettingsPrePersistHook(nil, req, f, in, "PostSettings")
assert.NoError(t, prePersistErr)
Expand Down

0 comments on commit 8a6056d

Please sign in to comment.