Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions Security/Http/Firewall/TwoFactorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Scheb\TwoFactorBundle\Security\Http\Firewall;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Scheb\TwoFactorBundle\DependencyInjection\Factory\Security\TwoFactorFactory;
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenFactoryInterface;
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
Expand Down Expand Up @@ -140,7 +141,7 @@ public function __construct(
$this->twoFactorAccessDecider = $twoFactorAccessDecider;
$this->eventDispatcher = $eventDispatcher;
$this->twoFactorTokenFactory = $twoFactorTokenFactory;
$this->logger = $logger;
$this->logger = $logger === null ? new NullLogger() : $logger;
$this->trustedDeviceManager = $trustedDeviceManager;
}

Expand Down Expand Up @@ -215,19 +216,15 @@ private function attemptAuthentication(Request $request, TwoFactorTokenInterface

private function onFailure(Request $request, AuthenticationException $failed): Response
{
if ($this->logger) {
$this->logger->info('Two-factor authentication request failed.', ['exception' => $failed]);
}
$this->logger->info('Two-factor authentication request failed.', ['exception' => $failed]);
$this->dispatchTwoFactorAuthenticationEvent(TwoFactorAuthenticationEvents::FAILURE, $request, $this->tokenStorage->getToken());

return $this->failureHandler->onAuthenticationFailure($request, $failed);
}

private function onSuccess(Request $request, TokenInterface $token, TwoFactorTokenInterface $previousTwoFactorToken): Response
{
if ($this->logger) {
$this->logger->info('User has been two-factor authenticated successfully.', ['username' => $token->getUsername()]);
}
$this->logger->info('User has been two-factor authenticated successfully.', ['username' => $token->getUsername()]);
$this->tokenStorage->setToken($token);
$this->dispatchTwoFactorAuthenticationEvent(TwoFactorAuthenticationEvents::SUCCESS, $request, $token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Scheb\TwoFactorBundle\Security\TwoFactor\Provider;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Event\TwoFactorAuthenticationEvent;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
Expand All @@ -29,7 +30,7 @@ class TwoFactorProviderPreparationListener
private $twoFactorToken;

/**
* @var LoggerInterface|null
* @var LoggerInterface
*/
private $logger;

Expand Down Expand Up @@ -58,7 +59,7 @@ public function __construct(
) {
$this->providerRegistry = $providerRegistry;
$this->preparationRecorder = $preparationRecorder;
$this->logger = $logger;
$this->logger = $logger === null ? new NullLogger() : $logger;
$this->firewallName = $firewallName;
$this->prepareOnLogin = $prepareOnLogin;
$this->prepareOnAccessDenied = $prepareOnAccessDenied;
Expand Down Expand Up @@ -105,18 +106,14 @@ public function onKernelFinishRequest(FinishRequestEvent $event): void

try {
if ($this->preparationRecorder->isProviderPrepared($firewallName, $providerName)) {
if ($this->logger) {
$this->logger->info(sprintf('Two-factor provider "%s" was already prepared.', $providerName));
}
$this->logger->info(sprintf('Two-factor provider "%s" was already prepared.', $providerName));

return;
}
$user = $this->twoFactorToken->getUser();
$this->providerRegistry->getProvider($providerName)->prepareAuthentication($user);
$this->preparationRecorder->recordProviderIsPrepared($firewallName, $providerName);
if ($this->logger) {
$this->logger->info(sprintf('Two-factor provider "%s" prepared.', $providerName));
}
$this->logger->info(sprintf('Two-factor provider "%s" prepared.', $providerName));
} finally {
$this->preparationRecorder->saveSession();
}
Expand Down