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

[make:security] Add throttling support to authenticator maker #1524

Closed
wants to merge 1 commit into from
Closed
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 @@ -32,6 +32,7 @@
"doctrine/orm": "^2.15|^3",
"symfony/http-client": "^6.4|^7.0",
"symfony/phpunit-bridge": "^6.4.1|^7.0",
"symfony/rate-limiter": "^6.4|^7.0",
"symfony/security-core": "^6.4|^7.0",
"symfony/yaml": "^6.4|^7.0",
"twig/twig": "^3.0|^4.x-dev"
Expand Down
30 changes: 26 additions & 4 deletions src/Maker/MakeAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
Expand Down Expand Up @@ -209,6 +210,15 @@ function ($answer) {
$supportRememberMeValues[$supportRememberMeType]
);
}

$command->addArgument('support-throttling', InputArgument::OPTIONAL);
Copy link
Collaborator

Choose a reason for hiding this comment

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

MakeAuthenticator is being deprecated - we are not adding any new features to it.

$input->setArgument(
'support-throttling',
$io->confirm(
'Do you want to enable the throttling protection?',
true
)
);
}
}

Expand All @@ -219,6 +229,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

$supportRememberMe = $input->hasArgument('support-remember-me') ? $input->getArgument('support-remember-me') : false;
$alwaysRememberMe = $input->hasArgument('always-remember-me') && self::REMEMBER_ME_TYPE_ALWAYS === $input->getArgument('always-remember-me');
$supportThrottling = $input->hasArgument('support-throttling') ? $input->getArgument('support-throttling') : false;

$this->generateAuthenticatorClass(
$securityData,
Expand Down Expand Up @@ -246,7 +257,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$input->getArgument('authenticator-class'),
$input->hasArgument('logout-setup') ? $input->getArgument('logout-setup') : false,
$supportRememberMe,
$alwaysRememberMe
$alwaysRememberMe,
$supportThrottling,
);
$generator->dumpFile($path, $newYaml);
$securityYamlUpdated = true;
Expand Down Expand Up @@ -275,7 +287,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$input->hasArgument('user-class') ? $input->getArgument('user-class') : null,
$input->hasArgument('logout-setup') ? $input->getArgument('logout-setup') : false,
$supportRememberMe,
$alwaysRememberMe
$alwaysRememberMe,
$supportThrottling
)
);
}
Expand Down Expand Up @@ -403,7 +416,7 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam
}

/** @return string[] */
private function generateNextMessage(bool $securityYamlUpdated, string $authenticatorType, string $authenticatorClass, ?string $userClass, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe): array
private function generateNextMessage(bool $securityYamlUpdated, string $authenticatorType, string $authenticatorClass, ?string $userClass, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe, bool $supportThrottling): array
{
$nextTexts = ['Next:'];
$nextTexts[] = '- Customize your new authenticator.';
Expand All @@ -416,7 +429,8 @@ private function generateNextMessage(bool $securityYamlUpdated, string $authenti
$authenticatorClass,
$logoutSetup,
$supportRememberMe,
$alwaysRememberMe
$alwaysRememberMe,
$supportThrottling
);
$nextTexts[] = "- Your <info>security.yaml</info> could not be updated automatically. You'll need to add the following config manually:\n\n".$yamlExample;
}
Expand Down Expand Up @@ -461,5 +475,13 @@ public function configureDependencies(DependencyBuilder $dependencies, ?InputInt
Yaml::class,
'yaml'
);

$supportThrottling = $input?->hasArgument('support-throttling') ? $input->getArgument('support-throttling') : false;
if ($supportThrottling) {
$dependencies->addClassDependency(
LimiterInterface::class,
'symfony/rate-limiter'
);
}
}
}
6 changes: 5 additions & 1 deletion src/Security/SecurityConfigUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function updateForUserClass(string $yamlSource, UserClassConfiguration $u
return $contents;
}

public function updateForAuthenticator(string $yamlSource, string $firewallName, $chosenEntryPoint, string $authenticatorClass, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe): string
public function updateForAuthenticator(string $yamlSource, string $firewallName, $chosenEntryPoint, string $authenticatorClass, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe, bool $supportThrottling): string
{
$this->createYamlSourceManipulator($yamlSource);

Expand Down Expand Up @@ -145,6 +145,10 @@ public function updateForAuthenticator(string $yamlSource, string $firewallName,
}
}

if ($supportThrottling) {
$firewall['throttling'] = null;
}

$newData['security']['firewalls'][$firewallName] = $firewall;

if (!isset($firewall['logout']) && $logoutSetup) {
Expand Down
23 changes: 21 additions & 2 deletions tests/Security/SecurityConfigUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ public function getUserClassTests(): \Generator
/**
* @dataProvider getAuthenticatorTests
*/
public function testUpdateForAuthenticator(string $firewallName, $entryPoint, string $expectedSourceFilename, string $startingSourceFilename, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe): void
public function testUpdateForAuthenticator(string $firewallName, $entryPoint, string $expectedSourceFilename, string $startingSourceFilename, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe, bool $supportThrottling): void
{
$this->createLogger();

$updater = new SecurityConfigUpdater($this->ysmLogger);
$source = file_get_contents(__DIR__.'/yaml_fixtures/source/'.$startingSourceFilename);
$actualSource = $updater->updateForAuthenticator($source, $firewallName, $entryPoint, 'App\\Security\\AppCustomAuthenticator', $logoutSetup, $supportRememberMe, $alwaysRememberMe);
$actualSource = $updater->updateForAuthenticator($source, $firewallName, $entryPoint, 'App\\Security\\AppCustomAuthenticator', $logoutSetup, $supportRememberMe, $alwaysRememberMe, $supportThrottling);
$expectedSource = file_get_contents(__DIR__.'/yaml_fixtures/expected_authenticator/'.$expectedSourceFilename);

$this->assertSame($expectedSource, $actualSource);
Expand All @@ -126,6 +126,7 @@ public function getAuthenticatorTests(): \Generator
false,
false,
false,
false,
];

yield 'simple_security' => [
Expand All @@ -136,6 +137,7 @@ public function getAuthenticatorTests(): \Generator
false,
false,
false,
false,
];

yield 'simple_security_with_firewalls' => [
Expand All @@ -146,6 +148,7 @@ public function getAuthenticatorTests(): \Generator
false,
false,
false,
false,
];

yield 'simple_security_with_firewalls_and_authenticator' => [
Expand All @@ -156,6 +159,7 @@ public function getAuthenticatorTests(): \Generator
false,
false,
false,
false,
];

yield 'simple_security_with_firewalls_and_logout' => [
Expand All @@ -166,6 +170,7 @@ public function getAuthenticatorTests(): \Generator
true,
false,
false,
false,
];

yield 'security_52_with_multiple_authenticators' => [
Expand All @@ -176,6 +181,7 @@ public function getAuthenticatorTests(): \Generator
false,
false,
false,
false,
];

yield 'simple_security_with_firewalls_and_remember_me_checkbox' => [
Expand All @@ -186,6 +192,7 @@ public function getAuthenticatorTests(): \Generator
false,
true,
false,
false,
];

yield 'simple_security_with_firewalls_and_always_remember_me' => [
Expand All @@ -196,6 +203,18 @@ public function getAuthenticatorTests(): \Generator
false,
true,
true,
false,
];

yield 'simple_security_with_firewalls_always_remember_me_and_throttling' => [
'main',
null,
'simple_security_with_firewalls_always_remember_me_and_throttling.yaml',
'simple_security.yaml',
false,
true,
true,
true,
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
security:
enable_authenticator_manager: true

# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
in_memory: { memory: ~ }

firewalls:
dev: ~
main:
lazy: true
custom_authenticator: App\Security\AppCustomAuthenticator

remember_me:
secret: '%kernel.secret%'
lifetime: 604800
path: /
always_remember_me: true
throttling: null
Loading