vendor/symfony/security-http/Authenticator/Passport/Credentials/CustomCredentials.php line 24

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Http\Authenticator\Passport\Credentials;
  11. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. /**
  14.  * Implements credentials checking using a custom checker function.
  15.  *
  16.  * @author Wouter de Jong <[email protected]>
  17.  *
  18.  * @final
  19.  */
  20. class CustomCredentials implements CredentialsInterface
  21. {
  22.     private \Closure $customCredentialsChecker;
  23.     private mixed $credentials;
  24.     private bool $resolved false;
  25.     /**
  26.      * @param callable $customCredentialsChecker the check function. If this function does not return `true`, a
  27.      *                                           BadCredentialsException is thrown. You may also throw a more
  28.      *                                           specific exception in the function.
  29.      */
  30.     public function __construct(callable $customCredentialsCheckermixed $credentials)
  31.     {
  32.         $this->customCredentialsChecker $customCredentialsChecker instanceof \Closure $customCredentialsChecker \Closure::fromCallable($customCredentialsChecker);
  33.         $this->credentials $credentials;
  34.     }
  35.     public function executeCustomChecker(UserInterface $user): void
  36.     {
  37.         $checker $this->customCredentialsChecker;
  38.         if (true !== $checker($this->credentials$user)) {
  39.             throw new BadCredentialsException('Credentials check failed as the callable passed to CustomCredentials did not return "true".');
  40.         }
  41.         $this->resolved true;
  42.     }
  43.     public function isResolved(): bool
  44.     {
  45.         return $this->resolved;
  46.     }
  47. }