From 95ad94d08efdbb369caecaa64cd0a30058c34ed3 Mon Sep 17 00:00:00 2001 From: joss <62952702+jossbnd@users.noreply.github.com> Date: Wed, 14 Jun 2023 14:45:26 +0200 Subject: [PATCH] fix: webhook config parse for settings flow (#3305) --- selfservice/hook/web_hook.go | 4 +- selfservice/hook/web_hook_integration_test.go | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/selfservice/hook/web_hook.go b/selfservice/hook/web_hook.go index c35f4d3eb93..97f77ea8be2 100644 --- a/selfservice/hook/web_hook.go +++ b/selfservice/hook/web_hook.go @@ -244,7 +244,7 @@ func (e *WebHook) ExecuteSettingsPreHook(_ http.ResponseWriter, req *http.Reques } func (e *WebHook) ExecuteSettingsPostPersistHook(_ http.ResponseWriter, req *http.Request, flow *settings.Flow, id *identity.Identity) error { - if gjson.GetBytes(e.conf, "can_interrupt").Bool() { + if gjson.GetBytes(e.conf, "can_interrupt").Bool() || gjson.GetBytes(e.conf, "response.parse").Bool() { return nil } return otelx.WithSpan(req.Context(), "selfservice.hook.WebHook.ExecuteSettingsPostPersistHook", func(ctx context.Context) error { @@ -260,7 +260,7 @@ func (e *WebHook) ExecuteSettingsPostPersistHook(_ http.ResponseWriter, req *htt } func (e *WebHook) ExecuteSettingsPrePersistHook(_ http.ResponseWriter, req *http.Request, flow *settings.Flow, id *identity.Identity) error { - if !gjson.GetBytes(e.conf, "can_interrupt").Bool() { + if !(gjson.GetBytes(e.conf, "can_interrupt").Bool() || gjson.GetBytes(e.conf, "response.parse").Bool()) { return nil } return otelx.WithSpan(req.Context(), "selfservice.hook.WebHook.ExecuteSettingsPrePersistHook", func(ctx context.Context) error { diff --git a/selfservice/hook/web_hook_integration_test.go b/selfservice/hook/web_hook_integration_test.go index e3f752ee51c..88632463d59 100644 --- a/selfservice/hook/web_hook_integration_test.go +++ b/selfservice/hook/web_hook_integration_test.go @@ -765,6 +765,45 @@ func TestWebHooks(t *testing.T) { assert.Error(t, err) }) + for _, tc := range []struct { + uc string + parse bool + }{ + {uc: "Post Settings Hook - parse true", parse: true}, + {uc: "Post Settings Hook - parse false", parse: false}, + } { + tc := tc + t.Run("uc="+tc.uc, func(t *testing.T) { + t.Parallel() + ts := newServer(webHookHttpCodeWithBodyEndPoint(t, 200, []byte(`{"identity":{"traits":{"email":"some@other-example.org"}}}`))) + req := &http.Request{ + Header: map[string][]string{"Some-Header": {"Some-Value"}}, + Host: "www.ory.sh", + TLS: new(tls.ConnectionState), + URL: &url.URL{Path: "/some_end_point"}, + + Method: http.MethodPost, + } + f := &settings.Flow{ID: x.NewUUID()} + conf := json.RawMessage(fmt.Sprintf(`{"url": "%s", "method": "POST", "body": "%s", "response": {"parse":%t}}`, ts.URL+path, "file://./stub/test_body.jsonnet", tc.parse)) + wh := hook.NewWebHook(&whDeps, conf) + uuid := x.NewUUID() + in := &identity.Identity{ID: uuid} + + postPersistErr := wh.ExecuteSettingsPostPersistHook(nil, req, f, in) + assert.NoError(t, postPersistErr) + assert.Equal(t, in, &identity.Identity{ID: uuid}) + + prePersistErr := wh.ExecuteSettingsPrePersistHook(nil, req, f, in) + assert.NoError(t, prePersistErr) + 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}) + } + }) + } + t.Run("must error when template is erroneous", func(t *testing.T) { t.Parallel() ts := newServer(webHookHttpCodeEndPoint(200))