vendor/scheb/2fa-bundle/Security/Authentication/AuthenticationTrustResolver.php line 27

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Scheb\TwoFactorBundle\Security\Authentication;
  4. use RuntimeException;
  5. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
  6. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use function method_exists;
  9. /**
  10.  * @final
  11.  */
  12. class AuthenticationTrustResolver implements AuthenticationTrustResolverInterface
  13. {
  14.     public function __construct(private AuthenticationTrustResolverInterface $decoratedTrustResolver)
  15.     {
  16.     }
  17.     /**
  18.      * Compatibility with Symfony < 6.0.
  19.      *
  20.      * @deprecated since Symfony 5.4, use !isAuthenticated() instead
  21.      */
  22.     public function isAnonymous(?TokenInterface $token null): bool
  23.     {
  24.         if (!method_exists($this->decoratedTrustResolver'isAnonymous')) {
  25.             throw new RuntimeException('Method "isAnonymous" was not declared on the decorated AuthenticationTrustResolverInterface');
  26.         }
  27.         return $this->decoratedTrustResolver->isAnonymous($token);
  28.     }
  29.     public function isRememberMe(?TokenInterface $token null): bool
  30.     {
  31.         return $this->decoratedTrustResolver->isRememberMe($token);
  32.     }
  33.     public function isFullFledged(?TokenInterface $token null): bool
  34.     {
  35.         return !$this->isTwoFactorToken($token) && $this->decoratedTrustResolver->isFullFledged($token);
  36.     }
  37.     /**
  38.      * Compatibility for Symfony >= 5.4.
  39.      */
  40.     public function isAuthenticated(?TokenInterface $token null): bool
  41.     {
  42.         // The "isAuthenticated" method must be declared in Symfony >= 6.0
  43.         if (method_exists($this->decoratedTrustResolver'isAuthenticated')) {
  44.             return $this->decoratedTrustResolver->isAuthenticated($token);
  45.         }
  46.         // Fallback for Symfony 5.4, when "isAuthenticated" is not declared, use the deprecated "isAnonymous" method
  47.         if (method_exists($this->decoratedTrustResolver'isAnonymous')) {
  48.             return !$this->decoratedTrustResolver->isAnonymous($token);
  49.         }
  50.         // This should never happen on Symfony 5.4 or 6.x versions, either "isAuthenticated" or "isAnonymous" must be declared
  51.         throw new RuntimeException('Neither method "isAuthenticated" nor "isAnonymous" was declared on the decorated AuthenticationTrustResolverInterface');
  52.     }
  53.     private function isTwoFactorToken(?TokenInterface $token): bool
  54.     {
  55.         return $token instanceof TwoFactorTokenInterface;
  56.     }
  57. }