3
3
` Zend\Authentication\Adapter\DbTable\CredentialTreatmentAdapter ` will execute a
4
4
SQL query containing the provided identity and credentials, passing the
5
5
credentials to a * credential treatment* function defined on the RDBMS server;
6
- if an identity is returned, authentication succeeds. Common credential
7
- treatments include ` MD5() ` and ` PASSWORD() ` .
6
+ if an identity is returned, authentication succeeds. Credential
7
+ treatments depends on your RDBMS, and while simple hashing function such as
8
+ ` md5 ` and ` sha1 ` are generally available, it is recommended not to use them and
9
+ rather use the RDBMS specific function such as
10
+ [ ` PASSWORD(?) ` for MySQL] ( http://dev.mysql.com/doc/refman/5.7/en/password-hashing.html ) or
11
+ [ ` crypt() ` for PostgreSQL] ( https://www.postgresql.org/docs/11/pgcrypto.html#id-1.11.7.34.6 ) .
12
+ More details are available in the next section.
13
+
14
+ ## Security considerations
15
+
16
+ Passing passwords to database in plaintext for insert or verification is
17
+ generally not recommended.
18
+ Sql statements can and usually are logged by the database, and passwords in them
19
+ become visible to anyone with access to the logs or monitoring tools that
20
+ consume those logs.
21
+
22
+ The safer approach is to hash passwords, and to verify them against a stored
23
+ hash in your application code. This way the password never leaves the
24
+ application, and only the hashed value is exchanged with the database.
25
+
26
+ As such, this adapter is not recommended for new applications, and existing
27
+ applications should consider migrating to using PHP-provided password handling
28
+ functions such as ` password_hash() ` and ` password_verify() ` . See
29
+ [ CallbackCheckAdapter] ( callback-check.md ) for more info.
8
30
9
31
## Configuration Options
10
32
@@ -23,7 +45,7 @@ The available configuration options include:
23
45
- ` credentialTreatment ` : In many cases, passwords and other sensitive data
24
46
are encrypted, hashed, encoded, obscured, salted or otherwise treated through
25
47
some function or algorithm. By specifying a parameterized treatment string
26
- with this method, such as '` MD5(?) ` ' or ' ` PASSWORD(?) ` ', a developer may
48
+ with this method, such as '` PASSWORD(?) ` ', a developer may
27
49
apply such arbitrary SQL upon input credential data. Since these functions
28
50
are specific to the underlying RDBMS, check the database manual for the
29
51
availability of such functions for your database system.
@@ -186,7 +208,7 @@ credential treatment to solve more complex problems.
186
208
187
209
### Check for compromised user
188
210
189
- In this scenario, we use the credential treatment ` MD5 ()` , but also check to see
211
+ In this scenario, we use the credential treatment ` PASSWORD ()` , but also check to see
190
212
that the user has not been flagged as "compromised", which is a potential value
191
213
of the ` status ` field for the user record.
192
214
@@ -199,7 +221,7 @@ $adapter = new AuthAdapter(
199
221
'users',
200
222
'username',
201
223
'password',
202
- 'MD5 (?) AND status != "compromised"'
224
+ 'PASSWORD (?) AND status != "compromised"'
203
225
);
204
226
```
205
227
@@ -218,7 +240,7 @@ $adapter = new AuthAdapter(
218
240
'users',
219
241
'username',
220
242
'password',
221
- 'MD5 (?) AND active = "TRUE"'
243
+ 'PASSWORD (?) AND active = "TRUE"'
222
244
);
223
245
```
224
246
@@ -238,7 +260,9 @@ $sqlAlter = "ALTER TABLE [users] "
238
260
. "AFTER [password]";
239
261
```
240
262
241
- Salts should be created * for each user* using a cryptographically sound pseudo-random number generator (CSPRNG). PHP 7 provides an implementation via ` random_bytes ` :
263
+ Salts should be created * for each user* using a cryptographically sound pseudo-random number generator (CSPRNG).
264
+ PHP 7 provides an implementation via ` random_bytes() ` (and
265
+ the [ random_compat package provides them for older, supported versions of PHP] ( https://github.com/paragonie/random_compat ) ):
242
266
243
267
``` php
244
268
$salt = random_bytes(32);
267
291
'users',
268
292
'username',
269
293
'password',
270
- "MD5 (CONCAT('staticSalt', ?, password_salt))"
294
+ "PASSWORD (CONCAT('staticSalt', ?, password_salt))"
271
295
);
272
296
```
273
297
@@ -304,13 +328,13 @@ The following uses the second example in this section, adding another `WHERE`
304
328
clause to determine if the user is active in the system.
305
329
306
330
``` php
307
- // Create a basic adapter, with only an MD5 () credential treatment:
331
+ // Create a basic adapter, with only an PASSWORD () credential treatment:
308
332
$adapter = new AuthAdapter(
309
333
$db,
310
334
'users',
311
335
'username',
312
336
'password',
313
- 'MD5 (?)'
337
+ 'PASSWORD (?)'
314
338
);
315
339
316
340
// Now retrieve the Select instance and modify it:
0 commit comments