Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two Factor refactoring. Introduce TwoFactorProcessors. #1072

Draft
wants to merge 1 commit into
base: 11.next-cake4
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"require": {
"php": ">=7.4.0",
"cakephp/cakephp": "^4.5",
"cakedc/auth": "^7.0",
"cakedc/auth": "^7.3",
"cakephp/authorization": "^2.0.0",
"cakephp/authentication": "^2.0.0"
},
Expand Down
19 changes: 6 additions & 13 deletions src/Loader/AuthenticationServiceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Cake\Core\Configure;
use CakeDC\Auth\Authentication\AuthenticationService;
use CakeDC\Users\Plugin;
use CakeDC\Auth\Authentication\TwoFactorProcessorLoader;
use Psr\Http\Message\ServerRequestInterface;

/**
Expand All @@ -34,10 +34,11 @@ class AuthenticationServiceLoader
*/
public function __invoke(ServerRequestInterface $request)
{
$service = new AuthenticationService();
$processors = TwoFactorProcessorLoader::processors();
$service = new AuthenticationService(['processors' => $processors]);
$this->loadIdentifiers($service);
$this->loadAuthenticators($service);
$this->loadTwoFactorAuthenticator($service);
$this->loadTwoFactorAuthenticator($service, $processors);

return $service;
}
Expand Down Expand Up @@ -81,17 +82,9 @@ protected function loadAuthenticators($service)
* @param \CakeDC\Auth\Authentication\AuthenticationService $service Authentication service to load identifiers
* @return void
*/
protected function loadTwoFactorAuthenticator($service)
protected function loadTwoFactorAuthenticator($service, $processors)
{
$u2fEnabled = Configure::read('U2f.enabled') !== false;
if ($u2fEnabled) {
trigger_error(Plugin::DEPRECATED_MESSAGE_U2F, E_USER_DEPRECATED);
}
if (
Configure::read('OneTimePasswordAuthenticator.login') !== false
|| Configure::read('Webauthn2fa.enabled') !== false
|| $u2fEnabled
) {
if (collection($processors)->some(fn ($processor) => $processor->enabled())) {
$service->loadAuthenticator('CakeDC/Auth.TwoFactor', [
'skipTwoFactorVerify' => true,
]);
Expand Down
14 changes: 3 additions & 11 deletions src/Loader/MiddlewareQueueLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
use Authorization\Middleware\RequestAuthorizationMiddleware;
use Cake\Core\Configure;
use Cake\Http\MiddlewareQueue;
use CakeDC\Auth\Authentication\TwoFactorProcessorLoader;
use CakeDC\Auth\Middleware\TwoFactorMiddleware;
use CakeDC\Users\Middleware\SocialAuthMiddleware;
use CakeDC\Users\Middleware\SocialEmailMiddleware;
use CakeDC\Users\Plugin;

/**
* Class MiddlewareQueueLoader
Expand Down Expand Up @@ -96,16 +96,8 @@ protected function loadAuthenticationMiddleware(
*/
protected function load2faMiddleware(MiddlewareQueue $middlewareQueue)
{
$u2fEnabled = Configure::read('U2f.enabled') !== false;
if ($u2fEnabled) {
trigger_error(Plugin::DEPRECATED_MESSAGE_U2F, E_USER_DEPRECATED);
}

if (
Configure::read('OneTimePasswordAuthenticator.login') !== false
|| Configure::read('Webauthn2fa.enabled') !== false
|| $u2fEnabled
) {
$processors = TwoFactorProcessorLoader::processors();
if (collection($processors)->some(fn ($processor) => $processor->enabled())) {
$middlewareQueue->add(TwoFactorMiddleware::class);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ public function testLoginPostRequestRightPasswordIsEnabledU2f()
{
EventManager::instance()->on('TestApp.afterPluginBootstrap', function () {
Configure::write(['U2f.enabled' => true]);
Configure::write([
'TwoFactorProcessors' => [
\CakeDC\Auth\Authentication\TwoFactorProcessor\U2FProcessor::class,
],
]);
});
$this->enableRetainFlashMessages();
$this->post('/login', [
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function testMiddleware()
{
Configure::write('Users.Social.login', true);
Configure::write('OneTimePasswordAuthenticator.login', true);
Configure::write('TwoFactorProcessors', [
\CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class,
]);
Configure::write('Auth.Authorization.enable', true);

$plugin = new Plugin();
Expand Down Expand Up @@ -75,6 +78,9 @@ public function testMiddlewareAuthorizationMiddlewareAndRbacMiddleware()
Configure::write('Users.Social.login', true);
Configure::write('OneTimePasswordAuthenticator.login', true);
Configure::write('Auth.Authorization.enable', true);
Configure::write('TwoFactorProcessors', [
\CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class,
]);

$plugin = new Plugin();

Expand Down Expand Up @@ -106,6 +112,9 @@ public function testMiddlewareWithoutAuhorization()
Configure::write('Users.Social.login', true);
Configure::write('OneTimePasswordAuthenticator.login', true);
Configure::write('Auth.Authorization.enable', false);
Configure::write('TwoFactorProcessors', [
\CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class,
]);

$plugin = new Plugin();

Expand Down Expand Up @@ -133,6 +142,10 @@ public function testMiddlewareNotSocial()
Configure::write('Users.Social.login', false);
Configure::write('OneTimePasswordAuthenticator.login', true);
Configure::write('Auth.Authorization.enable', true);
Configure::write('TwoFactorProcessors', [
\CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class,
]);

$plugin = new Plugin();

$middleware = new MiddlewareQueue();
Expand All @@ -158,6 +171,9 @@ public function testMiddlewareNotOneTimePasswordAuthenticator()
Configure::write('Users.Social.login', true);
Configure::write('OneTimePasswordAuthenticator.login', false);
Configure::write('Auth.Authorization.enable', true);
Configure::write('TwoFactorProcessors', [
\CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class,
]);
$plugin = new Plugin();

$middleware = new MiddlewareQueue();
Expand Down Expand Up @@ -185,6 +201,9 @@ public function testMiddlewareNotGoogleAuthenticationAndNotSocial()
Configure::write('Users.Social.login', false);
Configure::write('OneTimePasswordAuthenticator.login', false);
Configure::write('Auth.Authorization.enable', true);
Configure::write('TwoFactorProcessors', [
\CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class,
]);
$plugin = new Plugin();

$middleware = new MiddlewareQueue();
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase/Provider/AuthenticationServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public function testGetAuthenticationService()
'Authentication.JwtSubject',
]);
Configure::write('OneTimePasswordAuthenticator.login', true);
Configure::write('TwoFactorProcessors', [
\CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class,
]);

$authenticationServiceProvider = new AuthenticationServiceProvider();
$service = $authenticationServiceProvider->getAuthenticationService(new ServerRequest(), new Response());
Expand Down Expand Up @@ -215,6 +218,7 @@ public function testGetAuthenticationServiceWithoutOneTimePasswordAuthenticator(
'Authentication.JwtSubject',
]);
Configure::write('OneTimePasswordAuthenticator.login', false);
Configure::write('TwoFactorProcessors', []);

$authenticationServiceProvider = new AuthenticationServiceProvider();
$service = $authenticationServiceProvider->getAuthenticationService(new ServerRequest(), new Response());
Expand Down
5 changes: 5 additions & 0 deletions tests/test_app/config/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
'OAuth.providers.twitter.options.clientSecret' => '999988899',
'OAuth.providers.google.options.clientId' => '0000000990909090',
'OAuth.providers.google.options.clientSecret' => '1565464559789798',
'TwoFactorProcessors' => [
\CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class,
\CakeDC\Auth\Authentication\TwoFactorProcessor\U2FProcessor::class,
\CakeDC\Auth\Authentication\TwoFactorProcessor\Webauthn2faProcessor::class,
],
];
Loading