diff --git a/src/Authenticator/EnvironmentAuthenticator.php b/src/Authenticator/EnvironmentAuthenticator.php index f5c04068..40959a00 100644 --- a/src/Authenticator/EnvironmentAuthenticator.php +++ b/src/Authenticator/EnvironmentAuthenticator.php @@ -17,6 +17,7 @@ namespace Authentication\Authenticator; use Authentication\UrlChecker\UrlCheckerTrait; +use Cake\Routing\Router; use Psr\Http\Message\ServerRequestInterface; /** @@ -114,11 +115,18 @@ protected function _buildLoginUrlErrorResult(ServerRequestInterface $request): R $uri = $uri->getPath(); } + $loginUrls = (array)$this->getConfig('loginUrl'); + foreach ($loginUrls as $key => $loginUrl) { + if (is_array($loginUrl)) { + $loginUrls[$key] = Router::url($loginUrl); + } + } + $errors = [ sprintf( 'Login URL `%s` did not match `%s`.', $uri, - implode('` or `', (array)$this->getConfig('loginUrl')), + implode('` or `', $loginUrls), ), ]; diff --git a/src/Authenticator/FormAuthenticator.php b/src/Authenticator/FormAuthenticator.php index 53a41fd5..07bbe9d7 100644 --- a/src/Authenticator/FormAuthenticator.php +++ b/src/Authenticator/FormAuthenticator.php @@ -18,6 +18,7 @@ use Authentication\Identifier\AbstractIdentifier; use Authentication\UrlChecker\UrlCheckerTrait; +use Cake\Routing\Router; use Psr\Http\Message\ServerRequestInterface; /** @@ -96,11 +97,18 @@ protected function _buildLoginUrlErrorResult(ServerRequestInterface $request): R $uri = $uri->getPath(); } + $loginUrls = (array)$this->getConfig('loginUrl'); + foreach ($loginUrls as $key => $loginUrl) { + if (is_array($loginUrl)) { + $loginUrls[$key] = Router::url($loginUrl); + } + } + $errors = [ sprintf( 'Login URL `%s` did not match `%s`.', $uri, - implode('` or `', (array)$this->getConfig('loginUrl')), + implode('` or `', $loginUrls), ), ]; diff --git a/tests/TestCase/Authenticator/EnvironmentAuthenticatorTest.php b/tests/TestCase/Authenticator/EnvironmentAuthenticatorTest.php index 67a6f0d1..a5eb2d4d 100644 --- a/tests/TestCase/Authenticator/EnvironmentAuthenticatorTest.php +++ b/tests/TestCase/Authenticator/EnvironmentAuthenticatorTest.php @@ -21,6 +21,7 @@ use Authentication\Identifier\IdentifierCollection; use Authentication\Test\TestCase\AuthenticationTestCase as TestCase; use Cake\Http\ServerRequestFactory; +use Cake\Routing\Router; use RuntimeException; class EnvironmentAuthenticatorTest extends TestCase @@ -261,10 +262,13 @@ public function testSingleLoginUrlMismatch() */ public function testMultipleLoginUrlMismatch() { + Router::createRouteBuilder('/') + ->connect('/{lang}/secure', ['controller' => 'Users', 'action' => 'login']); + $envAuth = new EnvironmentAuthenticator($this->identifiers, [ 'loginUrl' => [ - '/en/secure', - '/de/secure', + ['lang' => 'en', 'controller' => 'Users', 'action' => 'login'], + ['lang' => 'de', 'controller' => 'Users', 'action' => 'login'], ], 'fields' => [ 'USER_ID', diff --git a/tests/TestCase/Authenticator/FormAuthenticatorTest.php b/tests/TestCase/Authenticator/FormAuthenticatorTest.php index 15db8cd0..3287f961 100644 --- a/tests/TestCase/Authenticator/FormAuthenticatorTest.php +++ b/tests/TestCase/Authenticator/FormAuthenticatorTest.php @@ -21,6 +21,7 @@ use Authentication\Identifier\IdentifierCollection; use Authentication\Test\TestCase\AuthenticationTestCase as TestCase; use Cake\Http\ServerRequestFactory; +use Cake\Routing\Router; use RuntimeException; class FormAuthenticatorTest extends TestCase @@ -156,10 +157,14 @@ public function testMultipleLoginUrlMismatch() ['username' => 'mariano', 'password' => 'password'], ); + Router::createRouteBuilder('/') + ->connect('/{lang}/users/login', ['controller' => 'Users', 'action' => 'login']); + $form = new FormAuthenticator($identifiers, [ + 'urlChecker' => 'Authentication.CakeRouter', 'loginUrl' => [ - '/en/users/login', - '/de/users/login', + ['lang' => 'en', 'controller' => 'Users', 'action' => 'login'], + ['lang' => 'de', 'controller' => 'Users', 'action' => 'login'], ], ]);