Skip to content

Commit 9ea8e31

Browse files
committed
Try making authenticator priority configurable
1 parent e4bf04e commit 9ea8e31

File tree

9 files changed

+24
-5
lines changed

9 files changed

+24
-5
lines changed

app/config/packages/scheb_2fa.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ scheb_two_factor:
5252
security_tokens:
5353
- Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
5454

55+
authenticator_priority: -50
56+
5557
# A list of IP addresses, which will not trigger two-factor authentication
5658
ip_whitelist:
5759
- 127.0.0.2 # Used for testing

doc/configuration.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ Bundle Configuration
7878
- Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
7979
- Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken
8080
81+
# Priority of the TwoFactorAuthenticator within Symfony's firewall
82+
authenticator_priority: 0
83+
8184
# A list of IP addresses or netmasks, which will not trigger two-factor authentication.
8285
# Supports IPv4, IPv6 and IP subnet masks.
8386
ip_whitelist:

src/bundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function getConfigTreeBuilder(): TreeBuilder
5555
->prototype('scalar')->end()
5656
->end()
5757
->scalarNode('ip_whitelist_provider')->defaultValue('scheb_two_factor.default_ip_whitelist_provider')->end()
58+
->scalarNode('authenticator_priority')->defaultValue(0)->end()
5859
->scalarNode('two_factor_token_factory')->defaultValue('scheb_two_factor.default_token_factory')->end()
5960
->scalarNode('two_factor_provider_decider')->defaultValue('scheb_two_factor.default_provider_decider')->end()
6061
->scalarNode('two_factor_condition')->defaultNull()->end()

src/bundle/DependencyInjection/Factory/Security/TwoFactorFactory.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ class TwoFactorFactory implements FirewallListenerFactoryInterface, Authenticato
5757
public const KERNEL_ACCESS_LISTENER_DEFINITION_ID = 'scheb_two_factor.security.access_listener';
5858
public const FORM_LISTENER_DEFINITION_ID = 'scheb_two_factor.security.form_listener';
5959

60-
public function __construct(private readonly TwoFactorServicesFactory $twoFactorServicesFactory)
61-
{
60+
public function __construct(
61+
private readonly ContainerBuilder $container,
62+
private readonly TwoFactorServicesFactory $twoFactorServicesFactory,
63+
) {
6264
}
6365

6466
public function addConfiguration(NodeDefinition $builder): void
@@ -169,6 +171,6 @@ public function getKey(): string
169171

170172
public function getPriority(): int
171173
{
172-
return 0;
174+
return $this->container->getParameter('scheb_two_factor.authenticator_priority');
173175
}
174176
}

src/bundle/DependencyInjection/SchebTwoFactorExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function load(array $configs, ContainerBuilder $container): void
3131
$container->setParameter('scheb_two_factor.model_manager_name', $config['model_manager_name']);
3232
$container->setParameter('scheb_two_factor.security_tokens', $config['security_tokens']);
3333
$container->setParameter('scheb_two_factor.ip_whitelist', $config['ip_whitelist']);
34+
$container->setParameter('scheb_two_factor.authenticator_priority', $config['authenticator_priority']);
3435

3536
$loader = new Loader\PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3637
$loader->load('security.php');

src/bundle/SchebTwoFactorBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function build(ContainerBuilder $container): void
3030
$extension = $container->getExtension('security');
3131
assert($extension instanceof SecurityExtension);
3232

33-
$securityFactory = new TwoFactorFactory(new TwoFactorServicesFactory());
33+
$securityFactory = new TwoFactorFactory($container, new TwoFactorServicesFactory());
3434
$extension->addAuthenticatorFactory($securityFactory);
3535
}
3636
}

tests/DependencyInjection/Factory/Security/TwoFactorFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TwoFactorFactoryTest extends TestCase
3535
public function setUp(): void
3636
{
3737
$this->servicesFactory = $this->createMock(TwoFactorServicesFactory::class);
38-
$this->factory = new TwoFactorFactory($this->servicesFactory);
38+
$this->factory = new TwoFactorFactory($this->servicesFactory, 0);
3939
$this->container = new ContainerBuilder();
4040
$this->container->setDefinition('scheb_two_factor.firewall_context', new Definition());
4141
}

tests/DependencyInjection/SchebTwoFactorExtensionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function load_emptyConfig_setDefaultValues(): void
6565
'Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken',
6666
], 'scheb_two_factor.security_tokens');
6767
$this->assertHasParameter([], 'scheb_two_factor.ip_whitelist');
68+
$this->assertHasParameter(0, 'scheb_two_factor.authenticator_priority');
6869
}
6970

7071
/**
@@ -100,6 +101,7 @@ public function load_fullConfig_setConfigValues(): void
100101
$this->assertHasParameter('/cookie-path', 'scheb_two_factor.trusted_device.cookie_path');
101102
$this->assertHasParameter(['Symfony\Component\Security\Core\Authentication\Token\SomeToken'], 'scheb_two_factor.security_tokens');
102103
$this->assertHasParameter(['127.0.0.1', '10.0.0.0/8', '192.168.0.0/16'], 'scheb_two_factor.ip_whitelist');
104+
$this->assertHasParameter(-50, 'scheb_two_factor.authenticator_priority');
103105
}
104106

105107
/**
@@ -649,6 +651,7 @@ private function getFullConfig(): array
649651
- 127.0.0.1
650652
- ['10.0.0.0/8', '192.168.0.0/16']
651653
ip_whitelist_provider: acme_test.ip_whitelist_provider
654+
authenticator_priority: -50
652655
two_factor_token_factory: acme_test.two_factor_token_factory
653656
two_factor_provider_decider: acme_test.two_factor_provider_decider
654657
two_factor_condition: acme_test.two_factor_condition

tests/SchebTwoFactorBundleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public function build_initializeBundle_addCompilerPass(): void
2828
$this->isInstanceOf(MailerCompilerPass::class),
2929
];
3030

31+
// Expect parameter to be read
32+
$containerBuilder
33+
->expects($this->any())
34+
->method('getParameter')
35+
->with('scheb_two_factor.authenticator_priority')
36+
->willReturn(0);
37+
3138
// Expect compiler pass to be added
3239
$containerBuilder
3340
->expects($this->exactly(count($compilerPasses)))

0 commit comments

Comments
 (0)