Unexpected AuthenticationException

318 views Asked by At

I just ended to set up my own UserProvider, but since this new code, at the login I get instantly disconnected beacause of an unexpected Authentication Exception. It seems to be a Token system problem (maybe I'm wrong).

// Errors context & trace

INFO
10:18:53
security    An AuthenticationException was thrown; redirecting to authentication entry point.
[▼
  "exception" => AuthenticationExpiredException {#579 ▼
    -user: null
    -token: PostAuthenticationGuardToken {#300 …}
        #message: ""
        #code: 0
        #file: "C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\Security\Guard\Provider\GuardAuthenticationProvider.php"
        #line: 86
        trace: {▶}
    }
]
{▼
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\Security\Guard\Provider\GuardAuthenticationProvider.php:86: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager.php:78: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authorization\AuthorizationChecker.php:61: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Bridge\Twig\Extension\SecurityExtension.php:45: {▶}
  C:\xampp\htdocs\underthemist\src\UTM\CoreBundle/Resources/views/Common/header.html.twig:15: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:389: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:366: {▶}
  C:\xampp\htdocs\underthemist\src\UTM\CoreBundle/Resources/views/Common/base.html.twig:16: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:389: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:366: {▶}
  C:\xampp\htdocs\underthemist\var\cache\dev\twig\71\71a9b9c106a0ccc28104344f49741ecbe4e58d1ff29986bcdf9989bf268c390e.php:30: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:389: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:366: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:374: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Bridge\Twig\TwigEngine.php:49: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\TwigEngine.php:45: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\TwigEngine.php:72: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait.php:234: {▶}
  C:\xampp\htdocs\underthemist\src\UTM\CoreBundle\Controller\CoreController.php:11: {▶}
  UTM\CoreBundle\Controller\CoreController->indexAction(): {}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:153: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:68: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php:171: {▶}
  C:\xampp\htdocs\underthemist\web\app_dev.php:30: {▶}
}

INFO
10:18:53
security    The security token was removed due to an AccountStatusException.
[▼
"exception" => AuthenticationExpiredException {#579 ▼
    -user: null
    -token: PostAuthenticationGuardToken {#300 …}
        #message: ""
        #code: 0
        #file: "C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\Security\Guard\Provider\GuardAuthenticationProvider.php"
        #line: 86
        trace: {▶}
     }
]
{▼
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\Security\Guard\Provider\GuardAuthenticationProvider.php:86: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager.php:78: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authorization\AuthorizationChecker.php:61: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Bridge\Twig\Extension\SecurityExtension.php:45: {▶}
  C:\xampp\htdocs\underthemist\src\UTM\CoreBundle/Resources/views/Common/header.html.twig:15: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:389: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:366: {▶}
  C:\xampp\htdocs\underthemist\src\UTM\CoreBundle/Resources/views/Common/base.html.twig:16: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:389: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:366: {▶}
  C:\xampp\htdocs\underthemist\var\cache\dev\twig\71\71a9b9c106a0ccc28104344f49741ecbe4e58d1ff29986bcdf9989bf268c390e.php:30: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:389: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:366: {▶}
  C:\xampp\htdocs\underthemist\vendor\twig\twig\lib\Twig\Template.php:374: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Bridge\Twig\TwigEngine.php:49: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\TwigEngine.php:45: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\TwigEngine.php:72: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait.php:234: {▶}
  C:\xampp\htdocs\underthemist\src\UTM\CoreBundle\Controller\CoreController.php:11: {▶}
  UTM\CoreBundle\Controller\CoreController->indexAction(): {}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:153: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:68: {▶}
  C:\xampp\htdocs\underthemist\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php:171: {▶}
  C:\xampp\htdocs\underthemist\web\app_dev.php:30: {▶}
}

Here is my code

// UserProvider

<?php

namespace UTM\UserBundle\Security;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use UTM\UserBundle\Entity\User;

class UserProvider implements UserProviderInterface
{
    private $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function loadUserByUsername($username)
    {
        $userRepository = $this->em->getRepository(User::class);

        $user = $userRepository->loadUserByUsername($username);

        dump($user);
        if ($user) {
            return $user;
        }

        throw new UsernameNotFoundException(
            sprintf('Username "%s" does not exist.', $username)
        );
    }

    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof User) {
            throw new UnsupportedUserException(
                sprintf('Instances of "%s" are not supported.', get_class($user))
            );
        }

        return $this->loadUserByUsername($user->getId());
    }

    public function supportsClass($class)
    {
        return User::class === $class;
    }

}

// UserRepository

<?php

namespace UTM\UserBundle\Repository;

use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class UserRepository extends EntityRepository implements UserLoaderInterface
{

    public function loadUserByUsername($username)
    {
        return $this->createQueryBuilder('u')
            ->select('u, g')
            ->leftJoin('u.group', 'g')
            ->where('u.id = :id')
            ->setParameter('id', $username)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }

    public function getUserWithNoDashesUuid($uuid)
    {
        return $this->findOneBy([
            'uuid' => preg_replace('#^(.{8})(.{4})(.{4})(.{4})(.{12})$#', '$1-$2-$3-$4-$5', $uuid)
        ]);
    }
}

// User

<?php

namespace UTM\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints\DateTime;
use UTM\UserBundle\Entity\Group;

/**
 * User
 *
 * @ORM\Table(name="utm_users")
 * @ORM\Entity(repositoryClass="UTM\UserBundle\Repository\UserRepository")
 */
class User implements UserInterface, EquatableInterface
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="uuid", type="string", length=36, nullable=false)
     */
    private $uuid;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=16, nullable=false)
     */
    private $username;

    /**
     * @var integer
     *
     * @ORM\Column(name="money", type="integer", nullable=false)
     */
    private $money = 0;

    /**
     * @var integer
     *
     * @ORM\Column(name="bank_money", type="integer", nullable=false)
     */
    private $bankMoney = 0;

    /**
     * @var integer
     *
     * @ORM\Column(name="home_quantity", type="integer", nullable=false)
     */
    private $homeQuantity = 1;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="last_login", type="datetime", nullable=true)
     */
    private $lastLogin;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="website_activity", type="datetime", nullable=true)
     */
    private $websiteActivity;

    /**
     * @var string
     *
     * @ORM\Column(name="client_token", type="string", length=36, nullable=true)
     */
    private $clientToken;


    /**
     * @var string
     *
     * @ORM\Column(name="access_token", type="string", length=200, nullable=true)
     */
    private $accessToken;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="mojang_data_check", type="datetime", nullable=true)
     */
    private $mojangDataCheck;

    /**
     * @var string
     *
     * @ORM\Column(name="reputation", type="integer", nullable=false)
     */
    private $reputation = 0;

    /**
     * @var Group
     *
     * @ORM\ManyToOne(targetEntity="Group")
     */
    private $group;

    public function __construct() {
        $this->mojangDataCheck = new \DateTime();
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set uuid
     *
     * @param string $uuid
     *
     * @return User
     */
    public function setUuid($uuid)
    {
        $this->uuid = $uuid;

        return $this;
    }

    public function setNoDashesUuid($uuid)
    {
        $this->uuid = preg_replace('#^(.{8})(.{4})(.{4})(.{4})(.{12})$#', '$1-$2-$3-$4-$5', $uuid);

        return $this;
    }

    /**
     * Get uuid
     *
     * @return string
     */
    public function getUuid()
    {
        return $this->uuid;
    }

    public function getNoDashesUuid()
    {
        return str_replace('-', '', $this->uuid);
    }

    /**
     * Set username
     *
     * @param string $username
     *
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Get username
     *
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set money
     *
     * @param integer $money
     *
     * @return User
     */
    public function setMoney($money)
    {
        $this->money = $money;

        return $this;
    }

    /**
     * Get money
     *
     * @return integer
     */
    public function getMoney()
    {
        return $this->money;
    }

    /**
     * Set bankMoney
     *
     * @param integer $bankMoney
     *
     * @return User
     */
    public function setBankMoney($bankMoney)
    {
        $this->bankMoney = $bankMoney;

        return $this;
    }

    /**
     * Get bankMoney
     *
     * @return integer
     */
    public function getBankMoney()
    {
        return $this->bankMoney;
    }

    /**
     * Set homeQuantity
     *
     * @param integer $homeQuantity
     *
     * @return User
     */
    public function setHomeQuantity($homeQuantity)
    {
        $this->homeQuantity = $homeQuantity;

        return $this;
    }

    /**
     * Get homeQuantity
     *
     * @return integer
     */
    public function getHomeQuantity()
    {
        return $this->homeQuantity;
    }

    /**
     * Set lastLogin
     *
     * @param \DateTime $lastLogin
     *
     * @return User
     */
    public function setLastLogin(DateTime $lastLogin)
    {
        $this->lastLogin = $lastLogin;

        return $this;
    }

    /**
     * Get lastLogin
     *
     * @return \DateTime
     */
    public function getLastLogin()
    {
        return $this->lastLogin;
    }

    /**
     * Set websiteActivity
     *
     * @param \DateTime $websiteActivity
     *
     * @return User
     */
    public function setWebsiteActivity($websiteActivity)
    {
        $this->websiteActivity = $websiteActivity;

        return $this;
    }

    /**
     * Get websiteActivity
     *
     * @return \DateTime
     */
    public function getWebsiteActivity()
    {
        return $this->websiteActivity;
    }

    /**
     * @return string
     */
    public function getClientToken()
    {
        return $this->clientToken;
    }

    /**
     * @param string $clientToken
     */
    public function setClientToken($clientToken)
    {
        $this->clientToken = $clientToken;

        return $this;
    }

    /**
     * @return string
     */
    public function getAccessToken()
    {
        return $this->accessToken;
    }

    /**
     * @param string $accessToken
     */
    public function setAccessToken($accessToken)
    {
        $this->accessToken = $accessToken;

        return $this;
    }



    /**
     * Set mojangDataCheck
     *
     * @param \DateTime $mojangDataCheck
     *
     * @return User
     */
    public function setMojangDataCheck()
    {
        $this->mojangDataCheck = new \DateTime();

        return $this;
    }

    public function mojangDataCheck()
    {
        $date = new \DateTime();
        $this->mojangDataCheck = $date->add(new \DateInterval('PT1H'));

        return $this;
    }
    /**
     * Get mojangDataCheck
     *
     * @return \DateTime
     */
    public function getMojangDataCheck()
    {
        return $this->mojangDataCheck;
    }

    /**
     * Set group
     *
     * @param \UTM\UserBundle\Entity\Group $group
     *
     * @return User
     */
    public function setGroup(Group $group)
    {
        $this->group = $group;

        return $this;
    }

    /**
     * Get group
     *
     * @return \UTM\UserBundle\Entity\Group
     */
    public function getGroup()
    {
        return $this->group;
    }

    /**
     * @return string
     */
    public function getReputation()
    {
        return $this->reputation;
    }

    /**
     * @param string $reputation
     */
    public function setReputation($reputation)
    {
        $this->reputation = $reputation;
    }


    public function getReputationRank()
    {
        $reputation = $this->getReputation();
        for ($level = 1; $level < 100; ++$level) {
            $reputation -= $level * 100;
            if ($reputation < 0) {
                return $level;
            }
        }
        return 100;
    }

    /**
     * @param int $id
     */
    public function getRequiredReputation()
    {
        $reputation = $this->getReputation();
        for ($level = 1; $level < 100; ++$level) {
            $reputation -= $level * 100;
            if ($reputation < 0) {
                return -$reputation;
            }
        }
        return -1;
    }

    public function getRoles()
    {
        // TODO: Implement getRoles() method.
        return ['ROLE_USER'];
    }

    public function getPassword()
    {
    }

    public function getSalt()
    {
    }

    public function eraseCredentials()
    {
    }

    public function isEqualTo(UserInterface $user)
    {
        if (!$user instanceof User) {
            return false;
        }

        if ($this->id !== $user->id) {
            return false;
        }

        if ($this->uuid !== $user->uuid) {
            return false;
        }
    }
}

// services.yml

# To get started with security, check out the documentation:
# https://symfony.com/doc/current/security.html
security:

    # https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
    providers:
        webservice:
            id: UTM\UserBundle\Security\UserProvider

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            pattern: ^/
            anonymous: true
            guard:
                authenticators:
                 - UTM\UserBundle\Security\LoginFormAuthenticator
            logout:
                path: /user/logout
            remember_me:
                secret: '%secret%'
                lifetime: 2592000
            # activate different ways to authenticate

            # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
            #http_basic: ~

            # https://symfony.com/doc/current/security/form_login_setup.html
            #form_login: ~

// security.yml

# To get started with security, check out the documentation:
# https://symfony.com/doc/current/security.html
security:

    # https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
    providers:
        webservice:
            id: UTM\UserBundle\Security\UserProvider

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            pattern: ^/
            anonymous: true
            guard:
                authenticators:
                 - UTM\UserBundle\Security\LoginFormAuthenticator
            logout:
                path: /user/logout
            remember_me:
                secret: '%secret%'
                lifetime: 2592000
            # activate different ways to authenticate

            # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
            #http_basic: ~

            # https://symfony.com/doc/current/security/form_login_setup.html
            #form_login: ~

I don't know how to fix it and I'm stuck for hours on this hours, It would be really nice to help me. Thank you !

0

There are 0 answers