Rename AuthenticatingListener

This commit is contained in:
Wouter de Jong 2020-03-07 14:04:35 +01:00
parent 60d396f2d1
commit 59f49b20ca
2 changed files with 18 additions and 12 deletions

View File

@ -43,7 +43,7 @@
<!-- Listeners -->
<service id="Symfony\Component\Security\Http\EventListener\AuthenticatingListener">
<service id="security.listener.verify_authenticator_credentials" class="Symfony\Component\Security\Http\EventListener\VerifyAuthenticatorCredentialsListener">
<tag name="kernel.event_subscriber" />
<argument type="service" id="security.encoder_factory" />
</service>

View File

@ -4,6 +4,7 @@ namespace Symfony\Component\Security\Http\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\LogicException;
use Symfony\Component\Security\Http\Authenticator\CustomAuthenticatedInterface;
use Symfony\Component\Security\Http\Authenticator\PasswordAuthenticatedInterface;
@ -19,7 +20,7 @@ use Symfony\Component\Security\Http\Event\VerifyAuthenticatorCredentialsEvent;
* @final
* @experimental in 5.1
*/
class AuthenticatingListener implements EventSubscriberInterface
class VerifyAuthenticatorCredentialsListener implements EventSubscriberInterface
{
private $encoderFactory;
@ -28,22 +29,22 @@ class AuthenticatingListener implements EventSubscriberInterface
$this->encoderFactory = $encoderFactory;
}
public static function getSubscribedEvents(): array
{
return [VerifyAuthenticatorCredentialsEvent::class => ['onAuthenticating', 128]];
}
public function onAuthenticating(VerifyAuthenticatorCredentialsEvent $event): void
{
$authenticator = $event->getAuthenticator();
if ($authenticator instanceof PasswordAuthenticatedInterface) {
// Use the password encoder to validate the credentials
$user = $event->getUser();
$event->setCredentialsValid($this->encoderFactory->getEncoder($user)->isPasswordValid(
$user->getPassword(),
$authenticator->getPassword($event->getCredentials()),
$user->getSalt()
));
$presentedPassword = $authenticator->getPassword($event->getCredentials());
if ('' === $presentedPassword) {
throw new BadCredentialsException('The presented password cannot be empty.');
}
if (null === $user->getPassword()) {
return;
}
$event->setCredentialsValid($this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $presentedPassword, $user->getSalt()));
return;
}
@ -65,4 +66,9 @@ class AuthenticatingListener implements EventSubscriberInterface
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));
}
public static function getSubscribedEvents(): array
{
return [VerifyAuthenticatorCredentialsEvent::class => ['onAuthenticating', 128]];
}
}