Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1dc1169

Browse filesBrowse files
committed
Decorate authenticators in the container rather than in the listener
1 parent 193b9dc commit 1dc1169
Copy full SHA for 1dc1169

File tree

7 files changed

+42
-40
lines changed
Filter options

7 files changed

+42
-40
lines changed

‎src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
use Symfony\Component\Security\Core\User\ChainUserProvider;
5959
use Symfony\Component\Security\Core\User\UserCheckerInterface;
6060
use Symfony\Component\Security\Core\User\UserProviderInterface;
61+
use Symfony\Component\Security\Http\Authenticator\Debug\TraceableAuthenticator;
6162
use Symfony\Component\Security\Http\Authenticator\Debug\TraceableAuthenticatorManagerListener;
6263
use Symfony\Component\Security\Http\Event\CheckPassportEvent;
6364
use Symfony\Flex\Command\InstallRecipesCommand;
@@ -638,6 +639,15 @@ private function createAuthenticationListeners(ContainerBuilder $container, stri
638639
}
639640
}
640641

642+
if ($container->hasDefinition('debug.security.firewall')) {
643+
foreach ($authenticationProviders as $authenticatorId) {
644+
$container->register('debug.'.$authenticatorId, TraceableAuthenticator::class)
645+
->setDecoratedService($authenticatorId)
646+
->setArguments([new Reference('debug.'.$authenticatorId.'.inner')])
647+
;
648+
}
649+
}
650+
641651
// the actual entry point is configured by the RegisterEntryPointPass
642652
$container->setParameter('security.'.$id.'._indexed_authenticators', $entryPoints);
643653

‎src/Symfony/Bundle/SecurityBundle/Resources/views/Collector/security.html.twig

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Resources/views/Collector/security.html.twig
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@
360360

361361
<tr>
362362
<td class="font-normal">{{ profiler_dump(authenticator.stub) }}</td>
363-
<td class="no-wrap">{{ source('@WebProfiler/Icon/' ~ (authenticator.supports ? 'yes' : 'no') ~ '.svg') }}</td>
363+
<td class="no-wrap">{{ source('@WebProfiler/Icon/' ~ (false != authenticator.supports ? 'yes' : 'no') ~ '.svg') }}</td>
364364
<td class="no-wrap">{{ authenticator.authenticated is not null ? source('@WebProfiler/Icon/' ~ (authenticator.authenticated ? 'yes' : 'no') ~ '.svg') : '' }}</td>
365365
<td class="no-wrap">{{ authenticator.duration is null ? '(none)' : '%0.2f ms'|format(authenticator.duration * 1000) }}</td>
366366
<td class="font-normal">{{ authenticator.passport ? profiler_dump(authenticator.passport) : '(none)' }}</td>

‎src/Symfony/Bundle/SecurityBundle/Tests/Debug/TraceableFirewallListenerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Tests/Debug/TraceableFirewallListenerTest.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2323
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2424
use Symfony\Component\Security\Http\Authentication\AuthenticatorManager;
25+
use Symfony\Component\Security\Http\Authenticator\Debug\TraceableAuthenticator;
2526
use Symfony\Component\Security\Http\Authenticator\Debug\TraceableAuthenticatorManagerListener;
2627
use Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface;
2728
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
@@ -99,7 +100,7 @@ public function testOnKernelRequestRecordsAuthenticatorsInfo()
99100
$tokenStorage = $this->createMock(TokenStorageInterface::class);
100101
$dispatcher = new EventDispatcher();
101102
$authenticatorManager = new AuthenticatorManager(
102-
[$notSupportingAuthenticator, $supportingAuthenticator],
103+
[new TraceableAuthenticator($notSupportingAuthenticator), new TraceableAuthenticator($supportingAuthenticator)],
103104
$tokenStorage,
104105
$dispatcher,
105106
'main'

‎src/Symfony/Component/Security/Http/Authentication/AuthenticatorManager.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Authentication/AuthenticatorManager.php
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,12 @@ public function supports(Request $request): ?bool
109109
}
110110

111111
$request->attributes->set('_security_skipped_authenticators', $skippedAuthenticators);
112+
$request->attributes->set('_security_authenticators', $authenticators);
112113

113114
if (!$authenticators) {
114115
return false;
115116
}
116117

117-
$request->attributes->set('_security_authenticators', $authenticators);
118-
119118
return $lazy ? null : true;
120119
}
121120

‎src/Symfony/Component/Security/Http/Authenticator/Debug/TraceableAuthenticator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Authenticator/Debug/TraceableAuthenticator.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*/
3131
final class TraceableAuthenticator implements AuthenticatorInterface, InteractiveAuthenticatorInterface, AuthenticationEntryPointInterface
3232
{
33+
private ?bool $supports;
3334
private ?Passport $passport = null;
3435
private ?float $duration = null;
3536
private ClassStub|string $stub;
@@ -42,7 +43,7 @@ public function __construct(private AuthenticatorInterface $authenticator)
4243
public function getInfo(): array
4344
{
4445
return [
45-
'supports' => true,
46+
'supports' => $this->supports,
4647
'passport' => $this->passport,
4748
'duration' => $this->duration,
4849
'stub' => $this->stub ??= class_exists(ClassStub::class) ? new ClassStub($this->authenticator::class) : $this->authenticator::class,
@@ -61,7 +62,7 @@ static function (BadgeInterface $badge): array {
6162

6263
public function supports(Request $request): ?bool
6364
{
64-
return $this->authenticator->supports($request);
65+
return $this->supports = $this->authenticator->supports($request);
6566
}
6667

6768
public function authenticate(Request $request): Passport

‎src/Symfony/Component/Security/Http/Authenticator/Debug/TraceableAuthenticatorManagerListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Authenticator/Debug/TraceableAuthenticatorManagerListener.php
+12-34Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\HttpKernel\Event\RequestEvent;
1616
use Symfony\Component\Security\Http\Firewall\AbstractListener;
1717
use Symfony\Component\Security\Http\Firewall\AuthenticatorManagerListener;
18-
use Symfony\Component\VarDumper\Caster\ClassStub;
1918
use Symfony\Contracts\Service\ResetInterface;
2019

2120
/**
@@ -25,52 +24,28 @@
2524
*/
2625
final class TraceableAuthenticatorManagerListener extends AbstractListener implements ResetInterface
2726
{
28-
private array $authenticatorsInfo = [];
29-
private bool $hasVardumper;
27+
private array $authenticators = [];
3028

31-
public function __construct(
32-
private AuthenticatorManagerListener $authenticationManagerListener,
33-
) {
34-
$this->hasVardumper = class_exists(ClassStub::class);
29+
public function __construct(private AuthenticatorManagerListener $authenticationManagerListener)
30+
{
3531
}
3632

3733
public function supports(Request $request): ?bool
3834
{
3935
$supports = $this->authenticationManagerListener->supports($request);
4036

41-
foreach ($request->attributes->get('_security_skipped_authenticators') as $skippedAuthenticator) {
42-
$this->authenticatorsInfo[] = [
43-
'supports' => false,
44-
'stub' => $this->hasVardumper ? new ClassStub($skippedAuthenticator::class) : $skippedAuthenticator::class,
45-
'passport' => null,
46-
'duration' => null,
47-
'authenticated' => null,
48-
'badges' => [],
49-
];
37+
foreach (array_merge($request->attributes->get('_security_skipped_authenticators'), $request->attributes->get('_security_authenticators')) as $authenticator) {
38+
if ($authenticator instanceof TraceableAuthenticator) {
39+
$this->authenticators[] = $authenticator;
40+
}
5041
}
5142

5243
return $supports;
5344
}
5445

5546
public function authenticate(RequestEvent $event): void
5647
{
57-
$request = $event->getRequest();
58-
59-
if (!$authenticators = $request->attributes->get('_security_authenticators')) {
60-
return;
61-
}
62-
63-
foreach ($authenticators as $key => $authenticator) {
64-
$authenticators[$key] = new TraceableAuthenticator($authenticator);
65-
}
66-
67-
$request->attributes->set('_security_authenticators', $authenticators);
68-
6948
$this->authenticationManagerListener->authenticate($event);
70-
71-
foreach ($authenticators as $authenticator) {
72-
$this->authenticatorsInfo[] = $authenticator->getInfo();
73-
}
7449
}
7550

7651
public function getAuthenticatorManagerListener(): AuthenticatorManagerListener
@@ -80,11 +55,14 @@ public function getAuthenticatorManagerListener(): AuthenticatorManagerListener
8055

8156
public function getAuthenticatorsInfo(): array
8257
{
83-
return $this->authenticatorsInfo;
58+
return array_map(
59+
static fn (TraceableAuthenticator $authenticator) => $authenticator->getInfo(),
60+
$this->authenticators
61+
);
8462
}
8563

8664
public function reset(): void
8765
{
88-
$this->authenticatorsInfo = [];
66+
$this->authenticators = [];
8967
}
9068
}

‎src/Symfony/Component/Security/Http/Tests/Authenticator/Debug/TraceableAuthenticatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Tests/Authenticator/Debug/TraceableAuthenticatorTest.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,35 @@ public function testGetInfo()
2626
$passport = new SelfValidatingPassport(new UserBadge('robin', function () {}));
2727

2828
$authenticator = $this->createMock(AuthenticatorInterface::class);
29+
$authenticator->expects($this->once())
30+
->method('supports')
31+
->with($request)
32+
->willReturn(true);
33+
2934
$authenticator
3035
->expects($this->once())
3136
->method('authenticate')
3237
->with($request)
3338
->willReturn($passport);
3439

3540
$traceable = new TraceableAuthenticator($authenticator);
41+
$this->assertTrue($traceable->supports($request));
3642
$this->assertSame($passport, $traceable->authenticate($request));
3743
$this->assertSame($passport, $traceable->getInfo()['passport']);
3844
}
3945

4046
public function testGetInfoWithoutAuth()
4147
{
48+
$request = new Request();
49+
4250
$authenticator = $this->createMock(AuthenticatorInterface::class);
51+
$authenticator->expects($this->once())
52+
->method('supports')
53+
->with($request)
54+
->willReturn(false);
4355

4456
$traceable = new TraceableAuthenticator($authenticator);
57+
$this->assertFalse($traceable->supports($request));
4558
$this->assertNull($traceable->getInfo()['passport']);
4659
$this->assertIsArray($traceable->getInfo()['badges']);
4760
$this->assertSame([], $traceable->getInfo()['badges']);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.