1
1
.. index ::
2
2
single: Security; Named Encoders
3
3
4
- How to Use A Different Password Encoder Algorithm Per User
5
- ==========================================================
4
+ How to Use A Different Password Hasher Algorithm Per User
5
+ =========================================================
6
6
7
- Usually, the same password encoder is used for all users by configuring it
7
+ Usually, the same password hasher is used for all users by configuring it
8
8
to apply to all instances of a specific class:
9
9
10
10
.. configuration-block ::
@@ -14,7 +14,7 @@ to apply to all instances of a specific class:
14
14
# config/packages/security.yaml
15
15
security :
16
16
# ...
17
- encoders :
17
+ password_hashers :
18
18
App\Entity\User :
19
19
algorithm : auto
20
20
cost : 12
@@ -33,7 +33,7 @@ to apply to all instances of a specific class:
33
33
>
34
34
<config >
35
35
<!-- ... -->
36
- <encoder class =" App\Entity\User"
36
+ <security : password-hasher class =" App\Entity\User"
37
37
algorithm =" auto"
38
38
cost =" 12"
39
39
/>
@@ -47,21 +47,21 @@ to apply to all instances of a specific class:
47
47
48
48
$container->loadFromExtension('security', [
49
49
// ...
50
- 'encoders ' => [
50
+ 'password_hashers ' => [
51
51
User::class => [
52
52
'algorithm' => 'auto',
53
53
'cost' => 12,
54
54
],
55
55
],
56
56
]);
57
57
58
- Another option is to use a "named" encoder and then select which encoder
58
+ Another option is to use a "named" hasher and then select which hasher
59
59
you want to use dynamically.
60
60
61
61
In the previous example, you've set the ``auto `` algorithm for ``App\Entity\User ``.
62
62
This may be secure enough for a regular user, but what if you want your admins
63
63
to have a stronger algorithm, for example ``auto `` with a higher cost. This can
64
- be done with named encoders :
64
+ be done with named hashers :
65
65
66
66
.. configuration-block ::
67
67
@@ -70,7 +70,7 @@ be done with named encoders:
70
70
# config/packages/security.yaml
71
71
security :
72
72
# ...
73
- encoders :
73
+ password_hashers :
74
74
harsh :
75
75
algorithm : auto
76
76
cost : 15
@@ -90,7 +90,7 @@ be done with named encoders:
90
90
91
91
<config >
92
92
<!-- ... -->
93
- <encoder class =" harsh"
93
+ <security : password-hasher class =" harsh"
94
94
algorithm =" auto"
95
95
cost =" 15" />
96
96
</config >
@@ -101,7 +101,7 @@ be done with named encoders:
101
101
// config/packages/security.php
102
102
$container->loadFromExtension('security', [
103
103
// ...
104
- 'encoders ' => [
104
+ 'password_hashers ' => [
105
105
'harsh' => [
106
106
'algorithm' => 'auto',
107
107
'cost' => '15',
@@ -115,33 +115,33 @@ be done with named encoders:
115
115
then the recommended hashing algorithm to use is
116
116
:ref: `Sodium <reference-security-sodium >`.
117
117
118
- This creates an encoder named ``harsh ``. In order for a ``User `` instance
118
+ This creates a hasher named ``harsh ``. In order for a ``User `` instance
119
119
to use it, the class must implement
120
- :class: `Symfony\\ Component\\ Security \\ Core \\ Encoder \\ EncoderAwareInterface `.
121
- The interface requires one method - ``getEncoderName () `` - which should return
122
- the name of the encoder to use::
120
+ :class: `Symfony\\ Component\\ PasswordHasher \\ Hasher \\ PasswordHasherAwareInterface `.
121
+ The interface requires one method - ``getPasswordHasherName () `` - which should return
122
+ the name of the hasher to use::
123
123
124
124
// src/Entity/User.php
125
125
namespace App\Entity;
126
126
127
- use Symfony\Component\Security\Core\Encoder\EncoderAwareInterface ;
127
+ use Symfony\Component\PasswordHasher\Hasher\PasswordHasherAwareInterface ;
128
128
use Symfony\Component\Security\Core\User\UserInterface;
129
129
130
- class User implements UserInterface, EncoderAwareInterface
130
+ class User implements UserInterface, PasswordHasherAwareInterface
131
131
{
132
- public function getEncoderName (): ?string
132
+ public function getPasswordHasherName (): ?string
133
133
{
134
134
if ($this->isAdmin()) {
135
135
return 'harsh';
136
136
}
137
137
138
- return null; // use the default encoder
138
+ return null; // use the default hasher
139
139
}
140
140
}
141
141
142
- If you created your own password encoder implementing the
143
- :class: `Symfony \\ Component\\ Security \\ Core \\ Encoder \\ PasswordEncoderInterface `,
144
- you must register a service for it in order to use it as a named encoder :
142
+ If you created your own password hasher implementing the
143
+ :class: `SSymfony \\ Component\\ PasswordHasher \\ Hasher \\ UserPasswordHasherInterface `,
144
+ you must register a service for it in order to use it as a named hasher :
145
145
146
146
.. configuration-block ::
147
147
@@ -150,9 +150,9 @@ you must register a service for it in order to use it as a named encoder:
150
150
# config/packages/security.yaml
151
151
security :
152
152
# ...
153
- encoders :
154
- app_encoder :
155
- id : ' App\Security\Encoder\MyCustomPasswordEncoder '
153
+ password_hashers :
154
+ app_hasher :
155
+ id : ' App\Security\Hasher\MyCustomPasswordHasher '
156
156
157
157
.. code-block :: xml
158
158
@@ -169,27 +169,27 @@ you must register a service for it in order to use it as a named encoder:
169
169
170
170
<config >
171
171
<!-- ... -->
172
- <encoder class =" app_encoder "
173
- id =" App\Security\Encoder\MyCustomPasswordEncoder " />
172
+ <security : password_hasher class =" app_hasher "
173
+ id =" App\Security\Hasher\MyCustomPasswordHasher " />
174
174
</config >
175
175
</srv : container >
176
176
177
177
.. code-block :: php
178
178
179
179
// config/packages/security.php
180
180
// ...
181
- use App\Security\Encoder\MyCustomPasswordEncoder ;
181
+ use App\Security\Hasher\MyCustomPasswordHasher ;
182
182
183
183
$container->loadFromExtension('security', [
184
184
// ...
185
- 'encoders ' => [
186
- 'app_encoder ' => [
187
- 'id' => MyCustomPasswordEncoder ::class,
185
+ 'password_hashers ' => [
186
+ 'app_hasher ' => [
187
+ 'id' => MyCustomPasswordHasher ::class,
188
188
],
189
189
],
190
190
]);
191
191
192
- This creates an encoder named ``app_encoder `` from a service with the ID
193
- ``App\Security\Encoder\MyCustomPasswordEncoder ``.
192
+ This creates a hasher named ``app_hasher `` from a service with the ID
193
+ ``App\Security\Hasher\MyCustomPasswordHasher ``.
194
194
195
195
.. _`libsodium` : https://pecl.php.net/package/libsodium
0 commit comments