|
| 1 | +.. index:: |
| 2 | + single: Security; Named Encoders |
| 3 | + |
| 4 | +How to Choose the Password Encoder Algorithm Dynamically |
| 5 | +======================================================== |
| 6 | + |
| 7 | +.. versionadded:: 2.5 |
| 8 | + Named encoders were introduced in Symfony 2.5. |
| 9 | + |
| 10 | +Usually, the same password encoder is used for all users by configuring it |
| 11 | +to apply to all instances of a specific class: |
| 12 | + |
| 13 | + # app/config/security.yml |
| 14 | + security: |
| 15 | + # ... |
| 16 | + encoders: |
| 17 | + Symfony\Component\Security\Core\User\User: sha512 |
| 18 | + |
| 19 | + .. code-block:: xml |
| 20 | +
|
| 21 | + <!-- app/config/security.xml --> |
| 22 | + <?xml version="1.0" encoding="UTF-8"?> |
| 23 | + <srv:container xmlns="http://symfony.com/schema/dic/security" |
| 24 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 25 | + xmlns:srv="http://symfony.com/schema/dic/services" |
| 26 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 27 | + http://symfony.com/schema/dic/services/services-1.0.xsd" |
| 28 | + > |
| 29 | + <config> |
| 30 | + <!-- ... --> |
| 31 | + <encoder class="Symfony\Component\Security\Core\User\User" |
| 32 | + algorithm="sha512" |
| 33 | + /> |
| 34 | + </config> |
| 35 | + </srv:container> |
| 36 | +
|
| 37 | + .. code-block:: php |
| 38 | +
|
| 39 | + // app/config/security.php |
| 40 | + $container->loadFromExtension('security', array( |
| 41 | + // ... |
| 42 | + 'encoders' => array( |
| 43 | + 'Symfony\Component\Security\Core\User\User' => array( |
| 44 | + 'algorithm' => 'sha512', |
| 45 | + ), |
| 46 | + ), |
| 47 | + )); |
| 48 | +
|
| 49 | +Another option is to use a "named" encoder and then select which encoder |
| 50 | +you want to use dynamically. |
| 51 | + |
| 52 | +In the previous example, you've set the ``sha512`` algorithm for ``Acme\UserBundle\Entity\User``. |
| 53 | +This may be secure enough for a regular user, but what if you want your admins |
| 54 | +to have a stronger algorithm, for example ``bcrypt``. This can be done with |
| 55 | +named encoders: |
| 56 | + |
| 57 | +.. configuration-block:: |
| 58 | + |
| 59 | + .. code-block:: yaml |
| 60 | +
|
| 61 | + # app/config/security.yml |
| 62 | + security: |
| 63 | + # ... |
| 64 | + encoders: |
| 65 | + harsh: |
| 66 | + algorithm: bcrypt |
| 67 | + cost: 15 |
| 68 | +
|
| 69 | + .. code-block:: xml |
| 70 | +
|
| 71 | + <!-- app/config/security.xml --> |
| 72 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 73 | + <srv:container xmlns="http://symfony.com/schema/dic/security" |
| 74 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 75 | + xmlns:srv="http://symfony.com/schema/dic/services" |
| 76 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 77 | + http://symfony.com/schema/dic/services/services-1.0.xsd" |
| 78 | + > |
| 79 | +
|
| 80 | + <config> |
| 81 | + <!-- ... --> |
| 82 | + <encoder class="harsh" |
| 83 | + algorithm="bcrypt" |
| 84 | + cost="15" /> |
| 85 | + </config> |
| 86 | + </srv:container> |
| 87 | +
|
| 88 | + .. code-block:: php |
| 89 | +
|
| 90 | + // app/config/security.php |
| 91 | + $container->loadFromExtension('security', array( |
| 92 | + // ... |
| 93 | + 'encoders' => array( |
| 94 | + 'harsh' => array( |
| 95 | + 'algorithm' => 'bcrypt', |
| 96 | + 'cost' => '15' |
| 97 | + ), |
| 98 | + ), |
| 99 | + )); |
| 100 | +
|
| 101 | +This creates an encoder named ``harsh``. In order for a ``User`` instance |
| 102 | +to use it, the class must implement |
| 103 | +:class:`Symfony\\Component\\Security\\Core\\Encoder\\EncoderAwareInterface`. |
| 104 | +The interface requires one method - ``getEncoderName`` - which should reutrn |
| 105 | +the name of the encoder to use:: |
| 106 | + |
| 107 | + // src/Acme/UserBundle/Entity/User.php |
| 108 | + namespace Acme\UserBundle\Entity; |
| 109 | + |
| 110 | + use Symfony\Component\Security\Core\User\UserInterface; |
| 111 | + use Symfony\Component\Security\Core\Encoder\EncoderAwareInterface; |
| 112 | + |
| 113 | + class User implements UserInterface, EncoderAwareInterface |
| 114 | + { |
| 115 | + public function getEncoderName() |
| 116 | + { |
| 117 | + if ($this->isAdmin()) { |
| 118 | + return 'harsh'; |
| 119 | + } |
| 120 | + |
| 121 | + return null; // use the default encoder |
| 122 | + } |
| 123 | + } |
0 commit comments