Skip to content

Commit

Permalink
feat: add headers to web hooks (#3849)
Browse files Browse the repository at this point in the history
  • Loading branch information
sashatalalasha committed Mar 27, 2024
1 parent da90502 commit 4642de0
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 68 deletions.
26 changes: 13 additions & 13 deletions driver/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func TestViperProvider(t *testing.T) {

t.Run("hook=before", func(t *testing.T) {
expHooks := []config.SelfServiceHook{
{Name: "web_hook", Config: json.RawMessage(`{"method":"GET","url":"https://test.kratos.ory.sh/before_registration_hook"}`)},
{Name: "web_hook", Config: json.RawMessage(`{"headers":{"X-Custom-Header":"test"},"method":"GET","url":"https://test.kratos.ory.sh/before_registration_hook"}`)},
{Name: "two_step_registration", Config: json.RawMessage(`{}`)},
}

Expand All @@ -246,7 +246,7 @@ func TestViperProvider(t *testing.T) {
strategy: "password",
hooks: []config.SelfServiceHook{
{Name: "session", Config: json.RawMessage(`{}`)},
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","method":"POST","url":"https://test.kratos.ory.sh/after_registration_password_hook"}`)},
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","headers":{"X-Custom-Header":"test"},"method":"POST","url":"https://test.kratos.ory.sh/after_registration_password_hook"}`)},
// {Name: "verify", Config: json.RawMessage(`{}`)},
// {Name: "redirect", Config: json.RawMessage(`{"allow_user_defined_redirect":false,"default_redirect_url":"http://test.kratos.ory.sh:4000/"}`)},
},
Expand All @@ -255,15 +255,15 @@ func TestViperProvider(t *testing.T) {
strategy: "oidc",
hooks: []config.SelfServiceHook{
// {Name: "verify", Config: json.RawMessage(`{}`)},
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","method":"GET","url":"https://test.kratos.ory.sh/after_registration_oidc_hook"}`)},
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","headers":{"X-Custom-Header":"test"},"method":"GET","url":"https://test.kratos.ory.sh/after_registration_oidc_hook"}`)},
{Name: "session", Config: json.RawMessage(`{}`)},
// {Name: "redirect", Config: json.RawMessage(`{"allow_user_defined_redirect":false,"default_redirect_url":"http://test.kratos.ory.sh:4000/"}`)},
},
},
{
strategy: config.HookGlobal,
hooks: []config.SelfServiceHook{
{Name: "web_hook", Config: json.RawMessage(`{"auth":{"config":{"in":"header","name":"My-Key","value":"My-Key-Value"},"type":"api_key"},"body":"/path/to/template.jsonnet","method":"POST","url":"https://test.kratos.ory.sh/after_registration_global_hook"}`)},
{Name: "web_hook", Config: json.RawMessage(`{"auth":{"config":{"in":"header","name":"My-Key","value":"My-Key-Value"},"type":"api_key"},"body":"/path/to/template.jsonnet","headers":{"X-Custom-Header":"test"},"method":"POST","url":"https://test.kratos.ory.sh/after_registration_global_hook"}`)},
},
},
} {
Expand All @@ -283,7 +283,7 @@ func TestViperProvider(t *testing.T) {

t.Run("hook=before", func(t *testing.T) {
expHooks := []config.SelfServiceHook{
{Name: "web_hook", Config: json.RawMessage(`{"method":"POST","url":"https://test.kratos.ory.sh/before_login_hook"}`)},
{Name: "web_hook", Config: json.RawMessage(`{"headers":{"X-Custom-Header":"test"},"method":"POST","url":"https://test.kratos.ory.sh/before_login_hook"}`)},
}

hooks := p.SelfServiceFlowLoginBeforeHooks(ctx)
Expand All @@ -303,20 +303,20 @@ func TestViperProvider(t *testing.T) {
hooks: []config.SelfServiceHook{
{Name: "revoke_active_sessions", Config: json.RawMessage(`{}`)},
{Name: "require_verified_address", Config: json.RawMessage(`{}`)},
{Name: "web_hook", Config: json.RawMessage(`{"auth":{"config":{"password":"super-secret","user":"test-user"},"type":"basic_auth"},"body":"/path/to/template.jsonnet","method":"POST","url":"https://test.kratos.ory.sh/after_login_password_hook"}`)},
{Name: "web_hook", Config: json.RawMessage(`{"auth":{"config":{"password":"super-secret","user":"test-user"},"type":"basic_auth"},"body":"/path/to/template.jsonnet","headers":{"X-Custom-Header":"test"},"method":"POST","url":"https://test.kratos.ory.sh/after_login_password_hook"}`)},
},
},
{
strategy: "oidc",
hooks: []config.SelfServiceHook{
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","method":"GET","url":"https://test.kratos.ory.sh/after_login_oidc_hook"}`)},
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","headers":{"X-Custom-Header":"test"},"method":"GET","url":"https://test.kratos.ory.sh/after_login_oidc_hook"}`)},
{Name: "revoke_active_sessions", Config: json.RawMessage(`{}`)},
},
},
{
strategy: config.HookGlobal,
hooks: []config.SelfServiceHook{
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","method":"POST","url":"https://test.kratos.ory.sh/after_login_global_hook"}`)},
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","headers":{"X-Custom-Header":"test"},"method":"POST","url":"https://test.kratos.ory.sh/after_login_global_hook"}`)},
},
},
} {
Expand All @@ -338,19 +338,19 @@ func TestViperProvider(t *testing.T) {
{
strategy: "password",
hooks: []config.SelfServiceHook{
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","method":"POST","url":"https://test.kratos.ory.sh/after_settings_password_hook"}`)},
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","headers":{"X-Custom-Header":"test"},"method":"POST","url":"https://test.kratos.ory.sh/after_settings_password_hook"}`)},
},
},
{
strategy: "profile",
hooks: []config.SelfServiceHook{
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","method":"POST","url":"https://test.kratos.ory.sh/after_settings_profile_hook"}`)},
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","headers":{"X-Custom-Header":"test"},"method":"POST","url":"https://test.kratos.ory.sh/after_settings_profile_hook"}`)},
},
},
{
strategy: config.HookGlobal,
hooks: []config.SelfServiceHook{
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","method":"POST","url":"https://test.kratos.ory.sh/after_settings_global_hook"}`)},
{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","headers":{"X-Custom-Header":"test"},"method":"POST","url":"https://test.kratos.ory.sh/after_settings_global_hook"}`)},
},
},
} {
Expand All @@ -367,15 +367,15 @@ func TestViperProvider(t *testing.T) {
assert.Equal(t, "http://test.kratos.ory.sh/recovery", p.SelfServiceFlowRecoveryUI(ctx).String())

hooks := p.SelfServiceFlowRecoveryAfterHooks(ctx, config.HookGlobal)
assert.Equal(t, []config.SelfServiceHook{{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","method":"GET","url":"https://test.kratos.ory.sh/after_recovery_hook"}`)}}, hooks)
assert.Equal(t, []config.SelfServiceHook{{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","headers":{"X-Custom-Header":"test"},"method":"GET","url":"https://test.kratos.ory.sh/after_recovery_hook"}`)}}, hooks)
})

t.Run("method=verification", func(t *testing.T) {
assert.Equal(t, time.Minute*97, p.SelfServiceFlowVerificationRequestLifespan(ctx))
assert.Equal(t, "http://test.kratos.ory.sh/verification", p.SelfServiceFlowVerificationUI(ctx).String())

hooks := p.SelfServiceFlowVerificationAfterHooks(ctx, config.HookGlobal)
assert.Equal(t, []config.SelfServiceHook{{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","method":"GET","url":"https://test.kratos.ory.sh/after_verification_hook"}`)}}, hooks)
assert.Equal(t, []config.SelfServiceHook{{Name: "web_hook", Config: json.RawMessage(`{"body":"/path/to/template.jsonnet","headers":{"X-Custom-Header":"test"},"method":"GET","url":"https://test.kratos.ory.sh/after_verification_hook"}`)}}, hooks)
})

t.Run("group=hashers", func(t *testing.T) {
Expand Down
26 changes: 26 additions & 0 deletions driver/config/stub/.kratos.webauthn.invalid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_recovery_hook
method: GET
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet

verification:
Expand All @@ -117,6 +119,8 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_verification_hook
method: GET
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet

settings:
Expand All @@ -132,19 +136,25 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_settings_password_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
profile:
hooks:
- hook: web_hook
config:
url: https://test.kratos.ory.sh/after_settings_profile_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
hooks:
- hook: web_hook
config:
url: https://test.kratos.ory.sh/after_settings_global_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet

login:
Expand All @@ -156,6 +166,8 @@ selfservice:
config:
url: https://test.kratos.ory.sh/before_login_hook
method: POST
headers:
X-Custom-Header: test
after:
default_browser_return_url: https://self-service/login/return_to
password:
Expand All @@ -167,6 +179,8 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_login_password_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
auth:
type: basic_auth
Expand All @@ -179,13 +193,17 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_login_oidc_hook
method: GET
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
- hook: revoke_active_sessions
hooks:
- hook: web_hook
config:
url: https://test.kratos.ory.sh/after_login_global_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet

registration:
Expand All @@ -198,6 +216,8 @@ selfservice:
config:
url: https://test.kratos.ory.sh/before_registration_hook
method: GET
headers:
X-Custom-Header: test
after:
default_browser_return_url: https://self-service/registration/return_to
password:
Expand All @@ -207,12 +227,16 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_registration_password_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
hooks:
- hook: web_hook
config:
url: https://test.kratos.ory.sh/after_registration_global_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
auth:
type: api_key
Expand All @@ -227,5 +251,7 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_registration_oidc_hook
method: GET
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
- hook: session
26 changes: 26 additions & 0 deletions driver/config/stub/.kratos.webauthn.origin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_recovery_hook
method: GET
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet

verification:
Expand All @@ -113,6 +115,8 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_verification_hook
method: GET
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet

settings:
Expand All @@ -128,19 +132,25 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_settings_password_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
profile:
hooks:
- hook: web_hook
config:
url: https://test.kratos.ory.sh/after_settings_profile_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
hooks:
- hook: web_hook
config:
url: https://test.kratos.ory.sh/after_settings_global_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet

login:
Expand All @@ -152,6 +162,8 @@ selfservice:
config:
url: https://test.kratos.ory.sh/before_login_hook
method: POST
headers:
X-Custom-Header: test
after:
default_browser_return_url: https://self-service/login/return_to
password:
Expand All @@ -163,6 +175,8 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_login_password_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
auth:
type: basic_auth
Expand All @@ -175,13 +189,17 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_login_oidc_hook
method: GET
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
- hook: revoke_active_sessions
hooks:
- hook: web_hook
config:
url: https://test.kratos.ory.sh/after_login_global_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet

registration:
Expand All @@ -194,6 +212,8 @@ selfservice:
config:
url: https://test.kratos.ory.sh/before_registration_hook
method: GET
headers:
X-Custom-Header: test
after:
default_browser_return_url: https://self-service/registration/return_to
password:
Expand All @@ -203,12 +223,16 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_registration_password_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
hooks:
- hook: web_hook
config:
url: https://test.kratos.ory.sh/after_registration_global_hook
method: POST
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
auth:
type: api_key
Expand All @@ -223,5 +247,7 @@ selfservice:
config:
url: https://test.kratos.ory.sh/after_registration_oidc_hook
method: GET
headers:
X-Custom-Header: test
body: /path/to/template.jsonnet
- hook: session
Loading

0 comments on commit 4642de0

Please sign in to comment.