@@ -8,13 +8,17 @@ Imagine you want to allow access to your website only between 2pm and 4pm
8
8
UTC. Before Symfony 2.4, you had to create a custom token, factory, listener
9
9
and provider. In this entry, you'll learn how to do this for a login form
10
10
(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.
11
12
12
13
The Password Authenticator
13
14
--------------------------
14
15
15
16
.. versionadded :: 2.4
16
17
The ``SimpleFormAuthenticatorInterface `` interface was introduced in Symfony 2.4.
17
18
19
+ .. versionadded :: 2.6
20
+ The ``UserPasswordEncoderInterface `` interface was introduced in Symfony 2.6.
21
+
18
22
First, create a new class that implements
19
23
:class: `Symfony\\ Component\\ Security\\ Core\\ Authentication\\ SimpleFormAuthenticatorInterface `.
20
24
Eventually, this will allow you to create custom logic for authenticating
@@ -27,18 +31,18 @@ the user::
27
31
use Symfony\Component\Security\Core\Authentication\SimpleFormAuthenticatorInterface;
28
32
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
29
33
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
30
- use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface ;
34
+ use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface ;
31
35
use Symfony\Component\Security\Core\Exception\AuthenticationException;
32
36
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
33
37
use Symfony\Component\Security\Core\User\UserProviderInterface;
34
38
35
39
class TimeAuthenticator implements SimpleFormAuthenticatorInterface
36
40
{
37
- private $encoderFactory ;
41
+ private $encoder ;
38
42
39
- public function __construct(EncoderFactoryInterface $encoderFactory )
43
+ public function __construct(UserPasswordEncoderInterface $encoder )
40
44
{
41
- $this->encoderFactory = $encoderFactory ;
45
+ $this->encoder = $encoder ;
42
46
}
43
47
44
48
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
@@ -49,12 +53,7 @@ the user::
49
53
throw new AuthenticationException('Invalid username or password');
50
54
}
51
55
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());
58
57
59
58
if ($passwordValid) {
60
59
$currentHour = date('G');
@@ -127,17 +126,12 @@ Ultimately, your job is to return a *new* token object that is "authenticated"
127
126
(i.e. it has at least 1 role set on it) and which has the ``User `` object
128
127
inside of it.
129
128
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::
131
130
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());
138
132
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 and it uses the password algorithm
134
+ that is configured in the security configuration (e.g. ``security.yml ``) under
141
135
the ``encoders `` key. Below, you'll see how to inject that into the ``TimeAuthenticator ``.
142
136
143
137
.. _cookbook-security-password-authenticator-config :
@@ -157,7 +151,7 @@ Now, configure your ``TimeAuthenticator`` as a service:
157
151
158
152
time_authenticator :
159
153
class : Acme\HelloBundle\Security\TimeAuthenticator
160
- arguments : ["@security.encoder_factory "]
154
+ arguments : ["@security.password_encoder "]
161
155
162
156
.. code-block :: xml
163
157
@@ -173,7 +167,7 @@ Now, configure your ``TimeAuthenticator`` as a service:
173
167
<service id =" time_authenticator"
174
168
class =" Acme\HelloBundle\Security\TimeAuthenticator"
175
169
>
176
- <argument type =" service" id =" security.encoder_factory " />
170
+ <argument type =" service" id =" security.password_encoder " />
177
171
</service >
178
172
</services >
179
173
</container >
@@ -188,7 +182,7 @@ Now, configure your ``TimeAuthenticator`` as a service:
188
182
189
183
$container->setDefinition('time_authenticator', new Definition(
190
184
'Acme\HelloBundle\Security\TimeAuthenticator',
191
- array(new Reference('security.encoder_factory '))
185
+ array(new Reference('security.password_encoder '))
192
186
));
193
187
194
188
Then, activate it in the ``firewalls `` section of the security configuration
0 commit comments