0

I'm trying to create a custom authenticator and I followed the tutorial here:

http://symfony.com/doc/current/cookbook/security/custom_password_authenticator.html

But I'm getting this error:

ServiceNotFoundException: The service "security.authentication.manager" has a dependency on a non-existent service "security.user.provider.concrete.authenticator".

I checked my version and I'm using 2.4, so thats not the issue but it seems like a symfony core class is missing?

Security.yml

security:
    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            pattern:    ^/
            provider: authenticator
            anonymous:  ~
            simple-form:
            login_path: snap_front_login
                check_path: login_check
                authenticator: SnapsavedAuthenticator

    encoders:
        Snap\RestBundle\entity\User: plaintext

providers:
    user_entity:
        id: SnapsavedUserprovider


    role_hierarchy:
        ROLE_USER:        ROLE_USER
        ROLE_ADMIN:       ROLE_ADMIN
        ROLE_SUPER_ADMIN: [ROLE_SUPER_ADMIN, ROLE_ALLOWED_TO_SWITCH ]

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, roles: ROLE_USER }

Authenticator

namespace Snap\ModelBundle\Security;

use Symfony\Component\Security\Core\Authentication\SimpleFormAuthenticatorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\HttpFoundation\Request;


class SnapsavedAuthenticator implements SimpleFormAuthenticatorInterface {

   private $encoderFactory;

    public function __construct(EncoderFactoryInterface $encoderFactory)
    {
        $this->encoderFactory = $encoderFactory;
    }

    public function createToken(Request $request, $username, $password, $providerKey)
    {
        return new UsernamePasswordToken($username, $password, $providerKey);
    }

    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
    {
        try {
            $user = $userProvider->loadUserByUsername($token->getUsername());
        } catch (UsernameNotFoundException $e) {
            throw new AuthenticationException('Invalid username or password');
        }

        $passwordValid = $this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $token->getCredentials(), $user->getSalt());

        if ($passwordValid) {
            $currentHour = date('G');
            if ($currentHour < 14 || $currentHour > 16) {
                throw new AuthenticationException('You can only log in between 2 and 4!', 100);
            }

            return new UsernamePasswordToken($user, 'bar', $providerKey, $user->getRoles());
        }

        throw new AuthenticationException('Invalid username or password');
    }

    public function supportsToken(TokenInterface $token, $providerKey)
    {
        return $token instanceof UsernamePasswordToken && $token->getProviderKey() === $providerKey;
    }
}
2
  • Can you show us your code please? (config.yml, security.yml and your Authenticator) Commented Dec 24, 2013 at 14:45
  • The authenticator is a replicate of the one in the tutorial, but here it is anyway Commented Dec 25, 2013 at 1:44

1 Answer 1

5

I think you're mixing two things here. The user provider and the authenticator.

In your security.yml you tell your firewall to use the provider provider: authenticator but in your providers section you have only the following provider : user_entity so your user provider should be user_entity.

If you want to learn more about the providers i suggest you to read this : http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch, you are correct! I wish i could give you more upvotes, this one has been bugging me for quite some time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.