Skip to content

Commit 4325675

Browse files
author
mscherer
committed
Fix up Url Checker defaulting.
1 parent d5ee52b commit 4325675

13 files changed

Lines changed: 412 additions & 358 deletions

docs/en/upgrade-3-to-4.rst

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,20 @@ For LDAP authentication:
105105
LdapIdentifier::CREDENTIAL_PASSWORD => 'password',
106106
];
107107
108-
URL Checker Renamed
109-
-------------------
108+
URL Checker Renamed and Restructured
109+
-------------------------------------
110110

111-
``CakeRouterUrlChecker`` has been renamed to ``CakeUrlChecker`` and now accepts
112-
both string and array URLs (just like ``Router::url()``).
111+
URL checkers have been completely restructured:
112+
113+
- ``CakeRouterUrlChecker`` has been renamed to ``DefaultUrlChecker``
114+
- The old ``DefaultUrlChecker`` (framework-agnostic) has been renamed to ``GenericUrlChecker``
115+
- Auto-detection has been removed - ``DefaultUrlChecker`` is now hardcoded
113116

114117
**Before (3.x):**
115118

116119
.. code-block:: php
117120
121+
// Using CakeRouterUrlChecker explicitly
118122
$service->loadAuthenticator('Authentication.Form', [
119123
'urlChecker' => 'Authentication.CakeRouter',
120124
'loginUrl' => [
@@ -123,25 +127,33 @@ both string and array URLs (just like ``Router::url()``).
123127
],
124128
]);
125129
130+
// Using DefaultUrlChecker explicitly (framework-agnostic)
131+
$service->loadAuthenticator('Authentication.Form', [
132+
'urlChecker' => 'Authentication.Default',
133+
'loginUrl' => '/users/login',
134+
]);
135+
136+
// Auto-detection (picks CakeRouter if available, otherwise Default)
137+
$service->loadAuthenticator('Authentication.Form', [
138+
'loginUrl' => '/users/login',
139+
]);
140+
126141
**After (4.x):**
127142

128143
.. code-block:: php
129144
130-
// CakeUrlChecker is now the default when CakePHP is installed
145+
// DefaultUrlChecker is now hardcoded (formerly CakeRouterUrlChecker)
131146
$service->loadAuthenticator('Authentication.Form', [
132147
'loginUrl' => [
133148
'controller' => 'Users',
134149
'action' => 'login',
135150
],
136151
]);
137152
138-
// Or explicitly:
153+
// For framework-agnostic projects, explicitly use GenericUrlChecker
139154
$service->loadAuthenticator('Authentication.Form', [
140-
'urlChecker' => 'Authentication.Cake',
141-
'loginUrl' => [
142-
'controller' => 'Users',
143-
'action' => 'login',
144-
],
155+
'urlChecker' => 'Authentication.Generic',
156+
'loginUrl' => '/users/login',
145157
]);
146158
147159
Simplified URL Checker API
@@ -189,31 +201,38 @@ Single URLs work the same in both versions:
189201
'loginUrl' => ['controller' => 'Users', 'action' => 'login'],
190202
]);
191203
192-
Auto-Detection Changes
204+
Auto-Detection Removed
193205
----------------------
194206

195207
URL Checkers
196208
^^^^^^^^^^^^
197209

198-
- When CakePHP Router is available: defaults to ``CakeUrlChecker``
199-
- Without CakePHP: defaults to ``DefaultUrlChecker``
200-
- For multiple URLs: you **must** explicitly configure ``MultiUrlChecker``
210+
**Important:** Auto-detection has been removed. ``DefaultUrlChecker`` is now hardcoded
211+
and assumes CakePHP is available.
212+
213+
- **4.x default:** Always uses ``DefaultUrlChecker`` (formerly ``CakeUrlChecker``)
214+
- **Framework-agnostic:** Must explicitly configure ``GenericUrlChecker``
215+
- **Multiple URLs:** Must explicitly configure ``MultiUrlChecker``
201216

202-
DefaultUrlChecker Changes
203-
^^^^^^^^^^^^^^^^^^^^^^^^^
217+
DefaultUrlChecker is Now CakePHP-Based
218+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
204219

205-
``DefaultUrlChecker`` no longer accepts array-based URLs. It throws a
206-
``RuntimeException`` if an array URL is provided:
220+
``DefaultUrlChecker`` is now the CakePHP checker (formerly ``CakeRouterUrlChecker``).
221+
It requires CakePHP Router and supports both string and array URLs.
222+
223+
The 3.x framework-agnostic ``DefaultUrlChecker`` has been renamed to ``GenericUrlChecker``.
207224

208225
.. code-block:: php
209226
210-
// This will throw an exception in 4.x
227+
// DefaultUrlChecker now requires CakePHP Router
211228
$checker = new DefaultUrlChecker();
212-
$checker->check($request, ['controller' => 'Users', 'action' => 'login']);
229+
$checker->check($request, ['controller' => 'Users', 'action' => 'login']); // Works
230+
$checker->check($request, '/users/login'); // Also works
213231
214-
// Use CakeUrlChecker instead:
215-
$checker = new CakeUrlChecker();
216-
$checker->check($request, ['controller' => 'Users', 'action' => 'login']);
232+
// For framework-agnostic usage:
233+
$checker = new GenericUrlChecker();
234+
$checker->check($request, '/users/login'); // Works
235+
$checker->check($request, ['controller' => 'Users']); // Throws exception
217236
218237
New Features
219238
============
@@ -264,20 +283,33 @@ Migration Tips
264283

265284
- ``AbstractIdentifier::CREDENTIAL_`` → ``PasswordIdentifier::CREDENTIAL_``
266285
- ``IdentifierCollection`` → ``IdentifierFactory``
267-
- ``'Authentication.CakeRouter'`` → ``'Authentication.Cake'``
268-
- ``CakeRouterUrlChecker`` → ``CakeUrlChecker``
286+
- ``'Authentication.CakeRouter'`` → Remove (no longer needed, default is now CakePHP-based)
287+
- ``CakeRouterUrlChecker`` → ``DefaultUrlChecker``
288+
- Old 3.x ``DefaultUrlChecker`` (framework-agnostic) → ``GenericUrlChecker``
289+
290+
2. **Framework-Agnostic Projects**:
291+
292+
If you're using this library without CakePHP, you **must** explicitly configure
293+
``GenericUrlChecker``:
294+
295+
.. code-block:: php
296+
297+
$service->loadAuthenticator('Authentication.Form', [
298+
'urlChecker' => 'Authentication.Generic',
299+
'loginUrl' => '/users/login',
300+
]);
269301
270-
2. **Multiple Login URLs**:
302+
3. **Multiple Login URLs**:
271303

272304
If you have multiple login URLs, add ``'urlChecker' => 'Authentication.Multi'``
273305
to your authenticator configuration.
274306

275-
3. **Custom Identifier Setup**:
307+
4. **Custom Identifier Setup**:
276308

277309
If you were passing ``IdentifierCollection`` to authenticators, switch to
278310
either passing a single identifier or null (to use defaults).
279311

280-
4. **Test Thoroughly**:
312+
5. **Test Thoroughly**:
281313

282314
The changes to identifier management and URL checking are significant.
283315
Test all authentication flows after upgrading.

docs/en/url-checkers.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ For multiple login URLs, use ``MultiUrlChecker``.
1111
Included Checkers
1212
=================
1313

14-
CakeUrlChecker
15-
--------------
14+
DefaultUrlChecker
15+
-----------------
1616

17-
The default checker when CakePHP is installed. Supports both string URLs and
18-
CakePHP's array-based routing notation. This checker also works with named routes.
17+
The default URL checker. Supports both string URLs and CakePHP's array-based
18+
routing notation. Uses CakePHP Router and works with named routes.
1919

2020
Single URL (string):
2121

@@ -41,18 +41,18 @@ Single URL (CakePHP route array):
4141
Options:
4242

4343
- **checkFullUrl**: To compare the full URL, including protocol, host
44-
and port or not. Default is ``false``
44+
and port or not. Default is ``false``.
4545

46-
DefaultUrlChecker
47-
-----------------
46+
GenericUrlChecker
47+
------------------
4848

4949
Framework-agnostic checker for string URLs. Supports regex matching.
50-
This is the default when CakePHP is not installed.
50+
Use this for non-CakePHP projects.
5151

5252
.. code-block:: php
5353
5454
$service->loadAuthenticator('Authentication.Form', [
55-
'urlChecker' => 'Authentication.Default',
55+
'urlChecker' => 'Authentication.Generic',
5656
'loginUrl' => '/users/login',
5757
]);
5858
@@ -62,7 +62,7 @@ Using regex:
6262
6363
$service->loadAuthenticator('Authentication.Form', [
6464
'urlChecker' => [
65-
'className' => 'Authentication.Default',
65+
'className' => 'Authentication.Generic',
6666
'useRegex' => true,
6767
],
6868
'loginUrl' => '%^/[a-z]{2}/users/login/?$%',

src/Authenticator/FormAuthenticator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class FormAuthenticator extends AbstractAuthenticator
4242
*/
4343
protected array $_defaultConfig = [
4444
'loginUrl' => null,
45-
'urlChecker' => 'Authentication.Cake',
45+
'urlChecker' => null,
4646
'fields' => [
4747
PasswordIdentifier::CREDENTIAL_USERNAME => 'username',
4848
PasswordIdentifier::CREDENTIAL_PASSWORD => 'password',

src/UrlChecker/CakeUrlChecker.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/UrlChecker/DefaultUrlChecker.php

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,22 @@
1616
*/
1717
namespace Authentication\UrlChecker;
1818

19+
use Cake\Routing\Router;
1920
use Psr\Http\Message\ServerRequestInterface;
20-
use RuntimeException;
2121

2222
/**
23-
* Checks if a request object contains a valid URL. Framework agnostic.
23+
* Default URL checker for CakePHP applications. Uses CakePHP Router.
2424
*/
2525
class DefaultUrlChecker implements UrlCheckerInterface
2626
{
2727
/**
2828
* Default Options
2929
*
30-
* - `urlChecker` Whether to use `loginUrl` as regular expression(s).
3130
* - `checkFullUrl` Whether to check the full request URI.
3231
*
3332
* @var array<string, mixed>
3433
*/
3534
protected array $_defaultOptions = [
36-
'useRegex' => false,
3735
'checkFullUrl' => false,
3836
];
3937

@@ -42,52 +40,26 @@ class DefaultUrlChecker implements UrlCheckerInterface
4240
*/
4341
public function check(ServerRequestInterface $request, array|string $loginUrls, array $options = []): bool
4442
{
45-
if (is_array($loginUrls)) {
46-
throw new RuntimeException(
47-
'Array-based login URLs require CakePHP Router and CakeUrlChecker. ' .
48-
'Either install cakephp/cakephp or use string URLs instead.',
49-
);
50-
}
51-
5243
$options = $this->_mergeDefaultOptions($options);
53-
$checker = $this->_getChecker($options);
5444
$url = $this->_getUrlFromRequest($request, $options['checkFullUrl']);
5545

56-
return (bool)$checker($loginUrls, $url);
46+
// Support both string URLs and array-based routes (like Router::url())
47+
$validUrl = Router::url($loginUrls, $options['checkFullUrl']);
48+
49+
return $validUrl === $url;
5750
}
5851

5952
/**
6053
* Merges given options with the defaults.
6154
*
62-
* The reason this method exists is that it makes it easy to override the
63-
* method and inject additional options without the need to use the
64-
* MergeVarsTrait.
65-
*
6655
* @param array<string, mixed> $options Options to merge in
67-
* @return array
56+
* @return array<string, mixed>
6857
*/
6958
protected function _mergeDefaultOptions(array $options): array
7059
{
7160
return $options + $this->_defaultOptions;
7261
}
7362

74-
/**
75-
* Gets the checker function name or a callback
76-
*
77-
* @param array<string, mixed> $options Array of options
78-
* @return callable
79-
*/
80-
protected function _getChecker(array $options): callable
81-
{
82-
if (!empty($options['useRegex'])) {
83-
return 'preg_match';
84-
}
85-
86-
return function ($validUrl, $url) {
87-
return $validUrl === $url;
88-
};
89-
}
90-
9163
/**
9264
* Returns current url.
9365
*

0 commit comments

Comments
 (0)