-
-
Notifications
You must be signed in to change notification settings - Fork 928
/
hook_test.go
298 lines (246 loc) · 13.2 KB
/
hook_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package login_test
import (
"context"
"net/http"
"net/url"
"testing"
"time"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/require"
"github.com/ory/kratos/hydra"
"github.com/ory/kratos/session"
"github.com/gobuffalo/httptest"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/internal"
"github.com/ory/kratos/internal/testhelpers"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/selfservice/flow/login"
"github.com/ory/kratos/x"
)
func TestLoginExecutor(t *testing.T) {
t.Parallel()
ctx := context.Background()
for _, strategy := range []identity.CredentialsType{
identity.CredentialsTypePassword,
identity.CredentialsTypeOIDC,
identity.CredentialsTypeTOTP,
identity.CredentialsTypeWebAuthn,
identity.CredentialsTypeLookup,
} {
strategy := strategy
t.Run("strategy="+strategy.String(), func(t *testing.T) {
t.Parallel()
conf, reg := internal.NewFastRegistryWithMocks(t)
reg.WithHydra(hydra.NewFake())
testhelpers.SetDefaultIdentitySchema(conf, "file://./stub/login.schema.json")
conf.MustSet(ctx, config.ViperKeySelfServiceBrowserDefaultReturnTo, "https://www.ory.sh/")
newServer := func(t *testing.T, ft flow.Type, useIdentity *identity.Identity, flowCallback ...func(*login.Flow)) *httptest.Server {
router := httprouter.New()
router.GET("/login/pre", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
loginFlow, err := login.NewFlow(conf, time.Minute, "", r, ft)
require.NoError(t, err)
if testhelpers.SelfServiceHookLoginErrorHandler(t, w, r, reg.LoginHookExecutor().PreLoginHook(w, r, loginFlow)) {
_, _ = w.Write([]byte("ok"))
}
})
router.GET("/login/post", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
loginFlow, err := login.NewFlow(conf, time.Minute, "", r, ft)
require.NoError(t, err)
loginFlow.Active = strategy
loginFlow.RequestURL = x.RequestURL(r).String()
for _, cb := range flowCallback {
cb(loginFlow)
}
sess := session.NewInactiveSession()
sess.CompletedLoginFor(identity.CredentialsTypePassword, identity.AuthenticatorAssuranceLevel1)
if useIdentity == nil {
useIdentity = testhelpers.SelfServiceHookCreateFakeIdentity(t, reg)
}
testhelpers.SelfServiceHookLoginErrorHandler(t, w, r,
reg.LoginHookExecutor().PostLoginHook(w, r, strategy.ToUiNodeGroup(), loginFlow, useIdentity, sess, ""))
})
ts := httptest.NewServer(router)
t.Cleanup(ts.Close)
conf.MustSet(ctx, config.ViperKeyPublicBaseURL, ts.URL)
return ts
}
makeRequestPost := testhelpers.SelfServiceMakeLoginPostHookRequest
viperSetPost := testhelpers.SelfServiceHookLoginViperSetPost
t.Run("method=PostLoginHook", func(t *testing.T) {
t.Run("case=pass without hooks", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser, nil), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/", res.Request.URL.String())
})
t.Run("case=pass if hooks pass", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
viperSetPost(t, conf, strategy.String(), []config.SelfServiceHook{{Name: "err", Config: []byte(`{}`)}})
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser, nil), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/", res.Request.URL.String())
})
t.Run("case=fail if hooks fail", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
viperSetPost(t, conf, strategy.String(), []config.SelfServiceHook{{Name: "err", Config: []byte(`{"ExecuteLoginPostHook": "abort"}`)}})
res, body := makeRequestPost(t, newServer(t, flow.TypeBrowser, nil), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.Equal(t, "", body)
})
t.Run("case=use return_to value", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
conf.MustSet(ctx, config.ViperKeyURLsAllowedReturnToDomains, []string{"https://www.ory.sh/"})
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser, nil), false, url.Values{"return_to": {"https://www.ory.sh/kratos/"}})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/kratos/", res.Request.URL.String())
})
t.Run("case=use nested config value", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
conf.MustSet(ctx, config.ViperKeySelfServiceLoginAfter+"."+config.DefaultBrowserReturnURL, "https://www.ory.sh/kratos")
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser, nil), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/kratos/", res.Request.URL.String())
})
t.Run("case=use nested config value", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
testhelpers.SelfServiceHookLoginSetDefaultRedirectTo(t, conf, "https://www.ory.sh/not-kratos")
testhelpers.SelfServiceHookLoginSetDefaultRedirectToStrategy(t, conf, strategy.String(), "https://www.ory.sh/kratos")
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser, nil), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/kratos/", res.Request.URL.String())
})
t.Run("case=pass if hooks pass", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
viperSetPost(t, conf, strategy.String(), []config.SelfServiceHook{{Name: "err", Config: []byte(`{}`)}})
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser, nil), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/", res.Request.URL.String())
})
t.Run("case=send a json response for API clients", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
res, body := makeRequestPost(t, newServer(t, flow.TypeAPI, nil), true, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.NotEmpty(t, gjson.Get(body, "session.identity.id").String())
})
t.Run("suite=handle login challenge with browser and application/json", func(t *testing.T) {
t.Run("case=includes the return_to address for a valid challenge", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
withOAuthChallenge := func(f *login.Flow) {
f.OAuth2LoginChallenge = uuid.NullUUID{Valid: true, UUID: uuid.Must(uuid.FromString(hydra.FakeValidLoginChallenge))}
}
res, body := makeRequestPost(t, newServer(t, flow.TypeBrowser, nil, withOAuthChallenge), true, url.Values{})
assert.EqualValues(t, http.StatusUnprocessableEntity, res.StatusCode)
assert.Equal(t, hydra.FakePostLoginURL, gjson.Get(body, "redirect_browser_to").String(), "%s", body)
})
t.Run("case=returns an error for an invalid challenge", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
withOAuthChallenge := func(f *login.Flow) {
f.OAuth2LoginChallenge = uuid.NullUUID{Valid: true, UUID: uuid.Must(uuid.FromString(hydra.FakeInvalidLoginChallenge))}
}
res, body := makeRequestPost(t, newServer(t, flow.TypeBrowser, nil, withOAuthChallenge), true, url.Values{})
assert.EqualValues(t, http.StatusInternalServerError, res.StatusCode)
assert.Equal(t, hydra.ErrFakeAcceptLoginRequestFailed.Error(), body, "%s", body)
})
})
t.Run("case=pass without hooks for browser flow with application/json", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
res, body := makeRequestPost(t, newServer(t, flow.TypeBrowser, nil), true, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.NotEmpty(t, gjson.Get(body, "session.identity.id").String())
assert.Empty(t, gjson.Get(body, "session.token").String())
assert.Empty(t, gjson.Get(body, "session_token").String())
})
t.Run("case=work normally if AAL is satisfied", func(t *testing.T) {
conf.MustSet(ctx, config.ViperKeySessionWhoAmIAAL, "aal1")
_ = testhelpers.NewLoginUIFlowEchoServer(t, reg)
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
useIdentity := &identity.Identity{Credentials: map[identity.CredentialsType]identity.Credentials{
identity.CredentialsTypePassword: {Type: identity.CredentialsTypePassword},
identity.CredentialsTypeTOTP: {Type: identity.CredentialsTypeTOTP},
}}
require.NoError(t, reg.Persister().CreateIdentity(context.Background(), useIdentity))
t.Run("browser client", func(t *testing.T) {
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser, useIdentity), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/", res.Request.URL.String())
})
t.Run("api client returns the token but not the identity", func(t *testing.T) {
res, body := makeRequestPost(t, newServer(t, flow.TypeAPI, useIdentity), true, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.NotEmpty(t, gjson.Get(body, "session.identity").String())
assert.NotEmpty(t, gjson.Get(body, "session_token").String())
})
t.Run("browser JSON client returns the token but not the identity", func(t *testing.T) {
res, body := makeRequestPost(t, newServer(t, flow.TypeBrowser, useIdentity), true, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.NotEmpty(t, gjson.Get(body, "session.id").String())
assert.NotEmpty(t, gjson.Get(body, "session.identity").String())
assert.Empty(t, gjson.Get(body, "session_token").String())
})
})
t.Run("case=redirect to login if AAL is too low", func(t *testing.T) {
conf.MustSet(ctx, config.ViperKeySessionWhoAmIAAL, "highest_available")
_ = testhelpers.NewLoginUIFlowEchoServer(t, reg)
t.Cleanup(func() {
conf.MustSet(ctx, config.ViperKeySessionWhoAmIAAL, "aal1")
})
t.Cleanup(testhelpers.SelfServiceHookConfigReset(t, conf))
useIdentity := &identity.Identity{Credentials: map[identity.CredentialsType]identity.Credentials{
identity.CredentialsTypePassword: {Type: identity.CredentialsTypePassword, Config: []byte(`{"hashed_password": "$argon2id$v=19$m=32,t=2,p=4$cm94YnRVOW5jZzFzcVE4bQ$MNzk5BtR2vUhrp6qQEjRNw"}`), Identifiers: []string{testhelpers.RandomEmail()}},
identity.CredentialsTypeWebAuthn: {Type: identity.CredentialsTypeWebAuthn, Config: []byte(`{"credentials":[{"is_passwordless":false}]}`), Identifiers: []string{testhelpers.RandomEmail()}},
}}
require.NoError(t, reg.Persister().CreateIdentity(context.Background(), useIdentity))
t.Run("browser client", func(t *testing.T) {
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser, useIdentity), false, url.Values{})
assert.EqualValues(t, http.StatusNotFound, res.StatusCode)
assert.Contains(t, res.Request.URL.String(), "/self-service/login/browser?aal=aal2")
})
t.Run("api client returns the token but not the identity", func(t *testing.T) {
res, body := makeRequestPost(t, newServer(t, flow.TypeAPI, useIdentity), true, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.Empty(t, gjson.Get(body, "session.identity").String())
assert.NotEmpty(t, gjson.Get(body, "session_token").String())
})
t.Run("browser JSON client returns the token but not the identity", func(t *testing.T) {
res, body := makeRequestPost(t, newServer(t, flow.TypeBrowser, useIdentity), true, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.NotEmpty(t, gjson.Get(body, "session.id").String())
assert.Empty(t, gjson.Get(body, "session.identity").String())
assert.Empty(t, gjson.Get(body, "session_token").String())
})
})
})
t.Run("type=api", func(t *testing.T) {
t.Run("method=PreLoginHook", testhelpers.TestSelfServicePreHook(
config.ViperKeySelfServiceLoginBeforeHooks,
testhelpers.SelfServiceMakeLoginPreHookRequest,
func(t *testing.T) *httptest.Server {
return newServer(t, flow.TypeAPI, nil)
},
conf,
))
})
t.Run("type=browser", func(t *testing.T) {
t.Run("method=PreLoginHook", testhelpers.TestSelfServicePreHook(
config.ViperKeySelfServiceLoginBeforeHooks,
testhelpers.SelfServiceMakeLoginPreHookRequest,
func(t *testing.T) *httptest.Server {
return newServer(t, flow.TypeBrowser, nil)
},
conf,
))
})
t.Run("requiresAAL2 should return true if there's an error", func(t *testing.T) {
requiresAAL2, err := login.RequiresAAL2ForTest(*reg.LoginHookExecutor(), &http.Request{}, &session.Session{})
require.NotNil(t, err)
require.True(t, requiresAAL2)
})
})
}
}