16
16
use Symfony \Bundle \SecurityBundle \DependencyInjection \SecurityExtension ;
17
17
use Symfony \Bundle \SecurityBundle \SecurityBundle ;
18
18
use Symfony \Bundle \SecurityBundle \Tests \DependencyInjection \Fixtures \UserProvider \DummyProvider ;
19
+ use Symfony \Bundle \SecurityBundle \Tests \Functional \Bundle \FirewallEntryPointBundle \Security \EntryPointStub ;
20
+ use Symfony \Bundle \SecurityBundle \Tests \Functional \Bundle \GuardedBundle \AppCustomAuthenticator ;
21
+ use Symfony \Component \Config \Definition \Exception \InvalidConfigurationException ;
19
22
use Symfony \Component \DependencyInjection \Argument \IteratorArgument ;
20
23
use Symfony \Component \DependencyInjection \ContainerBuilder ;
21
24
use Symfony \Component \DependencyInjection \Reference ;
22
25
use Symfony \Component \ExpressionLanguage \Expression ;
26
+ use Symfony \Component \HttpFoundation \Request ;
27
+ use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
67E6
28
+ use Symfony \Component \Security \Core \Exception \AuthenticationException ;
29
+ use Symfony \Component \Security \Core \User \UserInterface ;
23
30
use Symfony \Component \Security \Core \User \UserProviderInterface ;
31
+ use Symfony \Component \Security \Guard \AuthenticatorInterface ;
24
32
25
33
class SecurityExtensionTest extends TestCase
26
34
{
@@ -413,6 +421,90 @@ public function testSwitchUserWithSeveralDefinedProvidersButNoFirewallRootProvid
413
421
$ this ->assertEquals (new Reference ('security.user.provider.concrete.second ' ), $ container ->getDefinition ('security.authentication.switchuser_listener.foobar ' )->getArgument (1 ));
414
422
}
415
423
424
+ /**
425
+ * @dataProvider provideEntryPointFirewalls
426
+ */
427
+ public function testAuthenticatorManagerEnabledEntryPoint (array $ firewall , $ entryPointId )
428
+ {
429
+ $ container = $ this ->getRawContainer ();
430
+ $ container ->loadFromExtension ('security ' , [
431
+ 'enable_authenticator_manager ' => true ,
432
+ 'providers ' => [
433
+ 'first ' => ['id ' => 'users ' ],
434
+ ],
435
+
436
+ 'firewalls ' => [
437
+ 'main ' => $ firewall ,
438
+ ],
439
+ ]);
440
+
441
+ $ container ->compile ();
442
+
443
+ $ this ->assertEquals ($ entryPointId , (string ) $ container ->getDefinition ('security.firewall.map.config.main ' )->getArgument (7 ));
444
+ $ this ->assertEquals ($ entryPointId , (string ) $ container ->getDefinition ('security.exception_listener.main ' )->getArgument (4 ));
445
+ }
446
+
447
+ public function provideEntryPointFirewalls ()
448
+ {
449
+ // only one entry point available
450
+ yield [['http_basic ' => true ], 'security.authentication.basic_entry_point.main ' ];
451
+ // explicitly configured by authenticator key
452
+ yield [['form_login ' => true , 'http_basic ' => true , 'entry_point ' => 'form_login ' ], 'security.authentication.form_entry_point.main ' ];
453
+ // explicitly configured another service
454
+ yield [['form_login ' => true , 'entry_point ' => EntryPointStub::class], EntryPointStub::class];
455
+ // no entry point required
456
+ yield [['json_login ' => true ], null ];
457
+
458
+ // only one guard authenticator entry point available
459
+ yield [[
460
+ 'guard ' => ['authenticators ' => [AppCustomAuthenticator::class]],
461
+ ], AppCustomAuthenticator::class];
462
+ // explicitly configured guard authenticator entry point
463
+ yield [[
464
+ 'guard ' => [
465
+ 'authenticators ' => [AppCustomAuthenticator::class, NullAuthenticator::class],
466
+ 'entry_point ' => NullAuthenticator::class,
467
+ ],
468
+ ], NullAuthenticator::class];
469
+ }
470
+
471
+ /**
472
+ * @dataProvider provideEntryPointRequiredData
473
+ */
474
+ public function testEntryPointRequired (array $ firewall , $ messageRegex )
475
+ {
476
+ $ this ->expectException (InvalidConfigurationException::class);
477
+ $ this ->expectExceptionMessageMatches ($ messageRegex );
478
+
479
+ $ container = $ this ->getRawContainer ();
480
+ $ container ->loadFromExtension ('security ' , [
481
+ 'enable_authenticator_manager ' => true ,
482
+ 'providers ' => [
483
+ 'first ' => ['id ' => 'users ' ],
484
+ ],
485
+
486
+ 'firewalls ' => [
487
+ 'main ' => $ firewall ,
488
+ ],
489
+ ]);
490
+
491
+ $ container ->compile ();
492
+ }
493
+
494
+ public function provideEntryPointRequiredData ()
495
+ {
496
+ // more than one entry point available and not explicitly set
497
+ yield [
498
+ ['http_basic ' => true , 'form_login ' => true ],
499
+ '/^Because you have multiple authenticators in firewall "main", you need to set the "entry_point" key to one of your authenticators/ ' ,
500
+ ];
501
+ // more than one guard entry point available and not explicitly set
502
+ yield [
503
+ ['guard ' => ['authenticators ' => [AppCustomAuthenticator::class, NullAuthenticator::class]]],
504
+ '/^Because you have multiple guard authenticators, you need to set the "entry_point" key to one of your authenticators/ ' ,
505
+ ];
506
+ }
507
+
416
508
protected function getRawContainer ()
417
509
{
418
510
$ container = new ContainerBuilder ();
@@ -439,3 +531,42 @@ protected function getContainer()
439
531
return $ container ;
440
532
}
441
533
}
534
+
535
+ class NullAuthenticator implements AuthenticatorInterface
536
+ {
537
+ public function start (Request $ request , AuthenticationException $ authException = null )
538
+ {
539
+ }
540
+
541
+ public function supports (Request $ request )
542
+ {
543
+ }
544
+
545
+ public function getCredentials (Request $ request )
546
+ {
547
+ }
548
+
549
+ public function getUser ($ credentials , UserProviderInterface $ userProvider )
550
+ {
551
+ }
552
+
553
+ public function checkCredentials ($ credentials , UserInterface $ user )
554
+ {
555
+ }
556
+
557
+ public function createAuthenticatedToken (UserInterface $ user , string $ providerKey )
558
+ {
559
+ }
560
+
561
+ public function onAuthenticationFailure (Request $ request , AuthenticationException $ exception )
562
+ {
563
+ }
564
+
565
+ public function onAuthenticationSuccess (Request $ request , TokenInterface $ token , string $ providerKey )
566
+ {
567
+ }
568
+
569
+ public function supportsRememberMe ()
570
+ {
571
+ }
572
+ }
0 commit comments