Skip to content

Commit 634faa2

Browse files
committed
1 parent 782cdcd commit 634faa2

File tree

8 files changed

+32
-15
lines changed

8 files changed

+32
-15
lines changed

docs/reference/two_step_validation.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ Edit the configuration file:
3535
google_authenticator:
3636
enabled: true
3737
server: yourserver.com
38-
ip_white_list:
38+
trusted_ip_list:
3939
- 127.0.0.1
4040
forced_for_role:
4141
- ROLE_ADMIN
4242
43-
Also, if you want to use ``ip_white_list`` and ``forced_for_role``
43+
Also, if you want to use ``trusted_ip_list`` and ``forced_for_role``
4444
configuration nodes for automatically setting the secret to user
4545
(secret - a connection between user and device that will scans QR-code)
4646
and showing QR-code in login form, you need to set the success handler

src/DependencyInjection/Configuration.php

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public function getConfigTreeBuilder()
6969
->scalarNode('server')->cannotBeEmpty()->end()
7070
->scalarNode('enabled')->defaultFalse()->end()
7171
->arrayNode('ip_white_list')
72+
->prototype('scalar')->end()
73+
->info('IPs for which 2FA will be skipped.')
74+
->setDeprecated('The "%node%" option is deprecated. Use "trusted_ip_list" instead with the same values.')
75+
->end()
76+
->arrayNode('trusted_ip_list')
7277
->prototype('scalar')->end()
7378
->defaultValue(['127.0.0.1'])
7479
->info('IPs for which 2FA will be skipped.')

src/DependencyInjection/SonataUserExtension.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,22 @@ public function configureGoogleAuthenticator($config, ContainerBuilder $containe
174174

175175
if (!class_exists('Google\Authenticator\GoogleAuthenticator')
176176
&& !class_exists('Sonata\GoogleAuthenticator\GoogleAuthenticator')) {
177-
throw new \RuntimeException('Please add ``sonata-project/google-authenticator`` package');
177+
throw new \RuntimeException('Please add "sonata-project/google-authenticator" package');
178178
}
179179

180180
$container->setParameter('sonata.user.google.authenticator.forced_for_role', $config['google_authenticator']['forced_for_role']);
181-
$container->setParameter('sonata.user.google.authenticator.ip_white_list', $config['google_authenticator']['ip_white_list']);
181+
182+
// NEXT_MAJOR: Remove this checks and only set the `trusted_ip_list`.
183+
if (\count($config['google_authenticator']['ip_white_list']) > 0 && $config['google_authenticator']['trusted_ip_list'] !== ['127.0.0.1']) {
184+
throw new \LogicException('Please use only "trusted_ip_list" parameter, "ip_white_list" is deprecated.');
185+
}
186+
$trustedIpList = $config['google_authenticator']['trusted_ip_list'];
187+
if (\count($config['google_authenticator']['ip_white_list']) > 0) {
188+
$trustedIpList = $config['google_authenticator']['ip_white_list'];
189+
}
190+
// NEXT_MAJOR: Remove `sonata.user.google.authenticator.ip_white_list` parameter.
191+
$container->setParameter('sonata.user.google.authenticator.ip_white_list', $trustedIpList);
192+
$container->setParameter('sonata.user.google.authenticator.trusted_ip_list', $trustedIpList);
182193

183194
$container->getDefinition('sonata.user.google.authenticator.provider')
184195
->replaceArgument(0, $config['google_authenticator']['server']);

src/GoogleAuthenticator/Helper.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,28 @@ class Helper
3939
/**
4040
* @var string[]
4141
*/
42-
private $ipWhiteList;
42+
private $trustedIpList;
4343

4444
/**
4545
* @var AuthorizationCheckerInterface
4646
*/
4747
private $authorizationChecker;
4848

4949
/**
50-
* @param string[] $ipWhiteList IPs that will bypass 2FA authorization
50+
* @param string[] $trustedIpList IPs that will bypass 2FA authorization
5151
*/
5252
public function __construct(
5353
$server,
5454
BaseGoogleAuthenticator $authenticator,
5555
AuthorizationCheckerInterface $authorizationChecker,
5656
array $forcedForRoles = [],
57-
array $ipWhiteList = []
57+
array $trustedIpList = []
5858
) {
5959
$this->server = $server;
6060
$this->authenticator = $authenticator;
6161
$this->authorizationChecker = $authorizationChecker;
6262
$this->forcedForRoles = $forcedForRoles;
63-
$this->ipWhiteList = $ipWhiteList;
63+
$this->trustedIpList = $trustedIpList;
6464
}
6565

6666
/**
@@ -99,7 +99,7 @@ public function getSessionKey(UsernamePasswordToken $token)
9999

100100
public function needToHaveGoogle2FACode(Request $request): bool
101101
{
102-
if (\in_array($request->getClientIp(), $this->ipWhiteList, true)) {
102+
if (\in_array($request->getClientIp(), $this->trustedIpList, true)) {
103103
return false;
104104
}
105105

src/Resources/config/google_authenticator.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<argument type="service" id="sonata.user.google.authenticator"/>
1010
<argument type="service" id="security.authorization_checker"/>
1111
<argument>%sonata.user.google.authenticator.forced_for_role%</argument>
12-
<argument>%sonata.user.google.authenticator.ip_white_list%</argument>
12+
<argument>%sonata.user.google.authenticator.trusted_ip_list%</argument>
1313
</service>
1414
<service id="sonata.user.google.authenticator.interactive_login_listener" class="Sonata\UserBundle\GoogleAuthenticator\InteractiveLoginListener">
1515
<tag name="kernel.event_listener" event="security.interactive_login" method="onSecurityInteractiveLogin"/>

tests/DependencyInjection/ConfigurationTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public function testDefault(): void
4242
],
4343
'google_authenticator' => [
4444
'enabled' => false,
45-
'ip_white_list' => ['127.0.0.1'],
45+
'ip_white_list' => [],
46+
'trusted_ip_list' => ['127.0.0.1'],
4647
'forced_for_role' => ['ROLE_ADMIN'],
4748
],
4849
'manager_type' => 'orm',

tests/DependencyInjection/SonataUserExtensionTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public function testConfigureGoogleAuthenticatorEnabled(): void
256256
{
257257
$this->expectDeprecation('The \'Google\Authenticator\' namespace is deprecated in sonata-project/GoogleAuthenticator since version 2.1 and will be removed in 3.0.');
258258

259-
$this->load(['google_authenticator' => ['enabled' => true, 'forced_for_role' => ['ROLE_USER'], 'ip_white_list' => ['0.0.0.1'],
259+
$this->load(['google_authenticator' => ['enabled' => true, 'forced_for_role' => ['ROLE_USER'], 'trusted_ip_list' => ['0.0.0.1'],
260260
'server' => 'bar', ]]);
261261

262262
$this->assertContainerBuilderHasParameter('sonata.user.google.authenticator.enabled', true);
@@ -265,7 +265,7 @@ public function testConfigureGoogleAuthenticatorEnabled(): void
265265
$this->assertContainerBuilderHasService('sonata.user.google.authenticator.interactive_login_listener');
266266
$this->assertContainerBuilderHasService('sonata.user.google.authenticator.request_listener');
267267
$this->assertContainerBuilderHasParameter('sonata.user.google.authenticator.forced_for_role', ['ROLE_ADMIN', 'ROLE_USER']);
268-
$this->assertContainerBuilderHasParameter('sonata.user.google.authenticator.ip_white_list', ['127.0.0.1', '0.0.0.1']);
268+
$this->assertContainerBuilderHasParameter('sonata.user.google.authenticator.trusted_ip_list', ['127.0.0.1', '0.0.0.1']);
269269
$this->assertContainerBuilderHasServiceDefinitionWithArgument('sonata.user.google.authenticator.provider', 0, 'bar');
270270
}
271271

tests/EventListener/TwoFactorLoginSuccessHandlerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ private function createTestClass(?string $secret, string $userRole, ?string $rem
105105
$routerMock = $this->createMock(UrlGeneratorInterface::class);
106106
$routerMock->method('generate')->willReturn('/admin/dashboard');
107107
$forcedRoles = ['ROLE_ADMIN'];
108-
$ipWhiteList = ['127.0.0.1'];
109-
$helper = new Helper('site.tld', new GoogleAuthenticator(), $authChecker, $forcedRoles, $ipWhiteList);
108+
$trustedIpList = ['127.0.0.1'];
109+
$helper = new Helper('site.tld', new GoogleAuthenticator(), $authChecker, $forcedRoles, $trustedIpList);
110110
$this->testClass = new TwoFactorLoginSuccessHandler(
111111
$templateEngineMock,
112112
$helper,

0 commit comments

Comments
 (0)