1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* of the License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace App\Validator\Constraints; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use App\Entity\UserSystem\User; |
26
|
|
|
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticator; |
27
|
|
|
use Symfony\Component\Form\Form; |
28
|
|
|
use Symfony\Component\Form\FormInterface; |
29
|
|
|
use Symfony\Component\Validator\Constraint; |
30
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
31
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
32
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedValueException; |
33
|
|
|
|
34
|
|
|
class ValidGoogleAuthCodeValidator extends ConstraintValidator |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
protected $googleAuthenticator; |
38
|
|
|
|
39
|
|
|
public function __construct(GoogleAuthenticator $googleAuthenticator) |
40
|
|
|
{ |
41
|
|
|
$this->googleAuthenticator = $googleAuthenticator; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function validate($value, Constraint $constraint) |
48
|
|
|
{ |
49
|
|
|
if (!$constraint instanceof ValidGoogleAuthCode) { |
50
|
|
|
throw new UnexpectedTypeException($constraint, ValidGoogleAuthCode::class); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (null === $value || '' === $value) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!\is_string($value)) { |
58
|
|
|
throw new UnexpectedValueException($value, 'string'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if(!ctype_digit($value)) { |
62
|
|
|
$this->context->addViolation('validator.google_code.only_digits_allowed'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
//Number must have 6 digits |
66
|
|
|
if(strlen($value) !== 6) { |
67
|
|
|
$this->context->addViolation('validator.google_code.wrong_digit_count'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
//Try to retrieve the user we want to check |
71
|
|
|
if($this->context->getObject() instanceof FormInterface && |
72
|
|
|
$this->context->getObject()->getParent() instanceof FormInterface |
73
|
|
|
&& $this->context->getObject()->getParent()->getData() instanceof User) { |
74
|
|
|
$user = $this->context->getObject()->getParent()->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
|
|
|
} |