Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: webhook config parse for settings flow #3305

Merged
merged 4 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions selfservice/hook/web_hook.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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