Skip to content

Commit

Permalink
fix: webhook config parse for settings flow (#3305)
Browse files Browse the repository at this point in the history
  • Loading branch information
jossbnd committed Jun 14, 2023
1 parent b6b80a3 commit 95ad94d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions selfservice/hook/web_hook.go
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions selfservice/hook/web_hook_integration_test.go
Expand Up @@ -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))
Expand Down

0 comments on commit 95ad94d

Please sign in to comment.