|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace hiqdev\yii2\mfa\base; |
|
5
|
|
|
|
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\base\BaseObject; |
|
8
|
|
|
|
|
9
|
|
|
class RecoveryCodeCollection extends BaseObject |
|
10
|
|
|
{ |
|
11
|
|
|
protected $codes = []; |
|
12
|
|
|
|
|
13
|
|
|
protected $count = 10; |
|
14
|
|
|
|
|
15
|
|
|
protected $blocks = 4; |
|
16
|
|
|
|
|
17
|
|
|
protected $blockLength = 3; |
|
18
|
|
|
|
|
19
|
|
|
protected $blockSeparator = '-'; |
|
20
|
|
|
|
|
21
|
|
|
public function getCodes(): array |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->codes; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function setCount(int $count): void |
|
27
|
|
|
{ |
|
28
|
|
|
$this->count = $count; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getCount(): int |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->count; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function setBlocks(int $blocks): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->blocks = $blocks; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getBlocks(): int |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->blocks; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function setBlockLength(int $length): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->blockLength = $length; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getBlockLength(): int |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->blockLength; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function setBlockSeparator(string $separator): void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->blockSeparator = $separator; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getBlockSeparator(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->blockSeparator; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function generate(): self |
|
67
|
|
|
{ |
|
68
|
|
|
$this->reset(); |
|
69
|
|
|
foreach (range(1, $this->getCount()) as $counter) { |
|
70
|
|
|
$this->codes[] = $this->generateCode(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function save(): bool |
|
77
|
|
|
{ |
|
78
|
|
|
$userId = Yii::$app->user->identity->id; |
|
79
|
|
|
$this->remove(); |
|
80
|
|
|
|
|
81
|
|
|
$errors = []; |
|
82
|
|
|
foreach ($this->getCodes() as $code){ |
|
83
|
|
|
$recovery = new Recovery(); |
|
84
|
|
|
if (!$recovery->setUser($userId)->setCode($code)->save()){ |
|
85
|
|
|
$errors[] = $recovery->getErrors(); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return empty($errors); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function remove(): int { |
|
93
|
|
|
$userId = Yii::$app->user->identity->id; |
|
94
|
|
|
|
|
95
|
|
|
return Recovery::deleteAll(['user_id' => $userId]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function generateCode(): string |
|
99
|
|
|
{ |
|
100
|
|
|
$codeBlocks = []; |
|
101
|
|
|
foreach (range(1, $this->getBlocks()) as $counter) { |
|
102
|
|
|
$codeBlocks[] = $this->generateBlock(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return implode($this->getBlockSeparator(), $codeBlocks); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function generateBlock(): string |
|
109
|
|
|
{ |
|
110
|
|
|
return bin2hex(random_bytes($this->getBlockLength())); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private function reset(): self |
|
114
|
|
|
{ |
|
115
|
|
|
$this->codes = []; |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
} |