Skip to content

Commit fba19b8

Browse files
committed
Rename AuthenticatingListener
1 parent 73df611 commit fba19b8

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

EventListener/AuthenticatingListener.php renamed to EventListener/VerifyAuthenticatorCredentialsListener.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
66
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
7+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
78
use Symfony\Component\Security\Core\Exception\LogicException;
89
use Symfony\Component\Security\Http\Authenticator\CustomAuthenticatedInterface;
910
use Symfony\Component\Security\Http\Authenticator\PasswordAuthenticatedInterface;
@@ -19,7 +20,7 @@
1920
* @final
2021
* @experimental in 5.1
2122
*/
22-
class AuthenticatingListener implements EventSubscriberInterface
23+
class VerifyAuthenticatorCredentialsListener implements EventSubscriberInterface
2324
{
2425
private $encoderFactory;
2526

@@ -28,22 +29,22 @@ public function __construct(EncoderFactoryInterface $encoderFactory)
2829
$this->encoderFactory = $encoderFactory;
2930
}
3031

31-
public static function getSubscribedEvents(): array
32-
{
33-
return [VerifyAuthenticatorCredentialsEvent::class => ['onAuthenticating', 128]];
34-
}
35-
3632
public function onAuthenticating(VerifyAuthenticatorCredentialsEvent $event): void
3733
{
3834
$authenticator = $event->getAuthenticator();
3935
if ($authenticator instanceof PasswordAuthenticatedInterface) {
4036
// Use the password encoder to validate the credentials
4137
$user = $event->getUser();
42-
$event->setCredentialsValid($this->encoderFactory->getEncoder($user)->isPasswordValid(
43-
$user->getPassword(),
44-
$authenticator->getPassword($event->getCredentials()),
45-
$user->getSalt()
46-
));
38+
$presentedPassword = $authenticator->getPassword($event->getCredentials());
39+
if ('' === $presentedPassword) {
40+
throw new BadCredentialsException('The presented password cannot be empty.');
41+
}
42+
43+
if (null === $user->getPassword()) {
44+
return;
45+
}
46+
47+
$event->setCredentialsValid($this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $presentedPassword, $user->getSalt()));
4748

4849
return;
4950
}
@@ -65,4 +66,9 @@ public function onAuthenticating(VerifyAuthenticatorCredentialsEvent $event): vo
6566

6667
throw new LogicException(sprintf('Authenticator %s does not have valid credentials. Authenticators must implement one of the authenticated interfaces (%s, %s or %s).', \get_class($authenticator), PasswordAuthenticatedInterface::class, TokenAuthenticatedInterface::class, CustomAuthenticatedInterface::class));
6768
}
69+
70+
public static function getSubscribedEvents(): array
71+
{
72+
return [VerifyAuthenticatorCredentialsEvent::class => ['onAuthenticating', 128]];
73+
}
6874
}

0 commit comments

Comments
 (0)