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