Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 Custom Authenticator
    text
    copied!<p>I'm trying to create a custom authenticator and I followed the tutorial here:</p> <p><a href="http://symfony.com/doc/current/cookbook/security/custom_password_authenticator.html" rel="nofollow">http://symfony.com/doc/current/cookbook/security/custom_password_authenticator.html</a></p> <p>But I'm getting this error:</p> <pre><code>ServiceNotFoundException: The service "security.authentication.manager" has a dependency on a non-existent service "security.user.provider.concrete.authenticator". </code></pre> <p>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?</p> <p><strong>Security.yml</strong></p> <pre><code>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 } </code></pre> <p><strong>Authenticator</strong></p> <pre><code>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-&gt;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-&gt;loadUserByUsername($token-&gt;getUsername()); } catch (UsernameNotFoundException $e) { throw new AuthenticationException('Invalid username or password'); } $passwordValid = $this-&gt;encoderFactory-&gt;getEncoder($user)-&gt;isPasswordValid($user-&gt;getPassword(), $token-&gt;getCredentials(), $user-&gt;getSalt()); if ($passwordValid) { $currentHour = date('G'); if ($currentHour &lt; 14 || $currentHour &gt; 16) { throw new AuthenticationException('You can only log in between 2 and 4!', 100); } return new UsernamePasswordToken($user, 'bar', $providerKey, $user-&gt;getRoles()); } throw new AuthenticationException('Invalid username or password'); } public function supportsToken(TokenInterface $token, $providerKey) { return $token instanceof UsernamePasswordToken &amp;&amp; $token-&gt;getProviderKey() === $providerKey; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload