1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/2fa-library project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\TwoFA\Validator; |
13
|
|
|
|
14
|
|
|
use Da\TwoFA\Contracts\ValidatorInterface; |
15
|
|
|
use Da\TwoFA\Exception\GoogleAuthenticatorCompatibilityException; |
16
|
|
|
use Da\TwoFA\Exception\InvalidCharactersException; |
17
|
|
|
use Da\TwoFA\Traits\FailReasonTrait; |
18
|
|
|
|
19
|
|
|
class SecretKeyValidator implements ValidatorInterface |
20
|
|
|
{ |
21
|
|
|
use FailReasonTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Enforce Google Authenticator compatibility. |
25
|
|
|
*/ |
26
|
|
|
protected $googleAuthenticatorCompatibility; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Encoder constructor. |
30
|
|
|
* |
31
|
|
|
* @param bool $enforceGoogleAuthenticatorCompatibility |
32
|
|
|
*/ |
33
|
|
|
public function __construct(bool $enforceGoogleAuthenticatorCompatibility = true) |
34
|
|
|
{ |
35
|
|
|
$this->enforceGoogleAuthenticatorCompatibility($enforceGoogleAuthenticatorCompatibility); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param bool $enforce |
40
|
|
|
*/ |
41
|
|
|
public function enforceGoogleAuthenticatorCompatibility(bool $enforce): void |
42
|
|
|
{ |
43
|
|
|
$this->googleAuthenticatorCompatibility = $enforce; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function isGoogleAuthenticatorCompatibilityEnforced(): bool |
50
|
|
|
{ |
51
|
|
|
return $this->googleAuthenticatorCompatibility; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param mixed $value |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function validate($value): bool |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
$this->resetFailReason(); |
63
|
|
|
$this->checkForValidCharacters($value); |
64
|
|
|
$this->checkGoogleAuthenticatorCompatibility($value); |
65
|
|
|
} catch (InvalidCharactersException $e) { |
66
|
|
|
$this->failReason = 'Secret key contains invalid characters.'; |
67
|
|
|
} catch (GoogleAuthenticatorCompatibilityException $e) { |
68
|
|
|
$this->failReason = 'Google incompatible secret key.'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return null === $this->failReason; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if the secret key is compatible with Google Authenticator. |
76
|
|
|
* |
77
|
|
|
* @param string $value |
78
|
|
|
* |
79
|
|
|
* @throws GoogleAuthenticatorCompatibilityException |
80
|
|
|
*/ |
81
|
|
|
protected function checkGoogleAuthenticatorCompatibility(string $value): void |
82
|
|
|
{ |
83
|
|
|
if ( |
84
|
|
|
$this->isGoogleAuthenticatorCompatibilityEnforced() |
85
|
|
|
&& !(new GoogleAuthenticationCompatibilityValidator())->validate($value) |
86
|
|
|
) { |
87
|
|
|
throw new GoogleAuthenticatorCompatibilityException('Not google compatible key'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check if all secret key characters are valid. |
93
|
|
|
* |
94
|
|
|
* @param string $value |
95
|
|
|
* |
96
|
|
|
* @throws InvalidCharactersException |
97
|
|
|
*/ |
98
|
|
|
protected function checkForValidCharacters(string $value): void |
99
|
|
|
{ |
100
|
|
|
if (!(new CharactersValidator())->validate($value)) { |
101
|
|
|
throw new InvalidCharactersException('Invalid secret value'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|