Skip to content

Commit 3897b2d

Browse files
committed
Add throttling support to authenticator maker
In the MakeAuthenticator class, a new optional argument 'support-throttling' was added. This allows the user to specify if they want to enable throttling protection during the authentication generation process. If 'support-throttling' is enabled, the LimiterInterface dependency is added and the 'throttling' node is set in the firewall configuration.
1 parent e7ea13d commit 3897b2d

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

src/Maker/MakeAuthenticator.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Symfony\Component\HttpFoundation\RedirectResponse;
3939
use Symfony\Component\HttpFoundation\Request;
4040
use Symfony\Component\HttpFoundation\Response;
41+
use Symfony\Component\RateLimiter\LimiterInterface;
4142
use Symfony\Component\Routing\Attribute\Route;
4243
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
4344
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -209,6 +210,15 @@ function ($answer) {
209210
$supportRememberMeValues[$supportRememberMeType]
210211
);
211212
}
213+
214+
$command->addArgument('support-throttling', InputArgument::OPTIONAL);
215+
$input->setArgument(
216+
'support-throttling',
217+
$io->confirm(
218+
'Do you want to enable the throttling protection?',
219+
true
220+
)
221+
);
212222
}
213223
}
214224

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

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

223234
$this->generateAuthenticatorClass(
224235
$securityData,
@@ -246,7 +257,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
246257
$input->getArgument('authenticator-class'),
247258
$input->hasArgument('logout-setup') ? $input->getArgument('logout-setup') : false,
248259
$supportRememberMe,
249-
$alwaysRememberMe
260+
$alwaysRememberMe,
261+
$supportThrottling,
250262
);
251263
$generator->dumpFile($path, $newYaml);
252264
$securityYamlUpdated = true;
@@ -275,7 +287,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
275287
$input->hasArgument('user-class') ? $input->getArgument('user-class') : null,
276288
$input->hasArgument('logout-setup') ? $input->getArgument('logout-setup') : false,
277289
$supportRememberMe,
278-
$alwaysRememberMe
290+
$alwaysRememberMe,
291+
$supportThrottling
279292
)
280293
);
281294
}
@@ -403,7 +416,7 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam
403416
}
404417

405418
/** @return string[] */
406-
private function generateNextMessage(bool $securityYamlUpdated, string $authenticatorType, string $authenticatorClass, ?string $userClass, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe): array
419+
private function generateNextMessage(bool $securityYamlUpdated, string $authenticatorType, string $authenticatorClass, ?string $userClass, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe, bool $supportThrottling): array
407420
{
408421
$nextTexts = ['Next:'];
409422
$nextTexts[] = '- Customize your new authenticator.';
@@ -416,7 +429,8 @@ private function generateNextMessage(bool $securityYamlUpdated, string $authenti
416429
$authenticatorClass,
417430
$logoutSetup,
418431
$supportRememberMe,
419-
$alwaysRememberMe
432+
$alwaysRememberMe,
433+
$supportThrottling
420434
);
421435
$nextTexts[] = "- Your <info>security.yaml</info> could not be updated automatically. You'll need to add the following config manually:\n\n".$yamlExample;
422436
}
@@ -461,5 +475,13 @@ public function configureDependencies(DependencyBuilder $dependencies, ?InputInt
461475
Yaml::class,
462476
'yaml'
463477
);
478+
479+
$supportThrottling = $input->hasArgument('support-throttling') ? $input->getArgument('support-throttling') : false;
480+
if ($supportThrottling) {
481+
$dependencies->addClassDependency(
482+
LimiterInterface::class,
483+
'symfony/rate-limiter'
484+
);
485+
}
464486
}
465487
}

src/Security/SecurityConfigUpdater.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function updateForUserClass(string $yamlSource, UserClassConfiguration $u
6969
return $contents;
7070
}
7171

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

@@ -145,6 +145,10 @@ public function updateForAuthenticator(string $yamlSource, string $firewallName,
145145
}
146146
}
147147

148+
if ($supportThrottling) {
149+
$firewall['throttling'] = null;
150+
}
151+
148152
$newData['security']['firewalls'][$firewallName] = $firewall;
149153

150154
if (!isset($firewall['logout']) && $logoutSetup) {

tests/Security/SecurityConfigUpdaterTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ public function getUserClassTests(): \Generator
104104
/**
105105
* @dataProvider getAuthenticatorTests
106106
*/
107-
public function testUpdateForAuthenticator(string $firewallName, $entryPoint, string $expectedSourceFilename, string $startingSourceFilename, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe): void
107+
public function testUpdateForAuthenticator(string $firewallName, $entryPoint, string $expectedSourceFilename, string $startingSourceFilename, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe, bool $supportThrottling): void
108108
{
109109
$this->createLogger();
110110

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

116116
$this->assertSame($expectedSource, $actualSource);
@@ -126,6 +126,7 @@ public function getAuthenticatorTests(): \Generator
126126
false,
127127
false,
128128
false,
129+
false,
129130
];
130131

131132
yield 'simple_security' => [
@@ -136,6 +137,7 @@ public function getAuthenticatorTests(): \Generator
136137
false,
137138
false,
138139
false,
140+
false,
139141
];
140142

141143
yield 'simple_security_with_firewalls' => [
@@ -146,6 +148,7 @@ public function getAuthenticatorTests(): \Generator
146148
false,
147149
false,
148150
false,
151+
false,
149152
];
150153

151154
yield 'simple_security_with_firewalls_and_authenticator' => [
@@ -156,6 +159,7 @@ public function getAuthenticatorTests(): \Generator
156159
false,
157160
false,
158161
false,
162+
false,
159163
];
160164

161165
yield 'simple_security_with_firewalls_and_logout' => [
@@ -166,6 +170,7 @@ public function getAuthenticatorTests(): \Generator
166170
true,
167171
false,
168172
false,
173+
false,
169174
];
170175

171176
yield 'security_52_with_multiple_authenticators' => [
@@ -176,6 +181,7 @@ public function getAuthenticatorTests(): \Generator
176181
false,
177182
false,
178183
false,
184+
false,
179185
];
180186

181187
yield 'simple_security_with_firewalls_and_remember_me_checkbox' => [
@@ -186,6 +192,7 @@ public function getAuthenticatorTests(): \Generator
186192
false,
187193
true,
188194
false,
195+
false,
189196
];
190197

191198
yield 'simple_security_with_firewalls_and_always_remember_me' => [
@@ -196,6 +203,18 @@ public function getAuthenticatorTests(): \Generator
196203
false,
197204
true,
198205
true,
206+
false,
207+
];
208+
209+
yield 'simple_security_with_firewalls_always_remember_me_and_throttling' => [
210+
'main',
211+
null,
212+
'simple_security_with_firewalls_always_remember_me_and_throttling.yaml',
213+
'simple_security.yaml',
214+
false,
215+
true,
216+
true,
217+
true,
199218
];
200219
}
201220

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
security:
2+
enable_authenticator_manager: true
3+
4+
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
5+
providers:
6+
in_memory: { memory: ~ }
7+
8+
firewalls:
9+
dev: ~
10+
main:
11+
lazy: true
12+
custom_authenticator: App\Security\AppCustomAuthenticator
13+
14+
remember_me:
15+
secret: '%kernel.secret%'
16+
lifetime: 604800
17+
path: /
18+
always_remember_me: true
19+
throttling: null

0 commit comments

Comments
 (0)