8000 Updating behavior to not continue after an authenticator has set the … · symfony/symfony@f403444 · GitHub
[go: up one dir, main page]

Skip to content

Commit f403444

Browse files
committed
Updating behavior to not continue after an authenticator has set the response
This mirrors the behavior in core: *if* a listener sets a response (on success or failure), then the other listeners are not called. But if a response is *not* set (which is sometimes the case for success, like in BasicAuthenticationListener), then the other listeners are called, and can even fail.
1 parent 1d5557f commit f403444

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticationListener.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ public function handle(GetResponseEvent $event)
7575
$uniqueGuardKey = $this->providerKey.'_'.$key;
7676

7777
$this->executeGuardAuthenticator($uniqueGuardKey, $guardAuthenticator, $event);
78+
79+
if ($event->hasResponse()) {
80+
$this->logger->info(sprintf('The "%s" authenticator set the response. Any later authenticator will not be called', get_class($guardAuthenticator)));
81+
82+
break;
83+
}
7884
}
7985
}
8086

src/Symfony/Component/Security/Guard/Tests/Firewall/GuardAuthenticationListenerTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ public function testHandleSuccess()
7979
$listener->handle($this->event);
8080
}
8181

82+
public function testHandleSuccessStopsAfterResponseIsSet()
83+
{
84+
$authenticator1 = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
85+
$authenticator2 = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
86+
87+
// mock the first authenticator to fail, and set a Response
88+
$authenticator1
89+
->expects($this->once())
90+
->method('getCredentials')
91+
->willThrowException(new AuthenticationException());
92+
$this->guardAuthenticatorHandler
93+
->expects($this->once())
94+
->method('handleAuthenticationFailure')
95+
->willReturn(new Response());
96+
// the second authenticator should *never* be called
97+
$authenticator2
98+
->expects($this->never())
99+
->method('getCredentials');
100+
101+
$listener = new GuardAuthenticationListener(
102+
$this->guardAuthenticatorHandler,
103+
$this->authenticationManager,
104+
'my_firewall',
105+
array($authenticator1, $authenticator2),
106+
$this->logger
107+
);
108+
109+
$listener->handle($this->event);
110+
}
111+
82112
public function testHandleSuccessWithRememberMe()
83113
{
84114
$authenticator = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
@@ -201,7 +231,10 @@ protected function setUp()
201231

202232
$this->request = new Request(array(), array(), array(), array(), array(), array());
203233

204-
$this->event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
234+
$this->event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
235+
->disableOriginalConstructor()
236+
->setMethods(array('getRequest'))
237+
->getMock();
205238
$this->event
206239
->expects($this->any())
207240
->method('getRequest')

0 commit comments

Comments
 (0)
0