Skip to content

Commit

Permalink
Add Logger message and remove error exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabbarowski committed Nov 26, 2022
1 parent 0e7896b commit 64f06ee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
abstract_arg('authentication failure handler'),
abstract_arg('options'),
service('property_accessor')->nullOnInvalid(),
service('logger')->nullOnInvalid(),
])
->call('setTranslator', [service('translator')->ignoreOnInvalid()])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Security\Http\Authenticator;

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -50,15 +51,17 @@ class JsonLoginAuthenticator implements InteractiveAuthenticatorInterface
private ?AuthenticationSuccessHandlerInterface $successHandler;
private ?AuthenticationFailureHandlerInterface $failureHandler;
private ?TranslatorInterface $translator = null;
private ?LoggerInterface $logger = null;

public function __construct(HttpUtils $httpUtils, UserProviderInterface $userProvider, AuthenticationSuccessHandlerInterface $successHandler = null, AuthenticationFailureHandlerInterface $failureHandler = null, array $options = [], PropertyAccessorInterface $propertyAccessor = null)
public function __construct(HttpUtils $httpUtils, UserProviderInterface $userProvider, AuthenticationSuccessHandlerInterface $successHandler = null, AuthenticationFailureHandlerInterface $failureHandler = null, array $options = [], PropertyAccessorInterface $propertyAccessor = null, LoggerInterface $logger = null)
{
$this->options = array_merge(['username_path' => 'username', 'password_path' => 'password'], $options);
$this->httpUtils = $httpUtils;
$this->successHandler = $successHandler;
$this->failureHandler = $failureHandler;
$this->userProvider = $userProvider;
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
$this->logger = $logger;
}

public function supports(Request $request): ?bool
Expand Down Expand Up @@ -185,6 +188,7 @@ private function isNotLoginPath(Request $request): bool
/**
* Check is the user wants to login.
* It will detected by check the body content. If the array see a key called password and the type is wrong the Exception will be display.
* Also body is empty is.
*/
private function errorBadFormatMessage($request): void
{
Expand All @@ -198,7 +202,12 @@ private function errorBadFormatMessage($request): void
}
$data = json_decode($request->getContent(), true);
if (\is_array($data) && \array_key_exists($this->options['password_path'], $data)) {
throw new BadRequestHttpException(sprintf('Content type was detected as "%s". Request format was detected as "%s". Please use "json" as request format or content type. ', $contentType, $request->getRequestFormat()));
$this->logger?->debug(sprintf('Skipping JSON authenticator as the request body is not JSON (got: "%s").', $contentType), ['authenticator' => static::class]);

return;
}
if (null === $data) {
$this->logger?->debug('Skipping json login authenticator. No content in body. May you forget to add content or you installed a custom authenticator on same path?', ['exception' => $contentType, 'authenticator' => static::class]);
}
}
}

0 comments on commit 64f06ee

Please sign in to comment.