@@ -11,6 +11,8 @@ import (
11
11
"testing"
12
12
"time"
13
13
14
+ "github.com/ory/x/urlx"
15
+
14
16
"github.com/ory/x/sqlxx"
15
17
16
18
"github.com/ory/kratos/selfservice/flow"
@@ -233,6 +235,73 @@ func TestFlowLifecycle(t *testing.T) {
233
235
assert .NotEqual (t , gjson .Get (b , "session.id" ).String (), gjson .Get (a , "id" ).String ())
234
236
})
235
237
})
238
+
239
+ t .Run ("case=changed kratos session identifiers when refresh is true" , func (t * testing.T ) {
240
+ t .Cleanup (func () {
241
+ conf .MustSet (ctx , config .ViperKeySelfServiceBrowserDefaultReturnTo , "https://www.ory.sh" )
242
+ })
243
+
244
+ t .Run ("type=browser" , func (t * testing.T ) {
245
+ // Setup flow
246
+ f := login.Flow {Type : flow .TypeBrowser , ExpiresAt : time .Now ().Add (time .Minute ), IssuedAt : time .Now (), UI : container .New ("" ), Refresh : false , RequestedAAL : "aal1" }
247
+ require .NoError (t , reg .LoginFlowPersister ().CreateLoginFlow (context .Background (), & f ))
248
+
249
+ // Submit Login
250
+ hc := testhelpers .NewClientWithCookies (t )
251
+ 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 }})
252
+ require .NoError (t , err )
253
+
254
+ // Check response and session cookie presence
255
+ assert .Equal (t , http .StatusOK , res .StatusCode )
256
+ require .Len (t , hc .Jar .Cookies (urlx .ParseOrPanic (ts .URL + login .RouteGetFlow )), 1 )
257
+ require .Contains (t , fmt .Sprintf ("%v" , hc .Jar .Cookies (urlx .ParseOrPanic (ts .URL ))), "ory_kratos_session" )
258
+ cookies1 := hc .Jar .Cookies (urlx .ParseOrPanic (ts .URL + login .RouteGetFlow ))
259
+
260
+ req , err := http .NewRequest ("GET" , ts .URL + "/sessions/whoami" , nil )
261
+ require .NoError (t , err )
262
+
263
+ res , err = hc .Do (req )
264
+ require .NoError (t , err )
265
+ assert .Equal (t , http .StatusOK , res .StatusCode )
266
+ firstSession := x .MustReadAll (res .Body )
267
+ require .NoError (t , res .Body .Close ())
268
+
269
+ // Refresh
270
+ f = login.Flow {Type : flow .TypeBrowser , ExpiresAt : time .Now ().Add (time .Minute ), IssuedAt : time .Now (), UI : container .New ("" ), Refresh : true , RequestedAAL : "aal1" }
271
+ require .NoError (t , reg .LoginFlowPersister ().CreateLoginFlow (context .Background (), & f ))
272
+
273
+ vv := testhelpers .EncodeFormAsJSON (t , false , url.Values {"method" : {"password" }, "password_identifier" : {id1mail }, "password" : {"foobar" }, "csrf_token" : {x .FakeCSRFToken }})
274
+
275
+ req , err = http .NewRequest ("POST" , ts .URL + login .RouteSubmitFlow + "?flow=" + f .ID .String (), strings .NewReader (vv ))
276
+ require .NoError (t , err )
277
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
278
+
279
+ // Submit Login
280
+ res , err = hc .Do (req )
281
+ require .NoError (t , err )
282
+
283
+ // Check response and session cookie presence
284
+ assert .Equal (t , http .StatusOK , res .StatusCode )
285
+ require .Len (t , hc .Jar .Cookies (urlx .ParseOrPanic (ts .URL + login .RouteGetFlow )), 1 )
286
+ require .Contains (t , fmt .Sprintf ("%v" , hc .Jar .Cookies (urlx .ParseOrPanic (ts .URL ))), "ory_kratos_session" )
287
+ cookies2 := hc .Jar .Cookies (urlx .ParseOrPanic (ts .URL + login .RouteGetFlow ))
288
+
289
+ req , err = http .NewRequest ("GET" , ts .URL + "/sessions/whoami" , nil )
290
+ require .NoError (t , err )
291
+
292
+ res , err = hc .Do (req )
293
+ require .NoError (t , err )
294
+ assert .Equal (t , http .StatusOK , res .StatusCode )
295
+ secondSession := x .MustReadAll (res .Body )
296
+ require .NoError (t , res .Body .Close ())
297
+
298
+ // Sessions should still be resolvable despite different kratos session identifier due to nonce
299
+ assert .NotEqual (t , cookies1 [0 ].String (), cookies2 [0 ].String ())
300
+ assert .Equal (t , id1mail , gjson .Get (string (firstSession ), "identity.traits.username" ).String ())
301
+ assert .Equal (t , id1mail , gjson .Get (string (secondSession ), "identity.traits.username" ).String ())
302
+ assert .Equal (t , gjson .Get (string (secondSession ), "id" ).String (), gjson .Get (string (firstSession ), "id" ).String ())
303
+ })
304
+ })
236
305
})
237
306
238
307
t .Run ("case=ensure aal is checked for upgradeability on session" , func (t * testing.T ) {
0 commit comments