Skip to content

Commit 61c51d8

Browse files
committed
[FrameworkBundle] Deprecated the registerRateLimiter method
1 parent ad254ad commit 61c51d8

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

DependencyInjection/Security/Factory/LoginThrottlingFactory.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory;
1313

14-
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
1514
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1615
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
1716
use Symfony\Component\DependencyInjection\ChildDefinition;
1817
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\Exception\LogicException;
1919
use Symfony\Component\DependencyInjection\Reference;
2020
use Symfony\Component\HttpFoundation\RateLimiter\RequestRateLimiterInterface;
21+
use Symfony\Component\Lock\LockInterface;
2122
use Symfony\Component\RateLimiter\RateLimiterFactory;
23+
use Symfony\Component\RateLimiter\Storage\CacheStorage;
2224
use Symfony\Component\Security\Http\RateLimiter\DefaultLoginRateLimiter;
2325

2426
/**
@@ -60,20 +62,16 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
6062
}
6163

6264
if (!isset($config['limiter'])) {
63-
if (!class_exists(FrameworkExtension::class) || !method_exists(FrameworkExtension::class, 'registerRateLimiter')) {
64-
throw new \LogicException('You must either configure a rate limiter for "security.firewalls.'.$firewallName.'.login_throttling" or install symfony/framework-bundle:^5.2.');
65-
}
66-
6765
$limiterOptions = [
6866
'policy' => 'fixed_window',
6967
'limit' => $config['max_attempts'],
7068
'interval' => $config['interval'],
7169
'lock_factory' => $config['lock_factory'],
7270
];
73-
FrameworkExtension::registerRateLimiter($container, $localId = '_login_local_'.$firewallName, $limiterOptions);
71+
$this->registerRateLimiter($container, $localId = '_login_local_'.$firewallName, $limiterOptions);
7472

7573
$limiterOptions['limit'] = 5 * $config['max_attempts'];
76-
FrameworkExtension::registerRateLimiter($container, $globalId = '_login_global_'.$firewallName, $limiterOptions);
74+
$this->registerRateLimiter($container, $globalId = '_login_global_'.$firewallName, $limiterOptions);
7775

7876
$container->register($config['limiter'] = 'security.login_throttling.'.$firewallName.'.limiter', DefaultLoginRateLimiter::class)
7977
->addArgument(new Reference('limiter.'.$globalId))
@@ -88,4 +86,33 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
8886

8987
return [];
9088
}
89+
90+
private function registerRateLimiter(ContainerBuilder $container, string $name, array $limiterConfig)
91+
{
92+
// default configuration (when used by other DI extensions)
93+
$limiterConfig += ['lock_factory' => 'lock.factory', 'cache_pool' => 'cache.rate_limiter'];
94+
95+
$limiter = $container->setDefinition($limiterId = 'limiter.'.$name, new ChildDefinition('limiter'));
96+
97+
if (null !== $limiterConfig['lock_factory']) {
98+
if (!interface_exists(LockInterface::class)) {
99+
throw new LogicException(sprintf('Rate limiter "%s" requires the Lock component to be installed. Try running "composer require symfony/lock".', $name));
100+
}
101+
102+
$limiter->replaceArgument(2, new Reference($limiterConfig['lock_factory']));
103+
}
104+
unset($limiterConfig['lock_factory']);
105+
106+
if (null === $storageId = $limiterConfig['storage_service'] ?? null) {
107+
$container->register($storageId = 'limiter.storage.'.$name, CacheStorage::class)->addArgument(new Reference($limiterConfig['cache_pool']));
108+
}
109+
110+
$limiter->replaceArgument(1, new Reference($storageId));
111+
unset($limiterConfig['storage_service'], $limiterConfig['cache_pool']);
112+
113+
$limiterConfig['id'] = $name;
114+
$limiter->replaceArgument(0, $limiterConfig);
115+
116+
$container->registerAliasForArgument($limiterId, RateLimiterFactory::class, $name.'.limiter');
117+
}
91118
}

0 commit comments

Comments
 (0)