1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palladium\Entity; |
4
|
|
|
|
5
|
|
|
class StandardIdentity extends Identity |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
const HASH_ALGO = PASSWORD_BCRYPT; |
9
|
|
|
const HASH_COST = 12; |
10
|
|
|
|
11
|
|
|
private $identifier; |
12
|
|
|
private $password; |
13
|
|
|
private $hash; |
14
|
|
|
|
15
|
|
|
protected $type = Identity::TYPE_STANDARD; |
16
|
|
|
|
17
|
|
|
|
18
|
18 |
|
public function setIdentifier(string $identifier) |
19
|
|
|
{ |
20
|
18 |
|
$this->identifier = strtolower($identifier); |
21
|
18 |
|
} |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @codeCoverageIgnore |
26
|
|
|
*/ |
27
|
|
|
public function getIdentifier() |
28
|
|
|
{ |
29
|
|
|
return $this->identifier; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
16 |
|
public function getFingerprint(): string |
34
|
|
|
{ |
35
|
16 |
|
return hash('sha384', $this->identifier); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
13 |
|
public function setPassword(string $password, int $cost = null) |
40
|
|
|
{ |
41
|
13 |
|
$this->password = $password; |
42
|
13 |
|
if ($cost) { |
|
|
|
|
43
|
5 |
|
$this->hash = $this->createHash($password, $cost); |
44
|
|
|
} |
45
|
13 |
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
2 |
|
public function rehashPassword(int $cost = StandardIdentity::HASH_COST) |
49
|
|
|
{ |
50
|
2 |
|
$this->hash = $this->createHash($this->password, $cost); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @codeCoverageIgnore |
55
|
|
|
*/ |
56
|
|
|
public function getHash() |
57
|
|
|
{ |
58
|
|
|
return $this->hash; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
7 |
|
private function createHash(string $password, int $cost): string |
63
|
|
|
{ |
64
|
7 |
|
return password_hash($password, StandardIdentity::HASH_ALGO, ['cost' => $cost]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
17 |
|
public function setHash(string $hash = null) |
69
|
|
|
{ |
70
|
17 |
|
$this->hash = $hash; |
71
|
17 |
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
10 |
|
public function matchPassword(string $password): bool |
75
|
|
|
{ |
76
|
10 |
|
return password_verify($password, $this->hash); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
9 |
|
public function hasOldHash(int $cost = StandardIdentity::HASH_COST): bool |
81
|
|
|
{ |
82
|
9 |
|
return password_needs_rehash($this->hash, StandardIdentity::HASH_ALGO, ['cost' => $cost]); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function isVerified(): bool |
86
|
|
|
{ |
87
|
1 |
|
return $this->getStatus() === Identity::STATUS_ACTIVE; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: