|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Palladium\Entity; |
|
4
|
|
|
|
|
5
|
|
|
class OneTimeIdentity extends Identity |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
const HASH_ALGO = PASSWORD_BCRYPT; |
|
9
|
|
|
const HASH_COST = 12; |
|
10
|
|
|
|
|
11
|
|
|
const NONCE_SIZE = 16; |
|
12
|
|
|
const KEY_SIZE = 32; |
|
13
|
|
|
|
|
14
|
|
|
private $nonce; |
|
15
|
|
|
private $key; |
|
16
|
|
|
private $hash; |
|
17
|
|
|
|
|
18
|
|
|
protected $type = Identity::TYPE_ONETIME; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @codeCoverageIgnore |
|
23
|
|
|
*/ |
|
24
|
|
|
public function setNonce($nonce) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->nonce = (string) $nonce; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @codeCoverageIgnore |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getNonce() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->nonce; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
1 |
|
public function generateNewNonce() |
|
40
|
|
|
{ |
|
41
|
1 |
|
$this->nonce = bin2hex(random_bytes(self::NONCE_SIZE)); |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @codeCoverageIgnore |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getFingerprint() |
|
49
|
|
|
{ |
|
50
|
|
|
return hash('sha384', $this->nonce); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Sets a new key and resets the hash. |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function generateNewKey() |
|
58
|
|
|
{ |
|
59
|
1 |
|
$this->key = bin2hex(random_bytes(self::KEY_SIZE)); |
|
60
|
1 |
|
$this->hash = $this->makeHash($this->key); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
3 |
|
private function makeHash($key) |
|
65
|
|
|
{ |
|
66
|
3 |
|
$hash = password_hash($key, self::HASH_ALGO, ['cost' => self::HASH_COST]); |
|
67
|
|
|
|
|
68
|
3 |
|
return $hash; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
1 |
|
public function matchKey($key) |
|
73
|
|
|
{ |
|
74
|
1 |
|
return password_verify($key, $this->hash); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Assignes a new identification key and resets a the hash. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $key |
|
82
|
|
|
*/ |
|
83
|
4 |
View Code Duplication |
public function setKey($key) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
4 |
|
$this->hash = null; |
|
86
|
|
|
|
|
87
|
4 |
|
if (empty($key)) { |
|
88
|
2 |
|
$this->key = null; |
|
89
|
2 |
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
$this->key = (string) $key; |
|
93
|
2 |
|
$this->hash = $this->makeHash($key); |
|
94
|
2 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @codeCoverageIgnore |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getKey() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->key; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @codeCoverageIgnore |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setHash($hash) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->hash = (string) $hash; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @codeCoverageIgnore |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getHash() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->hash; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.