@@ -1265,7 +1265,7 @@ in plain text (whether those users are stored in a configuration file or in
1265
1265
a database somewhere). Of course, in a real application, you'll want to encode
1266
1266
your users' passwords for security reasons. This is easily accomplished by
1267
1267
mapping your User class to one of several built-in "encoders". For example,
1268
- to store your users in memory, but obscure their passwords via ``sha1 ``,
1268
+ to store your users in memory, but obscure their passwords via ``bcrypt ``,
1269
1269
do the following:
1270
1270
1271
1271
.. configuration-block ::
@@ -1279,14 +1279,17 @@ do the following:
1279
1279
in_memory :
1280
1280
memory :
1281
1281
users :
1282
- ryan : { password: bb87a29949f3a1ee0559f8a57357487151281386, roles: 'ROLE_USER' }
1283
- admin : { password: 74913f5cd5f61ec0bcfdb775414c2fb3d161b620, roles: 'ROLE_ADMIN' }
1282
+ ryan :
1283
+ password : $2a$12$w/aHvnC/XNeDVrrl65b3dept8QcKqpADxUlbraVXXsC03Jam5hvoO
1284
+ roles : ' ROLE_USER'
1285
+ admin :
1286
+ password : $2a$12$HmOsqRDJK0HuMDQ5Fb2.AOLMQHyNHGD0seyjU3lEVusjT72QQEIpW
1287
+ roles : ' ROLE_ADMIN'
1284
1288
1285
1289
encoders :
1286
1290
Symfony\Component\Security\Core\User\User :
1287
- algorithm : sha1
1288
- iterations : 1
1289
- encode_as_base64 : false
1291
+ algorithm : bcrypt
1292
+ cost : 12
1290
1293
1291
1294
.. code-block :: xml
1292
1295
@@ -1296,18 +1299,18 @@ do the following:
1296
1299
<provider name =" in_memory" >
1297
1300
<memory >
1298
1301
<user name =" ryan"
1299
- password =" bb87a29949f3a1ee0559f8a57357487151281386 "
1302
+ password =" $2a$12$w/aHvnC/XNeDVrrl65b3dept8QcKqpADxUlbraVXXsC03Jam5hvoO "
1300
1303
roles =" ROLE_USER" />
1301
1304
<user name =" admin"
1302
- password =" 74913f5cd5f61ec0bcfdb775414c2fb3d161b620 "
1305
+ password =" $2a$12$HmOsqRDJK0HuMDQ5Fb2.AOLMQHyNHGD0seyjU3lEVusjT72QQEIpW "
1303
1306
roles =" ROLE_ADMIN" />
1304
1307
</memory >
1305
1308
</provider >
1306
1309
1307
1310
<encoder class =" Symfony\Component\Security\Core\User\User"
1308
- algorithm =" sha1 "
1309
- iterations = " 1 "
1310
- encode_as_base64 = " false " />
1311
+ algorithm =" bcrypt "
1312
+ cost = " 12 "
1313
+ />
1311
1314
</config >
1312
1315
1313
1316
.. code-block :: php
@@ -1320,11 +1323,11 @@ do the following:
1320
1323
'memory' => array(
1321
1324
'users' => array(
1322
1325
'ryan' => array(
1323
- 'password' => 'bb87a29949f3a1ee0559f8a57357487151281386 ',
1326
+ 'password' => '$2a$12$w/aHvnC/XNeDVrrl65b3dept8QcKqpADxUlbraVXXsC03Jam5hvoO ',
1324
1327
'roles' => 'ROLE_USER',
1325
1328
),
1326
1329
'admin' => array(
1327
- 'password' => '74913f5cd5f61ec0bcfdb775414c2fb3d161b620 ',
1330
+ 'password' => '$2a$12$HmOsqRDJK0HuMDQ5Fb2.AOLMQHyNHGD0seyjU3lEVusjT72QQEIpW ',
1328
1331
'roles' => 'ROLE_ADMIN',
1329
1332
),
1330
1333
),
@@ -1333,71 +1336,35 @@ do the following:
1333
1336
),
1334
1337
'encoders' => array(
1335
1338
'Symfony\Component\Security\Core\User\User' => array(
1336
- 'algorithm' => 'sha1',
1337
- 'iterations' => 1,
1338
- 'encode_as_base64' => false,
1339
+ 'algorithm' => 'bcrypt',
1340
+ 'iterations' => 12,
1339
1341
),
1340
1342
),
1341
1343
));
1342
1344
1343
- By setting the ``iterations `` to ``1 `` and the ``encode_as_base64 `` to false,
1344
- the password is simply run through the ``sha1 `` algorithm one time and without
1345
- any extra encoding. You can now calculate the hashed password either programmatically
1346
- (e.g. ``hash('sha1', 'ryanpass') ``) or via some online tool like `functions-online.com `_
1347
-
1348
- .. tip ::
1349
-
1350
- Supported algorithms for this method depend on your PHP version.
1351
- A full list is available calling the PHP function :phpfunction: `hash_algos `.
1352
-
1353
- .. caution ::
1354
-
1355
- The above example is not meaned for practical usage, it uses a weak hash
1356
- algorithm and it is only done to be able to generate the password easily. Using
1357
- :ref: `BCrypt <reference-security-bcrypt >` is a better option.
1358
-
1359
1345
.. versionadded :: 2.2
1360
1346
The BCrypt encoder was introduced in Symfony 2.2.
1361
1347
1362
- If you're creating your users dynamically (and storing them in a database),
1363
- you can use even tougher hashing algorithms and then rely on an actual password
1364
- encoder object to help you encode passwords. For example, suppose your User
1365
- object is ``Acme\UserBundle\Entity\User `` (like in the above example). First,
1366
- configure the encoder for that user:
1367
-
1368
- .. configuration-block ::
1369
-
1370
- .. code-block :: yaml
1348
+ You can now calculate the hashed password either programmatically
1349
+ (e.g. ``password_hash('ryanpass', PASSWORD_BCRYPT, array('cost' => 12)); ``)
1350
+ or via some online tool.
1371
1351
1372
- # app/config/security.yml
1373
- security :
1374
- # ...
1375
-
1376
- encoders :
1377
- Acme\UserBundle\Entity\User : bcrypt
1378
-
1379
- .. code-block :: xml
1352
+ .. caution ::
1380
1353
1381
- <!-- app/config/security.xml -->
1382
- <config >
1383
- <!-- ... -->
1354
+ If you're using PHP 5.4 or lower, you'll need to install the ``ircmaxell/password-compat ``
1355
+ library via Composer:
1384
1356
1385
- <encoder class =" Acme\UserBundle\Entity\User" algorithm =" bcrypt" />
1386
- </config >
1357
+ .. code-block :: json
1387
1358
1388
- .. code-block :: php
1389
-
1390
- // app/config/security.php
1391
- $container->loadFromExtension('security', array(
1392
- // ...
1393
- 'encoders' => array(
1394
- 'Acme\UserBundle\Entity\User' => 'bcrypt',
1395
- ),
1396
- ));
1359
+ {
1360
+ "require" : {
1361
+ "..." : " all the other dependencies..." ,
1362
+ "ircmaxell/password-compat" : " ~1.0.3"
1363
+ }
1364
+ }
1397
1365
1398
- In this case, you're using the strong ``bcrypt `` algorithm. This means that the
1399
- password has been greatly obfuscated so that the hashed password can't be
1400
- decoded (i.e. you can't determine the password from the hashed password).
1366
+ Supported algorithms for this method depend on your PHP version. A full list
1367
+ is available by calling the PHP function :phpfunction: `hash_algos `.
1401
1368
1402
1369
.. versionadded :: 2.2
1403
1370
As of Symfony 2.2 you can also use the :ref: `PBKDF2 <reference-security-pbkdf2 >`
@@ -1406,10 +1373,11 @@ decoded (i.e. you can't determine the password from the hashed password).
1406
1373
Determining the Hashed Password
1407
1374
...............................
1408
1375
1409
- If you have some sort of registration form for users, you'll need to be able
1410
- to determine the hashed password so that you can set it on your user. No
1411
- matter what algorithm you configure for your user object, the hashed password
1412
- can always be determined in the following way from a controller::
1376
+ If you're storing users in the database and you have some sort of registration
1377
+ form for users, you'll need to be able to determine the hashed password so
1378
+ that you can set it on your user before inserting it. No matter what algorithm
1379
+ you configure for your user object, the hashed password can always be determined
1380
+ in the following way from a controller::
1413
1381
1414
1382
$factory = $this->get('security.encoder_factory');
1415
1383
$user = new Acme\UserBundle\Entity\User();
@@ -1418,6 +1386,10 @@ can always be determined in the following way from a controller::
1418
1386
$password = $encoder->encodePassword('ryanpass', $user->getSalt());
1419
1387
$user->setPassword($password);
1420
1388
1389
+ In order for this to work, just make sure that you have the encoder for your
1390
+ user class (e.g. ``Acme\UserBundle\Entity\User ``) configured under the ``encoders ``
1391
+ key in ``app/config/security.yml ``.
1392
+
1421
1393
.. caution ::
1422
1394
1423
1395
When you allow a user to submit a plaintext password (e.g. registration
@@ -2070,5 +2042,4 @@ Learn more from the Cookbook
2070
2042
.. _`JMSSecurityExtraBundle` : http://jmsyst.com/bundles/JMSSecurityExtraBundle/1.2
2071
2043
.. _`FOSUserBundle` : https://github.com/FriendsOfSymfony/FOSUserBundle
2072
2044
.. _`implement the \S erializable interface` : http://php.net/manual/en/class.serializable.php
2073
- .. _`functions-online.com` : http://www.functions-online.com/sha1.html
2074
2045
.. _`Timing attack` : http://en.wikipedia.org/wiki/Timing_attack
0 commit comments