|
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\Validator; |
|
22
|
|
|
|
|
23
|
|
|
use App\Entity\User; |
|
24
|
|
|
use function is_string; |
|
25
|
|
|
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticator; |
|
26
|
|
|
use function strlen; |
|
27
|
|
|
use Symfony\Component\Form\FormInterface; |
|
28
|
|
|
use Symfony\Component\Validator\Constraint; |
|
29
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
30
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
31
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedValueException; |
|
32
|
|
|
|
|
33
|
|
|
class ValidGoogleAuthCodeValidator extends ConstraintValidator |
|
34
|
|
|
{ |
|
35
|
|
|
protected $googleAuthenticator; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(GoogleAuthenticator $googleAuthenticator) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->googleAuthenticator = $googleAuthenticator; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function validate($value, Constraint $constraint): void |
|
43
|
|
|
{ |
|
44
|
|
|
if (!$constraint instanceof ValidGoogleAuthCode) { |
|
45
|
|
|
throw new UnexpectedTypeException($constraint, ValidGoogleAuthCode::class); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (null === $value || '' === $value) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!is_string($value)) { |
|
53
|
|
|
throw new UnexpectedValueException($value, 'string'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!ctype_digit($value)) { |
|
57
|
|
|
$this->context->addViolation('validator.google_code.only_digits_allowed'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
//Number must have 6 digits |
|
61
|
|
|
if (6 !== strlen($value)) { |
|
62
|
|
|
$this->context->addViolation('validator.google_code.wrong_digit_count'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
//Try to retrieve the user we want to check |
|
66
|
|
|
if ($this->context->getObject() instanceof FormInterface && |
|
67
|
|
|
$this->context->getObject() |
|
68
|
|
|
->getParent() instanceof FormInterface |
|
69
|
|
|
&& $this->context->getObject() |
|
70
|
|
|
->getParent() |
|
71
|
|
|
->getData() instanceof User) { |
|
72
|
|
|
$user = $this->context->getObject() |
|
73
|
|
|
->getParent() |
|
74
|
|
|
->getData(); |
|
75
|
|
|
|
|
76
|
|
|
//Check if the given code is valid |
|
77
|
|
|
if (!$this->googleAuthenticator->checkCode($user, $value)) { |
|
78
|
|
|
$this->context->addViolation('validator.google_code.wrong_code'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|