-
-
Notifications
You must be signed in to change notification settings - Fork 928
/
handler_test.go
755 lines (619 loc) · 34 KB
/
handler_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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package login_test
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"testing"
"time"
"github.com/pkg/errors"
"github.com/ory/x/urlx"
"github.com/ory/x/sqlxx"
"github.com/ory/kratos/hydra"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/ui/container"
"github.com/ory/kratos/text"
"github.com/gobuffalo/httptest"
"github.com/gofrs/uuid"
"github.com/ory/kratos/corpx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"github.com/ory/x/assertx"
"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/login"
"github.com/ory/kratos/x"
)
func init() {
corpx.RegisterFakes()
}
func TestFlowLifecycle(t *testing.T) {
ctx := context.Background()
conf, reg := internal.NewFastRegistryWithMocks(t)
reg.WithHydra(hydra.NewFakeHydra())
router := x.NewRouterPublic()
ts, _ := testhelpers.NewKratosServerWithRouters(t, reg, router, x.NewRouterAdmin())
loginTS := testhelpers.NewLoginUIFlowEchoServer(t, reg)
errorTS := testhelpers.NewErrorTestServer(t, reg)
conf.MustSet(ctx, config.ViperKeySelfServiceBrowserDefaultReturnTo, "https://www.ory.sh")
testhelpers.SetDefaultIdentitySchema(conf, "file://./stub/password.schema.json")
assertion := func(body []byte, isForced, isApi bool) {
r := gjson.GetBytes(body, "refresh")
assert.True(t, r.Exists(), "%s", body)
assert.Equal(t, isForced, r.Bool(), "%s", body)
if isApi {
assert.Equal(t, "api", gjson.GetBytes(body, "type").String())
} else {
assert.Equal(t, "browser", gjson.GetBytes(body, "type").String())
}
}
initAuthenticatedFlow := func(t *testing.T, extQuery url.Values, isAPI bool) (*http.Response, []byte) {
route := login.RouteInitBrowserFlow
if isAPI {
route = login.RouteInitAPIFlow
}
req := x.NewTestHTTPRequest(t, "GET", ts.URL+route, nil)
req.URL.RawQuery = extQuery.Encode()
body, res := testhelpers.MockMakeAuthenticatedRequest(t, reg, conf, router.Router, req)
if isAPI {
assert.Len(t, res.Header.Get("Set-Cookie"), 0)
}
return res, body
}
initUnauthenticatedFlow := func(t *testing.T, extQuery url.Values, isAPI bool) (*http.Response, []byte) {
route := login.RouteInitBrowserFlow
if isAPI {
route = login.RouteInitAPIFlow
}
client := ts.Client()
req := x.NewTestHTTPRequest(t, "GET", ts.URL+route, nil)
req.URL.RawQuery = extQuery.Encode()
res, err := client.Do(req)
require.NoError(t, errors.WithStack(err))
body, err := io.ReadAll(res.Body)
require.NoError(t, errors.WithStack(err))
require.NoError(t, res.Body.Close())
return res, body
}
initFlowWithAccept := func(t *testing.T, query url.Values, isAPI bool, accept string) (*http.Response, []byte) {
route := login.RouteInitBrowserFlow
if isAPI {
route = login.RouteInitAPIFlow
}
c := ts.Client()
req, err := http.NewRequest("GET", ts.URL+route+"?"+query.Encode(), nil)
require.NoError(t, err)
if accept != "" {
req.Header.Set("Accept", accept)
}
res, err := c.Do(req)
require.NoError(t, err)
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
return res, body
}
initFlow := func(t *testing.T, query url.Values, isAPI bool) (*http.Response, []byte) {
return initFlowWithAccept(t, query, isAPI, "")
}
initSPAFlow := func(t *testing.T, query url.Values) (*http.Response, []byte) {
return initFlowWithAccept(t, query, false, "application/json")
}
id1mail, id2mail := x.NewUUID().String(), x.NewUUID().String()
identity1 := &identity.Identity{
Credentials: map[identity.CredentialsType]identity.Credentials{
identity.CredentialsTypePassword: {
Type: identity.CredentialsTypePassword,
Identifiers: []string{id1mail},
Config: sqlxx.JSONRawMessage(`{"hashed_password":"$2a$08$.cOYmAd.vCpDOoiVJrO5B.hjTLKQQ6cAK40u8uB.FnZDyPvVvQ9Q."}`), // foobar
}},
State: identity.StateActive,
Traits: identity.Traits(`{"username":"` + id1mail + `"}`),
}
identity2 := &identity.Identity{
Credentials: map[identity.CredentialsType]identity.Credentials{
identity.CredentialsTypePassword: {
Type: identity.CredentialsTypePassword,
Identifiers: []string{id2mail},
Config: sqlxx.JSONRawMessage(`{"hashed_password":"$2a$08$.cOYmAd.vCpDOoiVJrO5B.hjTLKQQ6cAK40u8uB.FnZDyPvVvQ9Q."}`), // foobar
}},
State: identity.StateActive,
Traits: identity.Traits(`{"username":"` + id2mail + `"}`),
}
require.NoError(t, reg.PrivilegedIdentityPool().CreateIdentity(context.Background(), identity1))
require.NoError(t, reg.PrivilegedIdentityPool().CreateIdentity(context.Background(), identity2))
t.Run("lifecycle=submit", func(t *testing.T) {
t.Run("interaction=unauthenticated", func(t *testing.T) {
run := func(t *testing.T, tt flow.Type, aal string, values url.Values) (string, *http.Response) {
f := login.Flow{Type: tt, ExpiresAt: time.Now().Add(time.Minute), IssuedAt: time.Now(),
UI: container.New(""), Refresh: false, RequestedAAL: identity.AuthenticatorAssuranceLevel(aal)}
require.NoError(t, reg.LoginFlowPersister().CreateLoginFlow(context.Background(), &f))
res, err := http.PostForm(ts.URL+login.RouteSubmitFlow+"?flow="+f.ID.String(), values)
require.NoError(t, err)
body := x.MustReadAll(res.Body)
require.NoError(t, res.Body.Close())
return string(body), res
}
t.Run("case=ensure aal can only be upgraded with a session on submit", func(t *testing.T) {
t.Run("type=api", func(t *testing.T) {
body, res := run(t, flow.TypeAPI, "aal2", url.Values{"method": {"password"}})
assert.Contains(t, res.Request.URL.String(), login.RouteSubmitFlow)
assertx.EqualAsJSON(t, "You can not requested a higher AAL (AAL2/AAL3) without an active session.", gjson.Get(body, "error.reason").String())
})
t.Run("type=browser", func(t *testing.T) {
body, res := run(t, flow.TypeBrowser, "aal2", url.Values{"method": {"password"}})
assert.Contains(t, res.Request.URL.String(), errorTS.URL)
assertx.EqualAsJSON(t, "You can not requested a higher AAL (AAL2/AAL3) without an active session.", gjson.Get(body, "reason").String())
})
})
t.Run("case=end up with method missing when aal is ok", func(t *testing.T) {
t.Run("type=api", func(t *testing.T) {
body, res := run(t, flow.TypeAPI, "aal1", url.Values{"method": {"not-exist"}})
assert.Contains(t, res.Request.URL.String(), login.RouteSubmitFlow)
assertx.EqualAsJSON(t, text.NewErrorValidationLoginNoStrategyFound().Text, gjson.Get(body, "ui.messages.0.text").String(), body)
})
t.Run("type=browser", func(t *testing.T) {
body, res := run(t, flow.TypeBrowser, "aal1", url.Values{"method": {"not-exist"}})
assert.Contains(t, res.Request.URL.String(), loginTS.URL)
assertx.EqualAsJSON(t, text.NewErrorValidationLoginNoStrategyFound().Text, gjson.Get(body, "ui.messages.0.text").String(), body)
})
})
t.Run("case=reset the session when refresh is true but identity is different", func(t *testing.T) {
testhelpers.NewRedirSessionEchoTS(t, reg)
t.Cleanup(func() {
conf.MustSet(ctx, config.ViperKeySelfServiceBrowserDefaultReturnTo, "https://www.ory.sh")
})
run := func(t *testing.T, tt flow.Type) (string, string) {
f := login.Flow{Type: tt, ExpiresAt: time.Now().Add(time.Minute), IssuedAt: time.Now(), UI: container.New(""), Refresh: false, RequestedAAL: "aal1"}
require.NoError(t, reg.LoginFlowPersister().CreateLoginFlow(context.Background(), &f))
hc := testhelpers.NewClientWithCookies(t)
res, err := hc.PostForm(ts.URL+login.RouteSubmitFlow+"?flow="+f.ID.String(), url.Values{"method": {"password"}, "password_identifier": {id1mail}, "password": {"foobar"}, "csrf_token": {x.FakeCSRFToken}})
require.NoError(t, err)
firstSession := x.MustReadAll(res.Body)
require.NoError(t, res.Body.Close())
f = login.Flow{Type: tt, ExpiresAt: time.Now().Add(time.Minute), IssuedAt: time.Now(), UI: container.New(""), Refresh: true, RequestedAAL: "aal1"}
require.NoError(t, reg.LoginFlowPersister().CreateLoginFlow(context.Background(), &f))
vv := testhelpers.EncodeFormAsJSON(t, tt == flow.TypeAPI, url.Values{"method": {"password"}, "password_identifier": {id2mail}, "password": {"foobar"}, "csrf_token": {x.FakeCSRFToken}})
req, err := http.NewRequest("POST", ts.URL+login.RouteSubmitFlow+"?flow="+f.ID.String(), strings.NewReader(vv))
require.NoError(t, err)
if tt == flow.TypeAPI {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+gjson.GetBytes(firstSession, "session_token").String())
} else {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
res, err = hc.Do(req)
require.NoError(t, err)
secondSession := x.MustReadAll(res.Body)
require.NoError(t, res.Body.Close())
return string(firstSession), string(secondSession)
}
t.Run("type=browser", func(t *testing.T) {
a, b := run(t, flow.TypeBrowser)
assert.Equal(t, id1mail, gjson.Get(a, "identity.traits.username").String())
assert.Equal(t, id2mail, gjson.Get(b, "identity.traits.username").String())
assert.NotEmpty(t, gjson.Get(b, "id").String())
assert.NotEqual(t, gjson.Get(b, "id").String(), gjson.Get(a, "id").String())
})
t.Run("type=api", func(t *testing.T) {
a, b := run(t, flow.TypeAPI)
assert.Equal(t, id1mail, gjson.Get(a, "session.identity.traits.username").String())
assert.Equal(t, id2mail, gjson.Get(b, "session.identity.traits.username").String())
assert.NotEmpty(t, gjson.Get(a, "session_token").String())
assert.NotEqual(t, gjson.Get(a, "session_token").String(), gjson.Get(b, "session_token").String())
assert.NotEmpty(t, gjson.Get(b, "session.id").String())
assert.NotEqual(t, gjson.Get(b, "session.id").String(), gjson.Get(a, "id").String())
})
})
t.Run("case=changed kratos session identifiers when refresh is true", func(t *testing.T) {
t.Cleanup(func() {
conf.MustSet(ctx, config.ViperKeySelfServiceBrowserDefaultReturnTo, "https://www.ory.sh")
})
t.Run("type=browser", func(t *testing.T) {
// Setup flow
f := login.Flow{Type: flow.TypeBrowser, ExpiresAt: time.Now().Add(time.Minute), IssuedAt: time.Now(), UI: container.New(""), Refresh: false, RequestedAAL: "aal1"}
require.NoError(t, reg.LoginFlowPersister().CreateLoginFlow(context.Background(), &f))
// Submit Login
hc := testhelpers.NewClientWithCookies(t)
res, err := hc.PostForm(ts.URL+login.RouteSubmitFlow+"?flow="+f.ID.String(), url.Values{"method": {"password"}, "password_identifier": {id1mail}, "password": {"foobar"}, "csrf_token": {x.FakeCSRFToken}})
require.NoError(t, err)
// Check response and session cookie presence
assert.Equal(t, http.StatusOK, res.StatusCode)
require.Len(t, hc.Jar.Cookies(urlx.ParseOrPanic(ts.URL+login.RouteGetFlow)), 1)
require.Contains(t, fmt.Sprintf("%v", hc.Jar.Cookies(urlx.ParseOrPanic(ts.URL))), "ory_kratos_session")
cookies1 := hc.Jar.Cookies(urlx.ParseOrPanic(ts.URL + login.RouteGetFlow))
req, err := http.NewRequest("GET", ts.URL+"/sessions/whoami", nil)
require.NoError(t, err)
res, err = hc.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
firstSession := x.MustReadAll(res.Body)
require.NoError(t, res.Body.Close())
// Refresh
f = login.Flow{Type: flow.TypeBrowser, ExpiresAt: time.Now().Add(time.Minute), IssuedAt: time.Now(), UI: container.New(""), Refresh: true, RequestedAAL: "aal1"}
require.NoError(t, reg.LoginFlowPersister().CreateLoginFlow(context.Background(), &f))
vv := testhelpers.EncodeFormAsJSON(t, false, url.Values{"method": {"password"}, "password_identifier": {id1mail}, "password": {"foobar"}, "csrf_token": {x.FakeCSRFToken}})
req, err = http.NewRequest("POST", ts.URL+login.RouteSubmitFlow+"?flow="+f.ID.String(), strings.NewReader(vv))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Submit Login
res, err = hc.Do(req)
require.NoError(t, err)
// Check response and session cookie presence
assert.Equal(t, http.StatusOK, res.StatusCode)
require.Len(t, hc.Jar.Cookies(urlx.ParseOrPanic(ts.URL+login.RouteGetFlow)), 1)
require.Contains(t, fmt.Sprintf("%v", hc.Jar.Cookies(urlx.ParseOrPanic(ts.URL))), "ory_kratos_session")
cookies2 := hc.Jar.Cookies(urlx.ParseOrPanic(ts.URL + login.RouteGetFlow))
req, err = http.NewRequest("GET", ts.URL+"/sessions/whoami", nil)
require.NoError(t, err)
res, err = hc.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
secondSession := x.MustReadAll(res.Body)
require.NoError(t, res.Body.Close())
// Sessions should still be resolvable despite different kratos session identifier due to nonce
assert.NotEqual(t, cookies1[0].String(), cookies2[0].String())
assert.Equal(t, id1mail, gjson.Get(string(firstSession), "identity.traits.username").String())
assert.Equal(t, id1mail, gjson.Get(string(secondSession), "identity.traits.username").String())
assert.Equal(t, gjson.Get(string(secondSession), "id").String(), gjson.Get(string(firstSession), "id").String())
})
})
})
t.Run("case=ensure aal is checked for upgradeability on session", func(t *testing.T) {
run := func(t *testing.T, tt flow.Type, values url.Values) (string, *http.Response) {
f := login.Flow{Type: tt, ExpiresAt: time.Now().Add(time.Minute), IssuedAt: time.Now(),
UI: container.New(""), Refresh: false, RequestedAAL: "aal1"}
require.NoError(t, reg.LoginFlowPersister().CreateLoginFlow(context.Background(), &f))
req, err := http.NewRequest("GET", ts.URL+login.RouteSubmitFlow+"?flow="+f.ID.String(), strings.NewReader(values.Encode()))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
body, res := testhelpers.MockMakeAuthenticatedRequest(t, reg, conf, router.Router, req)
return string(body), res
}
t.Run("type=api", func(t *testing.T) {
body, res := run(t, flow.TypeAPI, url.Values{"method": {"password"}})
assert.Contains(t, res.Request.URL.String(), login.RouteSubmitFlow)
assertx.EqualAsJSON(t, login.ErrAlreadyLoggedIn.Reason(), gjson.Get(body, "ui.messages.0.text").String(), body)
})
t.Run("type=browser", func(t *testing.T) {
_, res := run(t, flow.TypeBrowser, url.Values{"method": {"password"}})
assert.Contains(t, res.Request.URL.String(), "https://www.ory.sh")
})
})
t.Run("case=should return an error because the request is expired", func(t *testing.T) {
conf.MustSet(ctx, config.ViperKeySelfServiceLoginRequestLifespan, "50ms")
t.Cleanup(func() {
conf.MustSet(ctx, config.ViperKeySelfServiceLoginRequestLifespan, "10m")
})
expired := time.Now().Add(-time.Minute)
run := func(t *testing.T, tt flow.Type, aal string, values string, isSPA bool) (string, *http.Response) {
f := login.Flow{Type: tt, ExpiresAt: expired, IssuedAt: time.Now(),
UI: container.New(""), Refresh: false, RequestedAAL: identity.AuthenticatorAssuranceLevel(aal)}
require.NoError(t, reg.LoginFlowPersister().CreateLoginFlow(context.Background(), &f))
req, err := http.NewRequest("POST", ts.URL+login.RouteSubmitFlow+"?flow="+f.ID.String(), strings.NewReader(values))
require.NoError(t, err)
if isSPA {
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
} else {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
body := x.MustReadAll(res.Body)
require.NoError(t, res.Body.Close())
return string(body), res
}
t.Run("type=api", func(t *testing.T) {
actual, res := run(t, flow.TypeAPI, "aal1", `{"method":"password"}`, false)
assert.Contains(t, res.Request.URL.String(), login.RouteSubmitFlow)
assert.NotEqual(t, "00000000-0000-0000-0000-000000000000", gjson.Get(actual, "use_flow_id").String())
assertx.EqualAsJSONExcept(t, flow.NewFlowExpiredError(expired), json.RawMessage(actual), []string{"use_flow_id", "since"}, "expired", "%s", actual)
})
t.Run("type=browser", func(t *testing.T) {
body, res := run(t, flow.TypeBrowser, "aal1", url.Values{"method": {"password"}}.Encode(), false)
assert.Contains(t, res.Request.URL.String(), loginTS.URL)
assert.Contains(t, gjson.Get(body, "ui.messages.0.text").String(), "expired", "%s", body)
})
t.Run("type=SPA", func(t *testing.T) {
actual, res := run(t, flow.TypeBrowser, "aal1", `{"method":"password"}`, true)
assert.Contains(t, res.Request.URL.String(), login.RouteSubmitFlow)
assert.NotEqual(t, "00000000-0000-0000-0000-000000000000", gjson.Get(actual, "use_flow_id").String())
assertx.EqualAsJSONExcept(t, flow.NewFlowExpiredError(expired), json.RawMessage(actual), []string{"use_flow_id", "since"}, "expired", "%s", actual)
})
})
})
t.Run("lifecycle=init", func(t *testing.T) {
t.Run("flow=api", func(t *testing.T) {
t.Run("case=does not set forced flag on unauthenticated request", func(t *testing.T) {
res, body := initFlow(t, url.Values{}, true)
assert.Contains(t, res.Request.URL.String(), login.RouteInitAPIFlow)
assertion(body, false, true)
assert.Empty(t, gjson.GetBytes(body, "session_token_exchange_code").String())
})
t.Run("case=returns session exchange code", func(t *testing.T) {
res, body := initFlow(t, urlx.ParseOrPanic("/?return_session_token_exchange_code=true").Query(), true)
assert.Contains(t, res.Request.URL.String(), login.RouteInitAPIFlow)
assert.NotEmpty(t, gjson.GetBytes(body, "session_token_exchange_code").String())
})
t.Run("case=can not request refresh and aal at the same time on unauthenticated request", func(t *testing.T) {
res, body := initFlow(t, url.Values{"refresh": {"true"}, "aal": {"aal2"}}, true)
assert.Contains(t, res.Request.URL.String(), login.RouteInitAPIFlow)
assertx.EqualAsJSON(t, "You can not requested a higher AAL (AAL2/AAL3) without an active session.", gjson.GetBytes(body, "error.reason").String())
})
t.Run("case=can request refresh and aal at the same time on authenticated request", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"refresh": {"true"}, "aal": {"aal2"}}, true)
assert.Contains(t, res.Request.URL.String(), login.RouteInitAPIFlow)
assertx.EqualAsJSON(t, "Please confirm this action by verifying that it is you.", gjson.GetBytes(body, "ui.messages.0.text").String(), "%s", body)
assertx.EqualAsJSON(t, "Please complete the second authentication challenge.", gjson.GetBytes(body, "ui.messages.1.text").String(), "%s", body)
})
t.Run("case=can not request aal2 on unauthenticated request", func(t *testing.T) {
res, body := initFlow(t, url.Values{"aal": {"aal2"}}, true)
assert.Contains(t, res.Request.URL.String(), login.RouteInitAPIFlow)
assertx.EqualAsJSON(t, "You can not requested a higher AAL (AAL2/AAL3) without an active session.", gjson.GetBytes(body, "error.reason").String())
})
t.Run("case=ignores aal1 if session has aal1 already", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"aal": {"aal1"}}, true)
assert.Contains(t, res.Request.URL.String(), login.RouteInitAPIFlow)
assertx.EqualAsJSON(t, "A valid session was detected and thus login is not possible. Did you forget to set `?refresh=true`?", gjson.GetBytes(body, "error.reason").String())
})
t.Run("case=aal0 is not a valid value", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"aal": {"aal0"}}, true)
assert.Contains(t, res.Request.URL.String(), login.RouteInitAPIFlow)
assertx.EqualAsJSON(t, "Unable to parse AuthenticationMethod Assurance Level (AAL): expected one of [aal1, aal2] but got aal0", gjson.GetBytes(body, "error.reason").String())
})
t.Run("case=indicates two factor auth", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"aal": {"aal2"}}, true)
assert.Contains(t, res.Request.URL.String(), login.RouteInitAPIFlow)
assert.Equal(t, gjson.GetBytes(body, "ui.messages.0.text").String(), text.NewInfoLoginMFA().Text)
})
t.Run("case=does not set forced flag on unauthenticated request with refresh=true", func(t *testing.T) {
res, body := initFlow(t, url.Values{"refresh": {"true"}}, true)
assert.Contains(t, res.Request.URL.String(), login.RouteInitAPIFlow)
assertion(body, false, true)
})
t.Run("case=does not set forced flag on authenticated request without refresh=true", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{}, true)
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
assertx.EqualAsJSON(t, login.ErrAlreadyLoggedIn, json.RawMessage(gjson.GetBytes(body, "error").Raw), "%s", body)
})
t.Run("case=does not set forced flag on authenticated request with refresh=false", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"refresh": {"false"}}, true)
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
assertx.EqualAsJSON(t, login.ErrAlreadyLoggedIn, json.RawMessage(gjson.GetBytes(body, "error").Raw), "%s", body)
})
t.Run("case=does set forced flag on authenticated request with refresh=true", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"refresh": {"true"}}, true)
assert.Contains(t, res.Request.URL.String(), login.RouteInitAPIFlow)
assertion(body, true, true)
})
t.Run("case=check info message on authenticated request with refresh=true", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"refresh": {"true"}}, true)
assert.Contains(t, res.Request.URL.String(), login.RouteInitAPIFlow)
assertion(body, true, true)
assert.Equal(t, gjson.GetBytes(body, "ui.messages.0.text").String(), text.NewInfoLoginReAuth().Text)
})
})
t.Run("flow=browser", func(t *testing.T) {
t.Run("case=does not set forced flag on unauthenticated request", func(t *testing.T) {
res, body := initFlow(t, url.Values{}, false)
assertion(body, false, false)
assert.Contains(t, res.Request.URL.String(), loginTS.URL)
})
t.Run("case=never returns a session token exchange code", func(t *testing.T) {
_, body := initFlow(t, urlx.ParseOrPanic("/?return_session_token_exchange_code=true").Query(), false)
assertion(body, false, false)
assert.Empty(t, gjson.GetBytes(body, "session_token_exchange_code").String())
})
t.Run("case=can not request refresh and aal at the same time on unauthenticated request", func(t *testing.T) {
res, body := initFlow(t, url.Values{"refresh": {"true"}, "aal": {"aal2"}}, false)
assert.Contains(t, res.Request.URL.String(), errorTS.URL)
assertx.EqualAsJSON(t, "You can not requested a higher AAL (AAL2/AAL3) without an active session.", gjson.GetBytes(body, "reason").String(), "%s", body)
})
t.Run("case=can request refresh and aal at the same time on authenticated request", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"refresh": {"true"}, "aal": {"aal2"}}, false)
assert.Contains(t, res.Request.URL.String(), loginTS.URL)
assertx.EqualAsJSON(t, "Please confirm this action by verifying that it is you.", gjson.GetBytes(body, "ui.messages.0.text").String(), "%s", body)
assertx.EqualAsJSON(t, "Please complete the second authentication challenge.", gjson.GetBytes(body, "ui.messages.1.text").String(), "%s", body)
})
t.Run("case=redirects if aal2 is requested and set up already without refresh", func(t *testing.T) {
res, _ := initAuthenticatedFlow(t, url.Values{"aal": {"aal2"}, "set_aal": {"aal2"}}, false)
assert.Contains(t, res.Request.URL.String(), "https://www.ory.sh")
})
t.Run("case=can not request aal2 on unauthenticated request", func(t *testing.T) {
res, body := initFlow(t, url.Values{"aal": {"aal2"}}, false)
assert.Contains(t, res.Request.URL.String(), errorTS.URL)
assertx.EqualAsJSON(t, "You can not requested a higher AAL (AAL2/AAL3) without an active session.", gjson.GetBytes(body, "reason").String())
})
t.Run("case=ignores aal1 if session has aal1 already", func(t *testing.T) {
res, _ := initAuthenticatedFlow(t, url.Values{"aal": {"aal1"}}, false)
assert.Contains(t, res.Request.URL.String(), "https://www.ory.sh")
})
t.Run("case=aal0 is not a valid value", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"aal": {"aal0"}}, false)
assert.Contains(t, res.Request.URL.String(), errorTS.URL)
assertx.EqualAsJSON(t, "Unable to parse AuthenticationMethod Assurance Level (AAL): expected one of [aal1, aal2] but got aal0", gjson.GetBytes(body, "reason").String())
})
t.Run("case=indicates two factor auth", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"aal": {"aal2"}}, false)
assert.Contains(t, res.Request.URL.String(), loginTS.URL)
assert.Equal(t, gjson.GetBytes(body, "ui.messages.0.text").String(), text.NewInfoLoginMFA().Text)
})
t.Run("case=makes request with JSON", func(t *testing.T) {
res, body := initSPAFlow(t, url.Values{})
assertion(body, false, false)
assert.NotContains(t, res.Request.URL.String(), loginTS.URL)
})
t.Run("case=does not set forced flag on unauthenticated request with refresh=true", func(t *testing.T) {
res, body := initFlow(t, url.Values{"refresh": {"true"}}, false)
assertion(body, false, false)
assert.Contains(t, res.Request.URL.String(), loginTS.URL)
})
t.Run("case=does not set forced flag on authenticated request without refresh=true", func(t *testing.T) {
res, _ := initAuthenticatedFlow(t, url.Values{}, false)
assert.Contains(t, res.Request.URL.String(), "https://www.ory.sh")
})
t.Run("case=does not set forced flag on authenticated request with refresh=false", func(t *testing.T) {
res, _ := initAuthenticatedFlow(t, url.Values{"refresh": {"false"}}, false)
assert.Contains(t, res.Request.URL.String(), "https://www.ory.sh")
})
t.Run("case=does set forced flag on authenticated request with refresh=true", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"refresh": {"true"}}, false)
assertion(body, true, false)
assert.Contains(t, res.Request.URL.String(), loginTS.URL)
})
t.Run("case=redirects with 303", func(t *testing.T) {
c := &http.Client{}
// don't get the reference, instead copy the values, so we don't alter the client directly.
*c = *ts.Client()
// prevent the redirect
c.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
req, err := http.NewRequest("GET", ts.URL+login.RouteInitBrowserFlow, nil)
require.NoError(t, err)
res, err := c.Do(req)
require.NoError(t, err)
defer res.Body.Close()
// here we check that the redirect status is 303
require.Equal(t, http.StatusSeeOther, res.StatusCode)
})
t.Run("case=refuses to parse oauth2 login challenge when Hydra is not configured", func(t *testing.T) {
res, body := initAuthenticatedFlow(t, url.Values{"login_challenge": {hydra.FAKE_GET_LOGIN_REQUEST_RETURN_NIL_NIL}}, false)
require.Contains(t, res.Request.URL.String(), errorTS.URL)
require.Contains(t, string(body), "refusing to parse")
})
conf.MustSet(ctx, config.ViperKeyOAuth2ProviderURL, "https://fake-hydra")
t.Run("case=oauth2 flow init should override return_to to the oauth2 request_url", func(t *testing.T) {
conf.MustSet(ctx, config.ViperKeyURLsAllowedReturnToDomains, []string{"https://www.ory.sh", "https://example.com"})
conf.MustSet(ctx, config.ViperKeyOAuth2ProviderOverrideReturnTo, true)
t.Cleanup(func() {
conf.MustSet(ctx, config.ViperKeyOAuth2ProviderOverrideReturnTo, false)
})
res, _ := initUnauthenticatedFlow(t, url.Values{
"return_to": {"https://example.com"},
"login_challenge": {hydra.FAKE_SUCCESS},
}, false)
require.Equal(t, http.StatusOK, res.StatusCode)
require.Contains(t, res.Request.URL.String(), loginTS.URL)
c := ts.Client()
req := x.NewTestHTTPRequest(t, "GET", ts.URL+login.RouteGetFlow, nil)
req.URL.RawQuery = url.Values{"id": {res.Request.URL.Query().Get("flow")}}.Encode()
res, err := c.Do(req)
require.NoError(t, err)
body, err := io.ReadAll(res.Body)
require.NoError(t, errors.WithStack(err))
require.NoError(t, res.Body.Close())
assert.Equal(t, "https://www.ory.sh", gjson.GetBytes(body, "return_to").Value())
})
t.Run("case=oauth2 flow init succeeds", func(t *testing.T) {
res, _ := initAuthenticatedFlow(t, url.Values{"login_challenge": {hydra.FAKE_SUCCESS}}, false)
require.Contains(t, res.Request.URL.String(), loginTS.URL)
})
t.Run("case=oauth2 flow init adds oauth2_login_request field", func(t *testing.T) {
res, body := initSPAFlow(t, url.Values{"login_challenge": {hydra.FAKE_SUCCESS}})
assert.NotContains(t, res.Request.URL.String(), loginTS.URL)
assert.NotEmpty(t, gjson.GetBytes(body, "oauth2_login_request").Value(), "%s", body)
})
})
t.Run("case=relative redirect when self-service login ui is a relative URL", func(t *testing.T) {
reg.Config().MustSet(ctx, config.ViperKeySelfServiceLoginUI, "/login-ts")
assert.Regexp(
t,
"^/login-ts.*$",
testhelpers.GetSelfServiceRedirectLocation(t, ts.URL+login.RouteInitBrowserFlow),
)
})
})
}
func TestGetFlow(t *testing.T) {
ctx := context.Background()
conf, reg := internal.NewFastRegistryWithMocks(t)
public, _ := testhelpers.NewKratosServerWithCSRF(t, reg)
_ = testhelpers.NewErrorTestServer(t, reg)
_ = testhelpers.NewRedirTS(t, "", conf)
setupLoginUI := func(t *testing.T, c *http.Client) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// It is important that we use a HTTP request to fetch the flow because that will show us if CSRF works or not
_, err := w.Write(x.EasyGetBody(t, c, public.URL+login.RouteGetFlow+"?id="+r.URL.Query().Get("flow")))
require.NoError(t, err)
}))
conf.MustSet(ctx, config.ViperKeySelfServiceLoginUI, ts.URL)
conf.MustSet(ctx, config.ViperKeySelfServiceBrowserDefaultReturnTo, "https://www.ory.sh")
t.Cleanup(ts.Close)
return ts
}
_ = testhelpers.NewLoginUIFlowEchoServer(t, reg)
conf.MustSet(ctx, config.ViperKeySelfServiceStrategyConfig+"."+string(identity.CredentialsTypePassword), map[string]interface{}{
"enabled": true})
t.Run("case=fetching successful", func(t *testing.T) {
client := testhelpers.NewClientWithCookies(t)
setupLoginUI(t, client)
body := x.EasyGetBody(t, client, public.URL+login.RouteInitBrowserFlow)
assert.NotEmpty(t, gjson.GetBytes(body, "ui.nodes.#(attributes.name==csrf_token).attributes.value").String(), "%s", body)
assert.NotEmpty(t, gjson.GetBytes(body, "id").String(), "%s", body)
assert.Empty(t, gjson.GetBytes(body, "headers").Value(), "%s", body)
assert.Contains(t, gjson.GetBytes(body, "ui.action").String(), gjson.GetBytes(body, "id").String(), "%s", body)
assert.Contains(t, gjson.GetBytes(body, "ui.action").String(), public.URL, "%s", body)
})
t.Run("case=csrf cookie missing", func(t *testing.T) {
client := http.DefaultClient
setupLoginUI(t, client)
body := x.EasyGetBody(t, client, public.URL+login.RouteInitBrowserFlow)
assert.EqualValues(t, x.ErrInvalidCSRFToken.ReasonField, gjson.GetBytes(body, "error.reason").String(), "%s", body)
})
t.Run("case=expired", func(t *testing.T) {
client := testhelpers.NewClientWithCookies(t)
setupLoginUI(t, client)
body := x.EasyGetBody(t, client, public.URL+login.RouteInitBrowserFlow)
// Expire the flow
f, err := reg.LoginFlowPersister().GetLoginFlow(context.Background(), uuid.FromStringOrNil(gjson.GetBytes(body, "id").String()))
require.NoError(t, err)
f.ExpiresAt = time.Now().Add(-time.Second)
require.NoError(t, reg.LoginFlowPersister().UpdateLoginFlow(context.Background(), f))
// Try the flow but it is expired
res, body := x.EasyGet(t, client, public.URL+login.RouteGetFlow+"?id="+f.ID.String())
assert.EqualValues(t, http.StatusGone, res.StatusCode)
assert.Equal(t, public.URL+login.RouteInitBrowserFlow, gjson.GetBytes(body, "error.details.redirect_to").String(), "%s", body)
})
t.Run("case=expired with return_to", func(t *testing.T) {
returnTo := "https://www.ory.sh"
conf.MustSet(ctx, config.ViperKeyURLsAllowedReturnToDomains, []string{returnTo})
client := testhelpers.NewClientWithCookies(t)
setupLoginUI(t, client)
body := x.EasyGetBody(t, client, public.URL+login.RouteInitBrowserFlow+"?return_to="+returnTo)
// Expire the flow
f, err := reg.LoginFlowPersister().GetLoginFlow(context.Background(), uuid.FromStringOrNil(gjson.GetBytes(body, "id").String()))
require.NoError(t, err)
f.ExpiresAt = time.Now().Add(-time.Second)
require.NoError(t, reg.LoginFlowPersister().UpdateLoginFlow(context.Background(), f))
// Retrieve the flow and verify that return_to is in the response
getURL := fmt.Sprintf("%s%s?id=%s&return_to=%s", public.URL, login.RouteGetFlow, f.ID, returnTo)
getBody := x.EasyGetBody(t, client, getURL)
assert.Equal(t, gjson.GetBytes(getBody, "error.details.return_to").String(), returnTo)
// submit the flow but it is expired
u := public.URL + login.RouteSubmitFlow + "?flow=" + f.ID.String()
res, err := client.PostForm(u, url.Values{"password_identifier": {"email@ory.sh"}, "csrf_token": {f.CSRFToken}, "password": {"password"}, "method": {"password"}})
require.NoError(t, err)
resBody, err := io.ReadAll(res.Body)
require.NoError(t, err)
require.NoError(t, res.Body.Close())
f, err = reg.LoginFlowPersister().GetLoginFlow(context.Background(), uuid.FromStringOrNil(gjson.GetBytes(resBody, "id").String()))
require.NoError(t, err)
assert.Equal(t, public.URL+login.RouteInitBrowserFlow+"?return_to="+returnTo, f.RequestURL)
})
t.Run("case=not found", func(t *testing.T) {
client := testhelpers.NewClientWithCookies(t)
setupLoginUI(t, client)
res, _ := x.EasyGet(t, client, public.URL+login.RouteGetFlow+"?id="+x.NewUUID().String())
assert.EqualValues(t, http.StatusNotFound, res.StatusCode)
})
}