Skip to content

Commit

Permalink
feat: add session to post login webhook (#3877)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Apr 16, 2024
1 parent 6b275f3 commit 386078e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
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

0 comments on commit 386078e

Please sign in to comment.