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

feat: add session to post login webhook #3877

Merged
merged 1 commit into from
Apr 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions selfservice/hook/stub/test_body.jsonnet
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function(ctx) std.prune({
flow_id: ctx.flow.id,
identity_id: if std.objectHas(ctx, "identity") then ctx.identity.id,
session_id: if std.objectHas(ctx, "session") then ctx.session.id,
headers: ctx.request_headers,
url: ctx.request_url,
method: ctx.request_method,
Expand Down
2 changes: 2 additions & 0 deletions selfservice/hook/web_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type (
RequestURL string `json:"request_url"`
RequestCookies map[string]string `json:"request_cookies"`
Identity *identity.Identity `json:"identity,omitempty"`
Session *session.Session `json:"session,omitempty"`
}

WebHook struct {
Expand Down Expand Up @@ -140,6 +141,7 @@ func (e *WebHook) ExecuteLoginPostHook(_ http.ResponseWriter, req *http.Request,
RequestURL: x.RequestURL(req).String(),
RequestCookies: cookies(req),
Identity: session.Identity,
Session: session,
})
})
}
Expand Down
20 changes: 19 additions & 1 deletion selfservice/hook/web_hook_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ func TestWebHooks(t *testing.T) {
}`, f.GetID(), s.Identity.ID, string(h), req.Method, "http://www.ory.sh/some_end_point", string(tp))
}

bodyWithFlowAndIdentityAndSessionAndTransientPayload := func(req *http.Request, f flow.Flow, s *session.Session, tp json.RawMessage) string {
h, _ := json.Marshal(req.Header)
return fmt.Sprintf(`{
"flow_id": "%s",
"identity_id": "%s",
"session_id": "%s",
"headers": %s,
"method": "%s",
"url": "%s",
"cookies": {
"Some-Cookie-1": "Some-Cookie-Value",
"Some-Cookie-2": "Some-other-Cookie-Value",
"Some-Cookie-3": "Third-Cookie-Value"
},
"transient_payload": %s
}`, f.GetID(), s.Identity.ID, s.ID, string(h), req.Method, "http://www.ory.sh/some_end_point", string(tp))
}

for _, tc := range []struct {
uc string
callWebHook func(wh *hook.WebHook, req *http.Request, f flow.Flow, s *session.Session) error
Expand All @@ -171,7 +189,7 @@ func TestWebHooks(t *testing.T) {
return wh.ExecuteLoginPostHook(nil, req, node.PasswordGroup, f.(*login.Flow), s)
},
expectedBody: func(req *http.Request, f flow.Flow, s *session.Session) string {
return bodyWithFlowAndIdentityAndTransientPayload(req, f, s, transientPayload)
return bodyWithFlowAndIdentityAndSessionAndTransientPayload(req, f, s, transientPayload)
},
},
{
Expand Down
Loading