/
StandardIdentity.php
89 lines (64 loc) · 1.76 KB
/
StandardIdentity.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
<?php
namespace Palladium\Entity;
class StandardIdentity extends Identity
{
const HASH_ALGO = PASSWORD_BCRYPT;
const HASH_COST = 12;
private $identifier;
private $password;
private $hash;
protected $type = Identity::TYPE_STANDARD;
public function setIdentifier(string $identifier)
{
$this->identifier = strtolower($identifier);
}
/**
* @codeCoverageIgnore
*/
public function getIdentifier()
{
return $this->identifier;
}
public function getFingerprint(): string
{
return hash('sha384', $this->identifier);
}
public function setPassword(string $password, int $cost = null)
{
$this->password = $password;
if ($cost) {
$this->hash = $this->createHash($password, $cost);
}
}
public function rehashPassword(int $cost = StandardIdentity::HASH_COST)
{
$this->hash = $this->createHash($this->password, $cost);
}
/**
* @codeCoverageIgnore
*/
public function getHash()
{
return $this->hash;
}
private function createHash(string $password, int $cost): string
{
return password_hash($password, StandardIdentity::HASH_ALGO, ['cost' => $cost]);
}
public function setHash(string $hash = null)
{
$this->hash = $hash;
}
public function matchPassword(string $password): bool
{
return password_verify($password, $this->hash);
}
public function hasOldHash(int $cost = StandardIdentity::HASH_COST): bool
{
return password_needs_rehash($this->hash, StandardIdentity::HASH_ALGO, ['cost' => $cost]);
}
public function isVerified(): bool
{
return $this->getStatus() === Identity::STATUS_ACTIVE;
}
}