Skip to content

Commit aedb109

Browse files
authored
Merge pull request #780 from cakephp/docs-fixes-4x
Fix documentation issues for 4.x
2 parents 46240e1 + 328c05f commit aedb109

File tree

10 files changed

+101
-100
lines changed

10 files changed

+101
-100
lines changed

docs/en/authenticators.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
182182
// ...
183183
$service->loadAuthenticator('Authentication.Jwt', [
184184
'identifier' => 'Authentication.JwtSubject',
185-
'secretKey' => file_get_contents(CONFIG . '/jwt.key'),
185+
'secretKey' => file_get_contents(CONFIG . 'jwt.key'),
186186
'algorithm' => 'RS256',
187187
'returnPayload' => false
188188
]);
@@ -196,11 +196,11 @@ In your `UsersController`:
196196
``` php
197197
use Firebase\JWT\JWT;
198198

199-
public function login()
199+
public function login(): void
200200
{
201201
$result = $this->Authentication->getResult();
202202
if ($result->isValid()) {
203-
$privateKey = file_get_contents(CONFIG . '/jwt.key');
203+
$privateKey = file_get_contents(CONFIG . 'jwt.key');
204204
$user = $result->getData();
205205
$payload = [
206206
'iss' => 'myapp',
@@ -260,16 +260,19 @@ distribute it via a JWKS endpoint by configuring your app as follows:
260260
``` php
261261
// config/routes.php
262262
$builder->setExtensions('json');
263-
$builder->connect('/.well-known/:controller/*', [
263+
$builder->connect('/.well-known/{controller}', [
264264
'action' => 'index',
265265
], [
266-
'controller' => '(jwks)',
266+
'controller' => 'jwks',
267+
'pass' => [],
267268
]); // connect /.well-known/jwks.json to JwksController
268269

269270
// controller/JwksController.php
271+
use Firebase\JWT\JWT;
272+
270273
public function index()
271274
{
272-
$pubKey = file_get_contents(CONFIG . './jwt.pem');
275+
$pubKey = file_get_contents(CONFIG . 'jwt.pem');
273276
$res = openssl_pkey_get_public($pubKey);
274277
$detail = openssl_pkey_get_details($res);
275278
$key = [
@@ -313,7 +316,7 @@ Configuration options:
313316

314317
- **realm**: Default is `null`
315318
- **qop**: Default is `auth`
316-
- **nonce**: Default is `uniqid(''),`
319+
- **nonce**: Default is `uniqid('')`
317320
- **opaque**: Default is `null`
318321

319322
## Cookie Authenticator aka "Remember Me"
@@ -344,7 +347,7 @@ Configuration options:
344347
- **samesite**: String/null The value for the same site attribute.
345348

346349
The defaults for the various options besides `cookie.name` will be those
347-
set for the `Cake\Http\Cookie\Cookie` class. See [Cookie::setDefaults()](https://api.cakephp.org/4.0/class-Cake.Http.Cookie.Cookie.html#setDefaults)
350+
set for the `Cake\Http\Cookie\Cookie` class. See [Cookie::setDefaults()](https://api.cakephp.org/5/class-Cake.Http.Cookie.Cookie.html#setDefaults)
348351
for the default values.
349352

350353
- **fields**: Array that maps `username` and `password` to the
@@ -369,16 +372,16 @@ Configuration options:
369372
The cookie authenticator can be added to a Form & Session based
370373
authentication system. Cookie authentication will automatically re-login users
371374
after their session expires for as long as the cookie is valid. If a user is
372-
explicity logged out via `AuthenticationComponent::logout()` the
375+
explicitly logged out via `AuthenticationComponent::logout()` the
373376
authentication cookie is **also destroyed**. An example configuration would be:
374377

375378
``` php
376379
// In Application::getAuthenticationService()
377380

378381
// Reuse fields in multiple authenticators.
379382
$fields = [
380-
AbstractIdentifier::CREDENTIAL_USERNAME => 'email',
381-
AbstractIdentifier::CREDENTIAL_PASSWORD => 'password',
383+
PasswordIdentifier::CREDENTIAL_USERNAME => 'email',
384+
PasswordIdentifier::CREDENTIAL_PASSWORD => 'password',
382385
];
383386

384387
// Put form authentication first so that users can re-login via
@@ -389,9 +392,7 @@ $service->loadAuthenticator('Authentication.Form', [
389392
'loginUrl' => '/users/login',
390393
]);
391394
// Then use sessions if they are active.
392-
$service->loadAuthenticator('Authentication.Session', [
393-
'identifier' => 'Authentication.Password',
394-
]);
395+
$service->loadAuthenticator('Authentication.Session');
395396

396397
// If the user is on the login page, check for a cookie as well.
397398
$service->loadAuthenticator('Authentication.Cookie', [
@@ -441,9 +442,6 @@ $service->loadAuthenticator('Authentication.Environment', [
441442
]);
442443
```
443444

444-
::: info Added in version 2.10.0
445-
`EnvironmentAuthenticator` was added.
446-
:::
447445

448446
## Events
449447

@@ -549,9 +547,7 @@ $passwordIdentifier = [
549547
];
550548

551549
// Load the authenticators leaving Basic as the last one.
552-
$service->loadAuthenticator('Authentication.Session', [
553-
'identifier' => $passwordIdentifier,
554-
]);
550+
$service->loadAuthenticator('Authentication.Session');
555551
$service->loadAuthenticator('Authentication.Form', [
556552
'identifier' => $passwordIdentifier,
557553
]);
@@ -589,7 +585,7 @@ Then in your controller's login method you can use `getLoginRedirect()` to get
589585
the redirect target safely from the query string parameter:
590586

591587
``` php
592-
public function login()
588+
public function login(): ?\Cake\Http\Response
593589
{
594590
$result = $this->Authentication->getResult();
595591

@@ -600,8 +596,11 @@ public function login()
600596
if (!$target) {
601597
$target = ['controller' => 'Pages', 'action' => 'display', 'home'];
602598
}
599+
603600
return $this->redirect($target);
604601
}
602+
603+
return null;
605604
}
606605
```
607606

@@ -622,7 +621,7 @@ public function getAuthenticationService(
622621

623622
// Configuration common to both the API and web goes here.
624623

625-
if ($request->getParam('prefix') == 'Api') {
624+
if ($request->getParam('prefix') === 'Api') {
626625
// Include API specific authenticators
627626
} else {
628627
// Web UI specific authenticators.

docs/en/contents.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- [Testing with Authentication](testing)
1313
- [User Impersonation](impersonation)
1414
- [URL Checkers](url-checkers)
15+
- [Redirect Validation](redirect-validation)
1516
- [View Helper](view-helper)
1617
- [Migration from the AuthComponent](migration-from-the-authcomponent)
1718
- [Upgrading from 2.x to 3.x](upgrade-2-to-3)
19+
- [Upgrading from 3.x to 4.x](upgrade-3-to-4)

docs/en/identifiers.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Identifiers
22

3-
Identifiers will identify an user or service based on the information
4-
that was extracted from the request by the authenticators. Identifiers
5-
can take options in the `loadIdentifier` method. A holistic example of
3+
Identifiers will identify a user or service based on the information
4+
that was extracted from the request by the authenticators. A holistic example of
65
using the Password Identifier looks like:
76

87
``` php
@@ -199,15 +198,7 @@ $identifier = [
199198
];
200199
```
201200

202-
Or injected using a setter:
203-
204-
``` php
205-
$resolver = new \App\Identifier\Resolver\CustomResolver();
206-
$identifier = $service->loadIdentifier('Authentication.Password');
207-
$identifier->setResolver($resolver);
208-
```
209-
210-
As of 3.3.0, you should pass the constructed resolver into the identifier:
201+
Or pass the constructed resolver directly into the identifier configuration:
211202

212203
``` php
213204
$resolver = new \App\Identifier\Resolver\CustomResolver();

docs/en/identity-object.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ class User extends Entity implements IdentityInterface
8080
/**
8181
* Authentication\IdentityInterface method
8282
*/
83-
public function getIdentifier()
83+
public function getIdentifier(): array|string|int|null
8484
{
8585
return $this->id;
8686
}
8787

8888
/**
8989
* Authentication\IdentityInterface method
9090
*/
91-
public function getOriginalData()
91+
public function getOriginalData(): \ArrayAccess|array
9292
{
9393
return $this;
9494
}

docs/en/impersonation.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
# User Impersonation
22

3-
::: info Added in version 2.10.0
4-
User impersonation was added.
5-
:::
6-
73
After deploying your application, you may occasionally need to
84
'impersonate' another user in order to debug problems that your customers report
95
or to see the application in the state that your customers are seeing it.
@@ -16,7 +12,7 @@ user from your application's database:
1612

1713
``` php
1814
// In a controller
19-
public function impersonate()
15+
public function impersonate(): \Cake\Http\Response
2016
{
2117
$this->request->allowMethod(['POST']);
2218

@@ -29,9 +25,9 @@ public function impersonate()
2925
}
3026

3127
// Fetch the user we want to impersonate.
32-
$targetUser = $this->Users->findById(
33-
$this->request->getData('user_id')
34-
)->firstOrFail();
28+
$targetUser = $this->fetchTable('Users')
29+
->findById($this->request->getData('user_id'))
30+
->firstOrFail();
3531

3632
// Enable impersonation.
3733
$this->Authentication->impersonate($targetUser);
@@ -50,7 +46,7 @@ back to your previous identity using `AuthenticationComponent`:
5046

5147
``` php
5248
// In a controller
53-
public function revertIdentity()
49+
public function revertIdentity(): \Cake\Http\Response
5450
{
5551
$this->request->allowMethod(['POST']);
5652

@@ -59,6 +55,8 @@ public function revertIdentity()
5955
throw new NotFoundException();
6056
}
6157
$this->Authentication->stopImpersonating();
58+
59+
return $this->redirect($this->referer());
6260
}
6361
```
6462

@@ -68,4 +66,4 @@ There are a few limitations to impersonation.
6866

6967
1. Your application must be using the `Session` authenticator.
7068
2. You cannot impersonate another user while impersonation is active. Instead
71-
you must `stopImpersonation()` and then start it again.
69+
you must `stopImpersonating()` and then start it again.

docs/en/index.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ imports:
2626
use Authentication\AuthenticationService;
2727
use Authentication\AuthenticationServiceInterface;
2828
use Authentication\AuthenticationServiceProviderInterface;
29-
use Authentication\Identifier\AbstractIdentifier;
29+
use Authentication\Identifier\PasswordIdentifier;
3030
use Authentication\Middleware\AuthenticationMiddleware;
3131
use Cake\Http\MiddlewareQueue;
3232
use Cake\Routing\Router;
@@ -93,8 +93,8 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
9393
]);
9494

9595
$fields = [
96-
AbstractIdentifier::CREDENTIAL_USERNAME => 'email',
97-
AbstractIdentifier::CREDENTIAL_PASSWORD => 'password',
96+
PasswordIdentifier::CREDENTIAL_USERNAME => 'email',
97+
PasswordIdentifier::CREDENTIAL_PASSWORD => 'password',
9898
];
9999

100100
// Load the authenticators. Session should be first.
@@ -109,7 +109,7 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
109109
'fields' => $fields,
110110
'loginUrl' => Router::url([
111111
'prefix' => false,
112-
'plugin' => null,
112+
'plugin' => false,
113113
'controller' => 'Users',
114114
'action' => 'login',
115115
]),
@@ -135,7 +135,7 @@ Next, in your `AppController` load the [Authentication Component](authentication
135135

136136
``` php
137137
// in src/Controller/AppController.php
138-
public function initialize()
138+
public function initialize(): void
139139
{
140140
parent::initialize();
141141

@@ -156,7 +156,7 @@ $this->Authentication->allowUnauthenticated(['view', 'index']);
156156
## Building a Login Action
157157

158158
Once you have the middleware applied to your application you'll need a way for
159-
users to login. Please ensure your database has been created with the Users table structure used in [tutorial](tutorials-and-examples/cms/database). First generate a Users model and controller with bake:
159+
users to login. Please ensure your database has been created with the Users table structure used in the [CMS tutorial](https://book.cakephp.org/5/en/tutorials-and-examples/cms/database.html). First generate a Users model and controller with bake:
160160

161161
``` bash
162162
bin/cake bake model Users
@@ -168,17 +168,20 @@ like:
168168

169169
``` php
170170
// in src/Controller/UsersController.php
171-
public function login()
171+
public function login(): ?\Cake\Http\Response
172172
{
173173
$result = $this->Authentication->getResult();
174174
// If the user is logged in send them away.
175175
if ($result && $result->isValid()) {
176176
$target = $this->Authentication->getLoginRedirect() ?? '/home';
177+
177178
return $this->redirect($target);
178179
}
179180
if ($this->request->is('post')) {
180181
$this->Flash->error('Invalid username or password');
181182
}
183+
184+
return null;
182185
}
183186
```
184187

@@ -188,7 +191,7 @@ unauthenticated users are able to access it:
188191

189192
``` php
190193
// in src/Controller/UsersController.php
191-
public function beforeFilter(\Cake\Event\EventInterface $event)
194+
public function beforeFilter(\Cake\Event\EventInterface $event): void
192195
{
193196
parent::beforeFilter($event);
194197

@@ -216,9 +219,10 @@ Then add a simple logout action:
216219

217220
``` php
218221
// in src/Controller/UsersController.php
219-
public function logout()
222+
public function logout(): \Cake\Http\Response
220223
{
221224
$this->Authentication->logout();
225+
222226
return $this->redirect(['controller' => 'Users', 'action' => 'login']);
223227
}
224228
```
@@ -240,9 +244,10 @@ class User extends Entity
240244
// ... other methods
241245

242246
// Automatically hash passwords when they are changed.
243-
protected function _setPassword(string $password)
247+
protected function _setPassword(string $password): string
244248
{
245249
$hasher = new DefaultPasswordHasher();
250+
246251
return $hasher->hash($password);
247252
}
248253
}

docs/en/middleware.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
3737
$path = $request->getPath();
3838

3939
$service = new AuthenticationService();
40-
if (strpos($path, '/api') === 0) {
40+
if (str_starts_with($path, '/api')) {
4141
// Accept API tokens only
4242
$service->loadAuthenticator('Authentication.Token', [
4343
'identifier' => 'Authentication.Token',
@@ -48,9 +48,7 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
4848

4949
// Web authentication
5050
// Support sessions and form login.
51-
$service->loadAuthenticator('Authentication.Session', [
52-
'identifier' => 'Authentication.Password',
53-
]);
51+
$service->loadAuthenticator('Authentication.Session');
5452
$service->loadAuthenticator('Authentication.Form', [
5553
'identifier' => 'Authentication.Password',
5654
]);

0 commit comments

Comments
 (0)