Skip to content

Commit 4904db8

Browse files
committed
Updated article for modern Symfony practices and the use of bcrypt
1 parent ea2503c commit 4904db8

File tree

1 file changed

+38
-43
lines changed

1 file changed

+38
-43
lines changed

cookbook/security/custom_provider.rst

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ method.
3535

3636
This is how your ``WebserviceUser`` class looks in action::
3737

38-
// src/Acme/WebserviceUserBundle/Security/User/WebserviceUser.php
39-
namespace Acme\WebserviceUserBundle\Security\User;
38+
// src/AppBundle/Security/User/WebserviceUser.php
39+
namespace AppBundle\Security\User;
4040

4141
use Symfony\Component\Security\Core\User\UserInterface;
4242
use Symfony\Component\Security\Core\User\EquatableInterface;
@@ -120,8 +120,8 @@ more details, see :class:`Symfony\\Component\\Security\\Core\\User\\UserProvider
120120

121121
Here's an example of how this might look::
122122

123-
// src/Acme/WebserviceUserBundle/Security/User/WebserviceUserProvider.php
124-
namespace Acme\WebserviceUserBundle\Security\User;
123+
// src/AppBundle/Security/User/WebserviceUserProvider.php
124+
namespace AppBundle\Security\User;
125125

126126
use Symfony\Component\Security\Core\User\UserProviderInterface;
127127
use Symfony\Component\Security\Core\User\UserInterface;
@@ -162,7 +162,7 @@ Here's an example of how this might look::
162162

163163
public function supportsClass($class)
164164
{
165-
return $class === 'Acme\WebserviceUserBundle\Security\User\WebserviceUser';
165+
return $class === 'AppBundle\Security\User\WebserviceUser';
166166
}
167167
}
168168

@@ -177,8 +177,8 @@ Now you make the user provider available as a service:
177177
178178
# app/config/services.yml
179179
services:
180-
webservice_user_provider:
181-
class: Acme\WebserviceUserBundle\Security\User\WebserviceUserProvider
180+
app.webservice_user_provider:
181+
class: AppBundle\Security\User\WebserviceUserProvider
182182
183183
.. code-block:: xml
184184
@@ -190,8 +190,8 @@ Now you make the user provider available as a service:
190190
http://symfony.com/schema/dic/services/services-1.0.xsd">
191191
192192
<services>
193-
<service id="webservice_user_provider"
194-
class="Acme\WebserviceUserBundle\Security\User\WebserviceUserProvider"
193+
<service id="app.webservice_user_provider"
194+
class="AppBundle\Security\User\WebserviceUserProvider"
195195
/>
196196
</services>
197197
</container>
@@ -202,8 +202,8 @@ Now you make the user provider available as a service:
202202
use Symfony\Component\DependencyInjection\Definition;
203203
204204
$container->setDefinition(
205-
'webservice_user_provider',
206-
new Definition('Acme\WebserviceUserBundle\Security\User\WebserviceUserProvider')
205+
'app.webservice_user_provider',
206+
new Definition('AppBundle\Security\User\WebserviceUserProvider')
207207
);
208208
209209
.. tip::
@@ -222,7 +222,7 @@ Modify ``security.yml``
222222

223223
Everything comes together in your security configuration. Add the user provider
224224
to the list of providers in the "security" section. Choose a name for the user provider
225-
(e.g. "webservice") and mention the id of the service you just defined.
225+
(e.g. "webservice") and mention the ``id`` of the service you just defined.
226226

227227
.. configuration-block::
228228

@@ -234,7 +234,7 @@ to the list of providers in the "security" section. Choose a name for the user p
234234
235235
providers:
236236
webservice:
237-
id: webservice_user_provider
237+
id: app.webservice_user_provider
238238
239239
.. code-block:: xml
240240
@@ -249,7 +249,7 @@ to the list of providers in the "security" section. Choose a name for the user p
249249
<config>
250250
<!-- ... -->
251251
252-
<provider name="webservice" id="webservice_user_provider" />
252+
<provider name="webservice" id="app.webservice_user_provider" />
253253
</config>
254254
</srv:container>
255255
@@ -261,7 +261,7 @@ to the list of providers in the "security" section. Choose a name for the user p
261261
262262
'providers' => array(
263263
'webservice' => array(
264-
'id' => 'webservice_user_provider',
264+
'id' => 'app.webservice_user_provider',
265265
),
266266
),
267267
));
@@ -279,7 +279,7 @@ users, e.g. by filling in a login form. You can do this by adding a line to the
279279
# ...
280280
281281
encoders:
282-
Acme\WebserviceUserBundle\Security\User\WebserviceUser: sha512
282+
AppBundle\Security\User\WebserviceUser: bcrypt
283283
284284
.. code-block:: xml
285285
@@ -294,9 +294,8 @@ users, e.g. by filling in a login form. You can do this by adding a line to the
294294
<config>
295295
<!-- ... -->
296296
297-
<encoder class="Acme\WebserviceUserBundle\Security\User\WebserviceUser"
298-
algorithm="sha512"
299-
/>
297+
<encoder class="AppBundle\Security\User\WebserviceUser"
298+
algorithm="bcrypt" />
300299
</config>
301300
</srv:container>
302301
@@ -307,16 +306,15 @@ users, e.g. by filling in a login form. You can do this by adding a line to the
307306
// ...
308307
309308
'encoders' => array(
310-
'Acme\WebserviceUserBundle\Security\User\WebserviceUser' => 'sha512',
309+
'AppBundle\Security\User\WebserviceUser' => 'bcrypt',
311310
),
311+
// ...
312312
));
313313
314314
The value here should correspond with however the passwords were originally
315315
encoded when creating your users (however those users were created). When
316-
a user submits their password, the salt value is appended to the password and
317-
then encoded using this algorithm before being compared to the hashed password
318-
returned by your ``getPassword()`` method. Additionally, depending on your
319-
options, the password may be encoded multiple times and encoded to base64.
316+
a user submits their password, it's encoded using this algorithm and the result
317+
is compared to the hashed password returned by your ``getPassword()`` method.
320318

321319
.. sidebar:: Specifics on how Passwords are Encoded
322320

@@ -331,12 +329,12 @@ options, the password may be encoded multiple times and encoded to base64.
331329
If your external users have their passwords salted via a different method,
332330
then you'll need to do a bit more work so that Symfony properly encodes
333331
the password. That is beyond the scope of this entry, but would include
334-
sub-classing ``MessageDigestPasswordEncoder`` and overriding the ``mergePasswordAndSalt``
335-
method.
332+
sub-classing ``MessageDigestPasswordEncoder`` and overriding the
333+
``mergePasswordAndSalt`` method.
336334

337-
Additionally, the hash, by default, is encoded multiple times and encoded
338-
to base64. For specific details, see `MessageDigestPasswordEncoder`_.
339-
To prevent this, configure it in your configuration file:
335+
Additionally, you can configure the details of the algorithm used to hash
336+
passwords. In this example, the application sets explicitly the cost of
337+
the bcrypt hashing:
340338

341339
.. configuration-block::
342340

@@ -347,10 +345,9 @@ options, the password may be encoded multiple times and encoded to base64.
347345
# ...
348346
349347
encoders:
350-
Acme\WebserviceUserBundle\Security\User\WebserviceUser:
351-
algorithm: sha512
352-
encode_as_base64: false
353-
iterations: 1
348+
AppBundle\Security\User\WebserviceUser:
349+
algorithm: bcrypt
350+
cost: 12
354351
355352
.. code-block:: xml
356353
@@ -365,11 +362,9 @@ options, the password may be encoded multiple times and encoded to base64.
365362
<config>
366363
<!-- ... -->
367364
368-
<encoder class="Acme\WebserviceUserBundle\Security\User\WebserviceUser"
369-
algorithm="sha512"
370-
encode-as-base64="false"
371-
iterations="1"
372-
/>
365+
<encoder class="AppBundle\Security\User\WebserviceUser"
366+
algorithm="bcrypt"
367+
cost="12" />
373368
</config>
374369
</srv:container>
375370
@@ -380,12 +375,12 @@ options, the password may be encoded multiple times and encoded to base64.
380375
// ...
381376
382377
'encoders' => array(
383-
'Acme\WebserviceUserBundle\Security\User\WebserviceUser' => array(
384-
'algorithm' => 'sha512',
385-
'encode_as_base64' => false,
386-
'iterations' => 1,
387-
),
378+
'AppBundle\Security\User\WebserviceUser' => array(
379+
'algorithm' => 'bcrypt',
380+
'cost' => 12,
381+
)
388382
),
383+
// ...
389384
));
390385
391386
.. _MessageDigestPasswordEncoder: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Core/Encoder/MessageDigestPasswordEncoder.php

0 commit comments

Comments
 (0)