@@ -397,6 +397,84 @@ public function testGetLoginRedirect(): void
397397 Configure::delete ('App.base ' );
398398 }
399399
400+ /**
401+ * testRedirectAfterLogin
402+ *
403+ * @return void
404+ */
405+ public function testRedirectAfterLogin (): void
406+ {
407+ Configure::write ('App.base ' , '/cakephp ' );
408+ $ url = ['controller ' => 'Users ' , 'action ' => 'dashboard ' ];
409+ Router::createRouteBuilder ('/ ' )
410+ ->connect ('/dashboard ' , $ url );
411+
412+ $ this ->service ->setConfig ('queryParam ' , 'redirect ' );
413+ $ request = $ this ->request
414+ ->withAttribute ('identity ' , $ this ->identity )
415+ ->withAttribute ('authentication ' , $ this ->service )
416+ ->withQueryParams (['redirect ' => 'ok/path?value=key ' ]);
417+
418+ $ controller = new Controller ($ request );
419+ $ registry = new ComponentRegistry ($ controller );
420+ $ component = new AuthenticationComponent ($ registry );
421+
422+ $ response = $ component ->redirectAfterLogin ($ url );
423+ $ this ->assertSame (Router::url ('/ok/path?value=key ' ), $ response ?->getHeaderLine('Location ' ));
424+
425+ Configure::delete ('App.base ' );
426+ }
427+
428+ /**
429+ * testRedirectAfterLoginFallsBackToDefaultForAbsoluteUrls
430+ *
431+ * @return void
432+ */
433+ public function testRedirectAfterLoginFallsBackToDefaultForAbsoluteUrls (): void
434+ {
435+ $ url = ['controller ' => 'Users ' , 'action ' => 'dashboard ' ];
436+ Router::createRouteBuilder ('/ ' )
437+ ->connect ('/dashboard ' , $ url );
438+
439+ $ this ->service ->setConfig ('queryParam ' , 'redirect ' );
440+ $ request = $ this ->request
441+ ->withAttribute ('identity ' , $ this ->identity )
442+ ->withAttribute ('authentication ' , $ this ->service )
443+ ->withQueryParams (['redirect ' => 'https://evil.example/phish ' ]);
444+
445+ $ controller = new Controller ($ request );
446+ $ registry = new ComponentRegistry ($ controller );
447+ $ component = new AuthenticationComponent ($ registry );
448+
449+ $ response = $ component ->redirectAfterLogin ($ url );
450+ $ this ->assertSame ('/dashboard ' , $ response ?->getHeaderLine('Location ' ));
451+ }
452+
453+ /**
454+ * testRedirectAfterLoginFallsBackToDefaultForProtocolRelativeUrls
455+ *
456+ * @return void
457+ */
458+ public function testRedirectAfterLoginFallsBackToDefaultForProtocolRelativeUrls (): void
459+ {
460+ $ url = ['controller ' => 'Users ' , 'action ' => 'dashboard ' ];
461+ Router::createRouteBuilder ('/ ' )
462+ ->connect ('/dashboard ' , $ url );
463+
464+ $ this ->service ->setConfig ('queryParam ' , 'redirect ' );
465+ $ request = $ this ->request
466+ ->withAttribute ('identity ' , $ this ->identity )
467+ ->withAttribute ('authentication ' , $ this ->service )
468+ ->withQueryParams (['redirect ' => '//evil.example/phish ' ]);
469+
470+ $ controller = new Controller ($ request );
471+ $ registry = new ComponentRegistry ($ controller );
472+ $ component = new AuthenticationComponent ($ registry );
473+
474+ $ response = $ component ->redirectAfterLogin ($ url );
475+ $ this ->assertSame ('/dashboard ' , $ response ?->getHeaderLine('Location ' ));
476+ }
477+
400478 /**
401479 * testAfterIdentifyEvent
402480 *
0 commit comments