@@ -12,6 +12,7 @@ import (
12
12
"testing"
13
13
"time"
14
14
15
+ "github.com/ory/kratos/driver"
15
16
"github.com/ory/kratos/session"
16
17
17
18
"github.com/davecgh/go-spew/spew"
@@ -56,6 +57,23 @@ func init() {
56
57
corpx .RegisterFakes ()
57
58
}
58
59
60
+ func createIdentityToRecover (t * testing.T , reg * driver.RegistryDefault , email string ) * identity.Identity {
61
+ var id = & identity.Identity {
62
+ Credentials : map [identity.CredentialsType ]identity.Credentials {
63
+ "password" : {Type : "password" , Identifiers : []string {email }, Config : sqlxx .JSONRawMessage (`{"hashed_password":"foo"}` )}},
64
+ Traits : identity .Traits (fmt .Sprintf (`{"email":"%s"}` , email )),
65
+ SchemaID : config .DefaultIdentityTraitsSchemaID ,
66
+ }
67
+ require .NoError (t , reg .IdentityManager ().Create (context .Background (), id , identity .ManagerAllowWriteProtectedTraits ))
68
+
69
+ addr , err := reg .IdentityPool ().FindVerifiableAddressByValue (context .Background (), identity .VerifiableAddressTypeEmail , email )
70
+ assert .NoError (t , err )
71
+ assert .False (t , addr .Verified )
72
+ assert .Nil (t , addr .VerifiedAt )
73
+ assert .Equal (t , identity .VerifiableAddressStatusPending , addr .Status )
74
+ return id
75
+ }
76
+
59
77
func TestAdminStrategy (t * testing.T ) {
60
78
ctx := context .Background ()
61
79
conf , reg := internal .NewFastRegistryWithMocks (t )
@@ -183,6 +201,59 @@ func TestAdminStrategy(t *testing.T) {
183
201
assert .Nil (t , addr .VerifiedAt )
184
202
assert .Equal (t , identity .VerifiableAddressStatusPending , addr .Status )
185
203
})
204
+
205
+ t .Run ("case=should not be able to use code from different flow" , func (t * testing.T ) {
206
+ email := strings .ToLower (testhelpers .RandomEmail ())
207
+ id := createIdentityToRecover (t , reg , email )
208
+
209
+ rl1 , _ , err := adminSDK .V0alpha2Api .
210
+ AdminCreateSelfServiceRecoveryLink (context .Background ()).
211
+ AdminCreateSelfServiceRecoveryLinkBody (kratos.AdminCreateSelfServiceRecoveryLinkBody {
212
+ IdentityId : id .ID .String (),
213
+ }).
214
+ Execute ()
215
+ require .NoError (t , err )
216
+
217
+ checkLink (t , rl1 , time .Now ().Add (conf .SelfServiceFlowRecoveryRequestLifespan (ctx )+ time .Second ))
218
+
219
+ rl2 , _ , err := adminSDK .V0alpha2Api .
220
+ AdminCreateSelfServiceRecoveryLink (context .Background ()).
221
+ AdminCreateSelfServiceRecoveryLinkBody (kratos.AdminCreateSelfServiceRecoveryLinkBody {
222
+ IdentityId : id .ID .String (),
223
+ }).
224
+ Execute ()
225
+ require .NoError (t , err )
226
+
227
+ checkLink (t , rl2 , time .Now ().Add (conf .SelfServiceFlowRecoveryRequestLifespan (ctx )+ time .Second ))
228
+
229
+ recoveryUrl1 , err := url .Parse (rl1 .RecoveryLink )
230
+ require .NoError (t , err )
231
+
232
+ recoveryUrl2 , err := url .Parse (rl2 .RecoveryLink )
233
+ require .NoError (t , err )
234
+
235
+ token1 := recoveryUrl1 .Query ().Get ("token" )
236
+ require .NotEmpty (t , token1 )
237
+ token2 := recoveryUrl2 .Query ().Get ("token" )
238
+ require .NotEmpty (t , token2 )
239
+ require .NotEqual (t , token1 , token2 )
240
+
241
+ values := recoveryUrl1 .Query ()
242
+
243
+ values .Set ("token" , token2 )
244
+
245
+ recoveryUrl1 .RawQuery = values .Encode ()
246
+
247
+ action := recoveryUrl1 .String ()
248
+ // Submit the modified link with token from rl2 and flow from rl1
249
+ res , err := publicTS .Client ().Get (action )
250
+ require .NoError (t , err )
251
+ body := ioutilx .MustReadAll (res .Body )
252
+
253
+ action = gjson .GetBytes (body , "ui.action" ).String ()
254
+ require .NotEmpty (t , action )
255
+ assert .Equal (t , "The recovery token is invalid or has already been used. Please retry the flow." , gjson .GetBytes (body , "ui.messages.0.text" ).String ())
256
+ })
186
257
}
187
258
188
259
func TestRecovery (t * testing.T ) {
@@ -197,23 +268,6 @@ func TestRecovery(t *testing.T) {
197
268
198
269
public , _ , publicRouter , _ := testhelpers .NewKratosServerWithCSRFAndRouters (t , reg )
199
270
200
- var createIdentityToRecover = func (email string ) * identity.Identity {
201
- var id = & identity.Identity {
202
- Credentials : map [identity.CredentialsType ]identity.Credentials {
203
- "password" : {Type : "password" , Identifiers : []string {email }, Config : sqlxx .JSONRawMessage (`{"hashed_password":"foo"}` )}},
204
- Traits : identity .Traits (fmt .Sprintf (`{"email":"%s"}` , email )),
205
- SchemaID : config .DefaultIdentityTraitsSchemaID ,
206
- }
207
- require .NoError (t , reg .IdentityManager ().Create (context .Background (), id , identity .ManagerAllowWriteProtectedTraits ))
208
-
209
- addr , err := reg .IdentityPool ().FindVerifiableAddressByValue (context .Background (), identity .VerifiableAddressTypeEmail , email )
210
- assert .NoError (t , err )
211
- assert .False (t , addr .Verified )
212
- assert .Nil (t , addr .VerifiedAt )
213
- assert .Equal (t , identity .VerifiableAddressStatusPending , addr .Status )
214
- return id
215
- }
216
-
217
271
var expect = func (t * testing.T , hc * http.Client , isAPI , isSPA bool , values func (url.Values ), c int ) string {
218
272
if hc == nil {
219
273
hc = testhelpers .NewDebugClient (t )
@@ -414,23 +468,23 @@ func TestRecovery(t *testing.T) {
414
468
415
469
t .Run ("type=browser" , func (t * testing.T ) {
416
470
417
- createIdentityToRecover (email )
471
+ createIdentityToRecover (t , reg , email )
418
472
check (t , expectSuccess (t , nil , false , false , func (v url.Values ) {
419
473
v .Set ("email" , email )
420
474
}), email , false )
421
475
})
422
476
423
477
t .Run ("type=spa" , func (t * testing.T ) {
424
478
425
- createIdentityToRecover (email )
479
+ createIdentityToRecover (t , reg , email )
426
480
check (t , expectSuccess (t , nil , true , true , func (v url.Values ) {
427
481
v .Set ("email" , email )
428
482
}), email , true )
429
483
})
430
484
431
485
t .Run ("type=api" , func (t * testing.T ) {
432
486
433
- createIdentityToRecover (email )
487
+ createIdentityToRecover (t , reg , email )
434
488
check (t , expectSuccess (t , nil , true , false , func (v url.Values ) {
435
489
v .Set ("email" , email )
436
490
}), email , true )
@@ -487,7 +541,7 @@ func TestRecovery(t *testing.T) {
487
541
488
542
t .Run ("type=browser" , func (t * testing.T ) {
489
543
490
- createIdentityToRecover (email )
544
+ createIdentityToRecover (t , reg , email )
491
545
check (t , expectSuccess (t , nil , false , false , func (v url.Values ) {
492
546
v .Set ("email" , email )
493
547
}), email , "" )
@@ -496,7 +550,7 @@ func TestRecovery(t *testing.T) {
496
550
t .Run ("type=browser set return_to" , func (t * testing.T ) {
497
551
498
552
returnTo := "https://www.ory.sh"
499
- createIdentityToRecover (email )
553
+ createIdentityToRecover (t , reg , email )
500
554
501
555
hc := testhelpers .NewClientWithCookies (t )
502
556
hc .Transport = testhelpers .NewTransportWithLogger (http .DefaultTransport , t ).RoundTripper
@@ -518,15 +572,15 @@ func TestRecovery(t *testing.T) {
518
572
519
573
t .Run ("type=spa" , func (t * testing.T ) {
520
574
521
- createIdentityToRecover (email )
575
+ createIdentityToRecover (t , reg , email )
522
576
check (t , expectSuccess (t , nil , true , true , func (v url.Values ) {
523
577
v .Set ("email" , email )
524
578
}), email , "" )
525
579
})
526
580
527
581
t .Run ("type=api" , func (t * testing.T ) {
528
582
529
- createIdentityToRecover (email )
583
+ createIdentityToRecover (t , reg , email )
530
584
check (t , expectSuccess (t , nil , true , false , func (v url.Values ) {
531
585
v .Set ("email" , email )
532
586
}), email , "" )
@@ -563,7 +617,7 @@ func TestRecovery(t *testing.T) {
563
617
}
564
618
565
619
email := x .NewUUID ().String () + "@ory.sh"
566
- id := createIdentityToRecover (email )
620
+ id := createIdentityToRecover (t , reg , email )
567
621
568
622
t .Run ("case=unauthenticated" , func (t * testing.T ) {
569
623
var values = func (v url.Values ) {
@@ -604,7 +658,7 @@ func TestRecovery(t *testing.T) {
604
658
605
659
recoveryEmail := strings .ToLower (testhelpers .RandomEmail ())
606
660
email := recoveryEmail
607
- id := createIdentityToRecover (email )
661
+ id := createIdentityToRecover (t , reg , email )
608
662
609
663
sess , err := session .NewActiveSession (ctx , id , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
610
664
require .NoError (t , err )
@@ -659,7 +713,7 @@ func TestRecovery(t *testing.T) {
659
713
660
714
t .Run ("description=should not be able to use an outdated link" , func (t * testing.T ) {
661
715
recoveryEmail := "[email protected] "
662
- createIdentityToRecover (recoveryEmail )
716
+ createIdentityToRecover (t , reg , recoveryEmail )
663
717
conf .MustSet (ctx , config .ViperKeySelfServiceRecoveryRequestLifespan , time .Millisecond * 200 )
664
718
t .Cleanup (func () {
665
719
conf .MustSet (ctx , config .ViperKeySelfServiceRecoveryRequestLifespan , time .Minute )
@@ -685,7 +739,7 @@ func TestRecovery(t *testing.T) {
685
739
686
740
t .Run ("description=should not be able to use an outdated flow" , func (t * testing.T ) {
687
741
recoveryEmail := "[email protected] "
688
- createIdentityToRecover (recoveryEmail )
742
+ createIdentityToRecover (t , reg , recoveryEmail )
689
743
conf .MustSet (ctx , config .ViperKeySelfServiceRecoveryRequestLifespan , time .Millisecond * 200 )
690
744
t .Cleanup (func () {
691
745
conf .MustSet (ctx , config .ViperKeySelfServiceRecoveryRequestLifespan , time .Minute )
0 commit comments