@@ -8,13 +8,17 @@ Imagine you want to allow access to your website only between 2pm and 4pm
88UTC. Before Symfony 2.4, you had to create a custom token, factory, listener
99and provider. In this entry, you'll learn how to do this for a login form
1010(i.e. where your user submits their username and password).
11+ Before Symfony 2.6, you had to use the password encoder to authenticate the user password.
1112
1213The Password Authenticator
1314--------------------------
1415
1516..versionadded ::2.4
1617 The ``SimpleFormAuthenticatorInterface `` interface was introduced in Symfony 2.4.
1718
19+ ..versionadded ::2.6
20+ The ``UserPasswordEncoderInterface `` interface was introduced in Symfony 2.6.
21+
1822First, create a new class that implements
1923:class: `Symfony\\ Component\\ Security\\ Core\\ Authentication\\ SimpleFormAuthenticatorInterface `.
2024Eventually, this will allow you to create custom logic for authenticating
@@ -27,18 +31,18 @@ the user::
2731 use Symfony\Component\Security\Core\Authentication\SimpleFormAuthenticatorInterface;
2832 use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2933 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
30- use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface ;
34+ use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface ;
3135 use Symfony\Component\Security\Core\Exception\AuthenticationException;
3236 use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
3337 use Symfony\Component\Security\Core\User\UserProviderInterface;
3438
3539 class TimeAuthenticator implements SimpleFormAuthenticatorInterface
3640 {
37- private $encoderFactory ;
41+ private $encoder ;
3842
39- public function __construct(EncoderFactoryInterface $encoderFactory )
43+ public function __construct(UserPasswordEncoderInterface $encoder )
4044 {
41- $this->encoderFactory = $encoderFactory ;
45+ $this->encoder = $encoder ;
4246 }
4347
4448 public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
@@ -49,12 +53,7 @@ the user::
4953 throw new AuthenticationException('Invalid username or password');
5054 }
5155
52- $encoder = $this->encoderFactory->getEncoder($user);
53- $passwordValid = $encoder->isPasswordValid(
54- $user->getPassword(),
55- $token->getCredentials(),
56- $user->getSalt()
57- );
56+ $passwordValid = $this->encoder->isPasswordValid($user, $token->getCredentials());
5857
5958 if ($passwordValid) {
6059 $currentHour = date('G');
@@ -127,17 +126,12 @@ Ultimately, your job is to return a *new* token object that is "authenticated"
127126(i.e. it has at least 1 role set on it) and which has the ``User `` object
128127inside of it.
129128
130- Inside this method,an encoder is needed to check the password's validity::
129+ Inside this method,the password encoder is needed to check the password's validity::
131130
132- $encoder = $this->encoderFactory->getEncoder($user);
133- $passwordValid = $encoder->isPasswordValid(
134- $user->getPassword(),
135- $token->getCredentials(),
136- $user->getSalt()
137- );
131+ $passwordValid = $this->encoder->isPasswordValid($user, $token->getCredentials());
138132
139- This is a service that is already available in Symfony and the password algorithm
140- is configured in the security configuration (e.g. ``security.yml ``) under
133+ This is a service that is already available in Symfony andit uses the password algorithm
134+ that is configured in the security configuration (e.g. ``security.yml ``) under
141135the ``encoders `` key. Below, you'll see how to inject that into the ``TimeAuthenticator ``.
142136
143137.. _cookbook-security-password-authenticator-config :
@@ -157,7 +151,7 @@ Now, configure your ``TimeAuthenticator`` as a service:
157151
158152time_authenticator :
159153class :Acme\HelloBundle\Security\TimeAuthenticator
160- arguments :["@security.encoder_factory "]
154+ arguments :["@security.password_encoder "]
161155
162156 ..code-block ::xml
163157
@@ -173,7 +167,7 @@ Now, configure your ``TimeAuthenticator`` as a service:
173167 <service id =" time_authenticator"
174168class =" Acme\HelloBundle\Security\TimeAuthenticator"
175169 >
176- <argument type =" service" id =" security.encoder_factory " />
170+ <argument type =" service" id =" security.password_encoder " />
177171 </service >
178172 </services >
179173 </container >
@@ -188,7 +182,7 @@ Now, configure your ``TimeAuthenticator`` as a service:
188182
189183 $container->setDefinition('time_authenticator', new Definition(
190184 'Acme\HelloBundle\Security\TimeAuthenticator',
191- array(new Reference('security.encoder_factory '))
185+ array(new Reference('security.password_encoder '))
192186 ));
193187
194188 Then, activate it in the ``firewalls `` section of the security configuration