Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"cakephp/cakephp": "^4.0",
"cakephp/cakephp-codesniffer": "^4.0",
"firebase/php-jwt": "^5.0",
"phpunit/phpunit": "^8.5 || ^9.3"
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/Component/AuthenticationComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class AuthenticationComponent extends Component implements EventDispatcherInterf
* - `requireIdentity` - By default AuthenticationComponent will require an
* identity to be present whenever it is active. You can set the option to
* false to disable that behavior. See allowUnauthenticated() as well.
* - `unauthenticatedMessage` - Error message to use when `UnauthenticatedException` is thrown.
*
* @var array
*/
Expand All @@ -53,6 +54,7 @@ class AuthenticationComponent extends Component implements EventDispatcherInterf
'requireIdentity' => true,
'identityAttribute' => 'identity',
'identityCheckEvent' => 'Controller.startup',
'unauthenticatedMessage' => null,
];

/**
Expand Down Expand Up @@ -172,9 +174,7 @@ protected function doIdentityCheck(): void

$identity = $request->getAttribute($this->getConfig('identityAttribute'));
if (!$identity) {
throw new UnauthenticatedException(
'No identity found. You can skip this check by configuring `requireIdentity` to be `false`.'
);
throw new UnauthenticatedException($this->getConfig('unauthenticatedMessage', ''));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a default message? Something like 'Authentication is required'?

Copy link
Member Author

@ADmad ADmad Jan 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor already sets one:

if (!$message) {
$message = 'Authentication is required to continue';
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job past us.

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,31 @@ public function testIdentityCheckInBeforeFilter()
$component = new AuthenticationComponent($registry);

$this->expectException(UnauthenticatedException::class);
$this->expectExceptionMessage('Authentication is required to continue');
$this->expectExceptionCode(401);

$component->setConfig('identityCheckEvent', 'Controller.initialize');
$component->allowUnauthenticated(['index', 'add']);
$component->beforeFilter();
}

public function testCustomUnauthenticatedMessage()
{
$request = $this->request
->withAttribute('authentication', $this->service);

$controller = new Controller($request, $this->response);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);

$errorMessage = 'You shall not pass!';
$this->expectException(UnauthenticatedException::class);
$this->expectExceptionMessage($errorMessage);
$this->expectExceptionCode(401);

$component->setConfig('identityCheckEvent', 'Controller.initialize');
$component->setConfig('unauthenticatedMessage', $errorMessage);
$component->allowUnauthenticated(['index', 'add']);
$component->beforeFilter();
}
}