@@ -95,6 +95,8 @@ focus on the most important methods that come from the
95
95
public function __construct()
96
96
{
97
97
$this->isActive = true;
98
+ // may not be needed, see section on salt below
99
+ // $this->salt = md5(uniqid(null, true));
98
100
}
99
101
100
102
/**
@@ -110,6 +112,8 @@ focus on the most important methods that come from the
110
112
*/
111
113
public function getSalt()
112
114
{
115
+ // you *may* need a real salt depending on your encoder
116
+ // see section on salt below
113
117
return null;
114
118
}
115
119
@@ -144,8 +148,9 @@ focus on the most important methods that come from the
144
148
return serialize(array(
145
149
$this->id,
146
150
$this->username,
147
- $this->salt,
148
151
$this->password,
152
+ // see section on salt below
153
+ // $this->salt,
149
154
));
150
155
}
151
156
@@ -157,19 +162,13 @@ focus on the most important methods that come from the
157
162
list (
158
163
$this->id,
159
164
$this->username,
160
- $this->salt,
161
165
$this->password,
166
+ // see section on salt below
167
+ // $this->salt
162
168
) = unserialize($serialized);
163
169
}
164
170
}
165
171
166
- .. note ::
167
-
168
- If you choose to implement
169
- :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ EquatableInterface `,
170
- you determine yourself which properties need to be compared to distinguish
171
- your user objects.
172
-
173
172
.. tip ::
174
173
175
174
:ref: `Generate the database table <book-doctrine-creating-the-database-tables-schema >`
@@ -186,7 +185,7 @@ interface forces the class to implement the five following methods:
186
185
187
186
* ``getRoles() ``,
188
187
* ``getPassword() ``,
189
- * ``getPassword () ``,
188
+ * ``getSalt () ``,
190
189
* ``getUsername() ``,
191
190
* ``eraseCredentials() ``
192
191
@@ -213,6 +212,20 @@ The next part will focus on how to authenticate one of these users
213
212
thanks to the Doctrine entity user provider and a couple of lines of
214
213
configuration.
215
214
215
+ .. sidebar:: Do you need to use a Salt?
216
+
217
+ Yes. Hashing a password with a salt is a necessary step so that encoded
218
+ passwords can' t be decoded. However, some encoders - like Bcrypt - have
219
+ a built-in salt mechanism. If you configure ``bcrypt`` as your encoder
220
+ in ``security.yml`` (see the next section), then ``getSalt()`` should
221
+ return ``null``, so that Bcrypt generates the salt itself.
222
+
223
+ However, if you use an encoder that does *not* have a built-in salting
224
+ ability (e.g. ``sha512``), you *must* (from a security perspective) generate
225
+ your own, random salt, store it on a ``salt`` property that is saved to
226
+ the database, and return it from ``getSalt()``. Some of the code needed
227
+ is commented out in the above example.
228
+
216
229
Authenticating Someone against a Database
217
230
-----------------------------------------
218
231
@@ -311,6 +324,8 @@ the database to be encoded using this encoder. For details on how to create
311
324
a new User object with a properly encoded password, see the
312
325
:ref:` book-security-encoding-user-password` section of the security chapter.
313
326
327
+ .. include:: /cookbook/security/_ircmaxwell_password-compat.rst.inc
328
+
314
329
The ` ` providers` ` section defines an ` ` administrators` ` user provider. A
315
330
user provider is a " source" of where users are loaded during authentication.
316
331
In this case, the ` ` entity` ` keyword means that Symfony will use the Doctrine
0 commit comments