/
Password.php
500 lines (437 loc) · 12.3 KB
/
Password.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Normalizer\NormalizerInterface;
class Password extends Data implements ResourcePersistenceAwareInterface, QueryResourcePersistenceAwareInterface, TypeDeclarationSupportInterface, EqualComparisonInterface, VarExporterInterface, NormalizerInterface
{
use DataObject\Traits\SimpleComparisonTrait;
use Extension\ColumnType;
use Extension\QueryColumnType;
use DataObject\Traits\SimpleNormalizerTrait;
const HASH_FUNCTION_PASSWORD_HASH = 'password_hash';
/**
* Static type of this element
*
* @internal
*
* @var string
*/
public $fieldtype = 'password';
/**
* @internal
*
* @var string|int
*/
public $width = 0;
/**
* Type for the column to query
*
* @internal
*
* @var string
*/
public $queryColumnType = 'varchar(255)';
/**
* Type for the column
*
* @internal
*
* @var string
*/
public $columnType = 'varchar(255)';
/**
* @internal
*
* @var string
*/
public $algorithm = self::HASH_FUNCTION_PASSWORD_HASH;
/**
* @internal
*
* @var string
*/
public $salt = '';
/**
* @internal
*
* @var string
*/
public $saltlocation = '';
/**
* @var int|null
*/
public $minimumLength;
/**
* @return string|int
*/
public function getWidth()
{
return $this->width;
}
/**
* @param string|int $width
*
* @return $this
*/
public function setWidth($width)
{
if (is_numeric($width)) {
$width = (int)$width;
}
$this->width = $width;
return $this;
}
/**
* @return int|null
*/
public function getMinimumLength(): ?int
{
return $this->minimumLength;
}
/**
* @param int|null $minimumLength
*/
public function setMinimumLength(?int $minimumLength): void
{
$this->minimumLength = $minimumLength;
}
/**
* @param string $algorithm
*/
public function setAlgorithm($algorithm)
{
$this->algorithm = $algorithm;
}
/**
* @return string
*/
public function getAlgorithm()
{
return $this->algorithm;
}
/**
* @param string $salt
*/
public function setSalt($salt)
{
$this->salt = $salt;
}
/**
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* @param string $saltlocation
*/
public function setSaltlocation($saltlocation)
{
$this->saltlocation = $saltlocation;
}
/**
* @return string
*/
public function getSaltlocation()
{
return $this->saltlocation;
}
/**
* @see ResourcePersistenceAwareInterface::getDataForResource
*
* @param string $data
* @param null|DataObject\Concrete $object
* @param mixed $params
*
* @return string|null
*/
public function getDataForResource($data, $object = null, $params = [])
{
if (empty($data)) {
return null;
}
// is already a hashed string? Then do not re-hash
$info = password_get_info($data);
if ($info['algo'] !== null && $info['algo'] !== 0) {
return $data;
}
// password_get_info() will not detect older, less secure, hashing algos.
// It might not detect some less common ones as well.
$maybeHash = preg_match('/^[a-f0-9]{32,}$/i', $data);
$hashLenghts = [
32, // MD2, MD4, MD5, RIPEMD-128, Snefru 128, Tiger/128, HAVAL128
40, // SHA-1, HAS-160, RIPEMD-160, Tiger/160, HAVAL160
48, // Tiger/192, HAVAL192
56, // SHA-224, HAVAL224
64, // SHA-256, BLAKE-256, GOST, GOST CryptoPro, HAVAL256, RIPEMD-256, Snefru 256
96, // SHA-384
128, // SHA-512, BLAKE-512, SWIFFT
];
if ($maybeHash && in_array(strlen($data), $hashLenghts, true)) {
// Probably already a hashed string
return $data;
}
$hashed = $this->calculateHash($data);
/** set the hashed password back to the object, to be sure that is not plain-text after the first save
this is especially to avoid plaintext passwords in the search-index see: PIMCORE-1406 */
// a model should be switched if the owner parameter is used,
// for example: field collections would use \Pimcore\Model\DataObject\Fieldcollection\Data\Dao
$passwordModel = array_key_exists('owner', $params)
? $params['owner']
: ($object ?: null);
if (null !== $passwordModel && !$passwordModel instanceof DataObject\Classificationstore && !$passwordModel instanceof DataObject\Localizedfield) {
$setter = 'set' . ucfirst($this->getName());
$passwordModel->$setter($hashed);
}
return $hashed;
}
/**
* Calculate hash according to configured parameters
*
* @internal
*
* @param string $data
*
* @return bool|null|string
*/
public function calculateHash($data)
{
$hash = null;
if ($this->algorithm === static::HASH_FUNCTION_PASSWORD_HASH) {
$hash = password_hash($data, PASSWORD_DEFAULT);
} else {
if (!empty($this->salt)) {
if ($this->saltlocation == 'back') {
$data = $data . $this->salt;
} elseif ($this->saltlocation == 'front') {
$data = $this->salt . $data;
}
}
$hash = hash($this->algorithm, $data);
}
return $hash;
}
/**
* Verify password. Optionally re-hash the password if needed.
*
* Re-hash will be performed if PHP's password_hash default params (algorithm, cost) differ
* from the ones which were used to create the hash (e.g. cost was increased from 10 to 12).
* In this case, the hash will be re-calculated with the new parameters and saved back to the object.
*
* @internal
*
* @param string $password
* @param DataObject\Concrete $object
* @param bool|true $updateHash
*
* @return bool
*/
public function verifyPassword($password, DataObject\Concrete $object, $updateHash = true)
{
$getter = 'get' . ucfirst($this->getName());
$setter = 'set' . ucfirst($this->getName());
$objectHash = $object->$getter();
if (null === $objectHash || empty($objectHash)) {
return false;
}
if ($this->getAlgorithm() === static::HASH_FUNCTION_PASSWORD_HASH) {
$result = (true === password_verify($password, $objectHash));
if ($result && $updateHash) {
// password needs rehash (e.g PASSWORD_DEFAULT changed to a stronger algorithm)
if (true === password_needs_rehash($objectHash, PASSWORD_DEFAULT)) {
$newHash = $this->calculateHash($password);
$object->$setter($newHash);
$object->save();
}
}
} else {
$hash = $this->calculateHash($password);
$result = $hash === $objectHash;
}
return $result;
}
/**
* @see ResourcePersistenceAwareInterface::getDataFromResource
*
* @param string $data
* @param null|DataObject\Concrete $object
* @param mixed $params
*
* @return string
*/
public function getDataFromResource($data, $object = null, $params = [])
{
return $data;
}
/**
* @see QueryResourcePersistenceAwareInterface::getDataForQueryResource
*
* @param string $data
* @param null|DataObject\Concrete $object
* @param mixed $params
*
* @return string
*/
public function getDataForQueryResource($data, $object = null, $params = [])
{
return $this->getDataForResource($data, $object, $params);
}
/**
* @see Data::getDataForEditmode
*
* @param string $data
* @param null|DataObject\Concrete $object
* @param mixed $params
*
* @return string
*/
public function getDataForEditmode($data, $object = null, $params = [])
{
return $data;
}
/**
* @see Data::getDataFromEditmode
*
* @param string $data
* @param null|DataObject\Concrete $object
* @param mixed $params
*
* @return string
*/
public function getDataFromEditmode($data, $object = null, $params = [])
{
return $data;
}
/**
* @see Data::getVersionPreview
*
* @param string $data
* @param null|DataObject\Concrete $object
* @param mixed $params
*
* @return string
*/
public function getVersionPreview($data, $object = null, $params = [])
{
return '******';
}
/**
* @param string $data
* @param DataObject\Concrete $object
* @param array $params
*
* @return string
*/
public function getDataForGrid($data, $object, $params = [])
{
return '******';
}
/**
* {@inheritdoc}
*/
public function getDataForSearchIndex($object, $params = [])
{
return '';
}
/**
* {@inheritdoc}
*/
public function isDiffChangeAllowed($object, $params = [])
{
return true;
}
/**
* @param array $data
* @param DataObject\Concrete|null $object
* @param mixed $params
*
* @return mixed
*/
public function getDiffDataFromEditmode($data, $object = null, $params = [])
{
return $data[0]['data'];
}
/** See parent class.
* @param mixed $data
* @param DataObject\Concrete|null $object
* @param mixed $params
*
* @return array|null
*/
public function getDiffDataForEditMode($data, $object = null, $params = [])
{
$diffdata = [];
$diffdata['data'] = $data;
$diffdata['disabled'] = !($this->isDiffChangeAllowed($object, $params));
$diffdata['field'] = $this->getName();
$diffdata['key'] = $this->getName();
$diffdata['type'] = $this->fieldtype;
if ($data) {
$diffdata['value'] = $this->getVersionPreview($data, $object, $params);
// $diffdata["value"] = $data;
}
$diffdata['title'] = !empty($this->title) ? $this->title : $this->name;
$result = [];
$result[] = $diffdata;
return $result;
}
/**
* @param DataObject\ClassDefinition\Data\Password $masterDefinition
*/
public function synchronizeWithMasterDefinition(DataObject\ClassDefinition\Data $masterDefinition)
{
$this->algorithm = $masterDefinition->algorithm;
$this->salt = $masterDefinition->salt;
$this->saltlocation = $masterDefinition->saltlocation;
}
/**
* {@inheritdoc}
*/
public function getParameterTypeDeclaration(): ?string
{
return '?string';
}
/**
* {@inheritdoc}
*/
public function getReturnTypeDeclaration(): ?string
{
return '?string';
}
/**
* {@inheritdoc}
*/
public function getPhpdocInputType(): ?string
{
return 'string|null';
}
/**
* {@inheritdoc}
*/
public function getPhpdocReturnType(): ?string
{
return 'string|null';
}
public function checkValidity($data, $omitMandatoryCheck = false, $params = [])
{
if($this->getMinimumLength() && strlen($data) < $this->getMinimumLength()) {
throw new Model\Element\ValidationException('Value in field [ ' . $this->getName() . ' ] is not at least ' . $this->getMinimumLength() . ' characters');
}
parent::checkValidity($data, $omitMandatoryCheck, $params);
}
}