1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (C) 2020 Jan Böhmer |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Affero General Public License as published |
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Affero General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
declare(strict_types=1); |
20
|
|
|
|
21
|
|
|
namespace App\Services\TFA; |
22
|
|
|
|
23
|
|
|
use Exception; |
24
|
|
|
use RuntimeException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This class generates random backup codes for two factor authentication. |
28
|
|
|
*/ |
29
|
|
|
class BackupCodeGenerator |
30
|
|
|
{ |
31
|
|
|
protected $code_length; |
32
|
|
|
protected $code_count; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* BackupCodeGenerator constructor. |
36
|
|
|
* |
37
|
|
|
* @param int $code_length How many characters a single code should have. |
38
|
|
|
* @param int $code_count How many codes are generated for a whole backup set. |
39
|
|
|
*/ |
40
|
|
|
public function __construct(int $code_length, int $code_count) |
41
|
|
|
{ |
42
|
|
|
if ($code_length > 32) { |
43
|
|
|
throw new RuntimeException('Backup code can have maximum 32 digits!'); |
44
|
|
|
} |
45
|
|
|
if ($code_length < 6) { |
46
|
|
|
throw new RuntimeException('Code must have at least 6 digits to ensure security!'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->code_count = $code_count; |
50
|
|
|
$this->code_length = $code_length; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Generates a single backup code. |
55
|
|
|
* It is a random hexadecimal value with the digit count configured in constructor. |
56
|
|
|
* |
57
|
|
|
* @return string The generated backup code (e.g. 1f3870be2) |
58
|
|
|
* |
59
|
|
|
* @throws Exception If no entropy source is available. |
60
|
|
|
*/ |
61
|
|
|
public function generateSingleCode(): string |
62
|
|
|
{ |
63
|
|
|
$bytes = random_bytes(32); |
64
|
|
|
|
65
|
|
|
return substr(md5($bytes), 0, $this->code_length); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns a full backup code set. The code count can be configured in the constructor. |
70
|
|
|
* |
71
|
|
|
* @return string[] An array containing different backup codes. |
72
|
|
|
*/ |
73
|
|
|
public function generateCodeSet(): array |
74
|
|
|
{ |
75
|
|
|
$array = []; |
76
|
|
|
for ($n = 0; $n < $this->code_count; ++$n) { |
77
|
|
|
$array[] = $this->generateSingleCode(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $array; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|