0
votes

I am running into the error:

The class 'App\Entity\User' was not found in the chain configured namespaces

I am running Symfony 4.2 with API Platform. I need to create an API token/key authentication setup and am using the guard authenticator.

Entity:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\Table(name="arc_sync_api_keys")
 */
class User implements UserInterface
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    public $id;

    /** @ORM\Column(length=20) */
    public $username;

    /** @ORM\Column(name="api_token", length=40) */
    public $apiKey;

    /** @ORM\Column(length=30) */
    public $roles = [];

    public function getUsername(): string
    {
        return $this->username;
    }

    public function getRoles(): array
    {
        return array('ROLE_USER');
    }

    public function getPassword()
    {
    }
    public function getSalt()
    {
    }
    public function eraseCredentials()
    {
    }
}

Authenticator:

namespace App\Security;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;

class ApiKeyAuthenticator extends AbstractGuardAuthenticator
{
    ...

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $apiKey = $credentials['token'];

        if (null === $apiKey) {
            return;
        }



        // if a User object, checkCredentials() is called
        return $userProvider->loadUserByUsername($apiKey);
    }

    ...
}

security.yaml

security:
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\User
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: ~
            logout: ~

            guard:
                authenticators:
                    - App\Security\ApiKeyAuthenticator

The error happens here:

return $userProvider->loadUserByUsername($apiKey);

after following it through it fails to load the driver, but I do not know how to fix this issue. Thanks!

1

1 Answers

0
votes

I had a same problem when i'm working with multi entity manager. A exception like AuthenticationServiceException App\Entity\User' was not found in the chain ..

This exception was only in production env.

I solved it by adding a default_entity_manager :

# config/packages/prod/doctrine.yaml
doctrine:
    orm:
        default_entity_manager: 'your default entity manager name'

# ...