Skip to content

Commit dd013ec

Browse files
committed
Fix error message generation when using multiple array login URLs.
1 parent 058de14 commit dd013ec

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/Authenticator/EnvironmentAuthenticator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace Authentication\Authenticator;
1818

1919
use Authentication\UrlChecker\UrlCheckerTrait;
20+
use Cake\Routing\Router;
2021
use Psr\Http\Message\ServerRequestInterface;
2122

2223
/**
@@ -114,11 +115,18 @@ protected function _buildLoginUrlErrorResult(ServerRequestInterface $request): R
114115
$uri = $uri->getPath();
115116
}
116117

118+
$loginUrls = (array)$this->getConfig('loginUrl');
119+
foreach ($loginUrls as $key => $loginUrl) {
120+
if (is_array($loginUrl)) {
121+
$loginUrls[$key] = Router::url($loginUrl);
122+
}
123+
}
124+
117125
$errors = [
118126
sprintf(
119127
'Login URL `%s` did not match `%s`.',
120128
$uri,
121-
implode('` or `', (array)$this->getConfig('loginUrl')),
129+
implode('` or `', $loginUrls),
122130
),
123131
];
124132

src/Authenticator/FormAuthenticator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use Authentication\Identifier\AbstractIdentifier;
2020
use Authentication\UrlChecker\UrlCheckerTrait;
21+
use Cake\Routing\Router;
2122
use Psr\Http\Message\ServerRequestInterface;
2223

2324
/**
@@ -96,11 +97,18 @@ protected function _buildLoginUrlErrorResult(ServerRequestInterface $request): R
9697
$uri = $uri->getPath();
9798
}
9899

100+
$loginUrls = (array)$this->getConfig('loginUrl');
101+
foreach ($loginUrls as $key => $loginUrl) {
102+
if (is_array($loginUrl)) {
103+
$loginUrls[$key] = Router::url($loginUrl);
104+
}
105+
}
106+
99107
$errors = [
100108
sprintf(
101109
'Login URL `%s` did not match `%s`.',
102110
$uri,
103-
implode('` or `', (array)$this->getConfig('loginUrl')),
111+
implode('` or `', $loginUrls),
104112
),
105113
];
106114

tests/TestCase/Authenticator/EnvironmentAuthenticatorTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Authentication\Identifier\IdentifierCollection;
2222
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;
2323
use Cake\Http\ServerRequestFactory;
24+
use Cake\Routing\Router;
2425
use RuntimeException;
2526

2627
class EnvironmentAuthenticatorTest extends TestCase
@@ -261,10 +262,13 @@ public function testSingleLoginUrlMismatch()
261262
*/
262263
public function testMultipleLoginUrlMismatch()
263264
{
265+
Router::createRouteBuilder('/')
266+
->connect('/{lang}/secure', ['controller' => 'Users', 'action' => 'login']);
267+
264268
$envAuth = new EnvironmentAuthenticator($this->identifiers, [
265269
'loginUrl' => [
266-
'/en/secure',
267-
'/de/secure',
270+
['lang' => 'en', 'controller' => 'Users', 'action' => 'login'],
271+
['lang' => 'de', 'controller' => 'Users', 'action' => 'login'],
268272
],
269273
'fields' => [
270274
'USER_ID',

tests/TestCase/Authenticator/FormAuthenticatorTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Authentication\Identifier\IdentifierCollection;
2222
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;
2323
use Cake\Http\ServerRequestFactory;
24+
use Cake\Routing\Router;
2425
use RuntimeException;
2526

2627
class FormAuthenticatorTest extends TestCase
@@ -156,10 +157,14 @@ public function testMultipleLoginUrlMismatch()
156157
['username' => 'mariano', 'password' => 'password'],
157158
);
158159

160+
Router::createRouteBuilder('/')
161+
->connect('/{lang}/users/login', ['controller' => 'Users', 'action' => 'login']);
162+
159163
$form = new FormAuthenticator($identifiers, [
164+
'urlChecker' => 'Authentication.CakeRouter',
160165
'loginUrl' => [
161-
'/en/users/login',
162-
'/de/users/login',
166+
['lang' => 'en', 'controller' => 'Users', 'action' => 'login'],
167+
['lang' => 'de', 'controller' => 'Users', 'action' => 'login'],
163168
],
164169
]);
165170

0 commit comments

Comments
 (0)