1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Challenge; |
6
|
|
|
|
7
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationProcess; |
8
|
|
|
use LM\AuthAbstractor\Implementation\ChallengeResponse; |
9
|
|
|
use LM\AuthAbstractor\Model\IChallengeResponse; |
10
|
|
|
use LM\AuthAbstractor\Model\AuthenticationProcess; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
12
|
|
|
use LM\Common\Enum\Scalar; |
13
|
|
|
use Twig_Environment; |
14
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
15
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
16
|
|
|
use LM\AuthAbstractor\Model\IMailer; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
18
|
|
|
use LM\AuthAbstractor\Configuration\IApplicationConfiguration; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\Form\FormError; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A challenge for asking the user to enter a code sent by email. |
24
|
|
|
*/ |
25
|
|
|
class EmailChallenge implements IChallenge |
26
|
|
|
{ |
27
|
|
|
/** @var int */ |
28
|
|
|
const CODE_MIN = 0; |
29
|
|
|
|
30
|
|
|
/** @var int */ |
31
|
|
|
const CODE_MAX = 999999; |
32
|
|
|
|
33
|
|
|
/** @var IApplicationConfiguration */ |
34
|
|
|
private $appConfig; |
35
|
|
|
|
36
|
|
|
/** @var FormFactoryInterface */ |
37
|
|
|
private $formFactory; |
38
|
|
|
|
39
|
|
|
/** @var HttpFoundationFactory */ |
40
|
|
|
private $httpFoundationFactory; |
41
|
|
|
|
42
|
|
|
/** @var Twig_Environment */ |
43
|
|
|
private $twig; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @internal |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
IApplicationConfiguration $appConfig, |
50
|
|
|
FormFactoryInterface $formFactory, |
51
|
|
|
HttpFoundationFactory $httpFoundationFactory, |
52
|
|
|
Twig_Environment $twig |
53
|
|
|
) { |
54
|
|
|
$this->appConfig = $appConfig; |
55
|
|
|
$this->formFactory = $formFactory; |
56
|
|
|
$this->httpFoundationFactory = $httpFoundationFactory; |
57
|
|
|
$this->twig = $twig; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @internal |
62
|
|
|
*/ |
63
|
|
|
public function process( |
64
|
|
|
IAuthenticationProcess $authenticationProcess, |
65
|
|
|
?ServerRequestInterface $httpRequest |
66
|
|
|
): IChallengeResponse { |
67
|
|
|
$email = $authenticationProcess |
|
|
|
|
68
|
|
|
->getTypedMap() |
69
|
|
|
->get('email', Scalar::_STR) |
70
|
|
|
; |
71
|
|
|
|
72
|
|
|
$form = $this |
73
|
|
|
->formFactory |
74
|
|
|
->createBuilder() |
75
|
|
|
->add('emailCode') |
76
|
|
|
->add('submit', SubmitType::class) |
77
|
|
|
->getForm() |
78
|
|
|
; |
79
|
|
|
|
80
|
|
|
if (null !== $httpRequest) { |
81
|
|
|
$form->handleRequest( |
82
|
|
|
$this->httpFoundationFactory->createRequest($httpRequest) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ( |
87
|
|
|
$form->isSubmitted() && |
88
|
|
|
$form->isValid() && |
89
|
|
|
null!== $httpRequest |
90
|
|
|
) { |
91
|
|
|
$code = $authenticationProcess |
|
|
|
|
92
|
|
|
->getTypedMap() |
93
|
|
|
->get('email_code_hash', Scalar::_STR) |
94
|
|
|
; |
95
|
|
|
$isCodeCorrect = password_verify( |
96
|
|
|
$form['emailCode']->getData(), |
97
|
|
|
$code |
|
|
|
|
98
|
|
|
); |
99
|
|
|
if (true !== $isCodeCorrect) { |
100
|
|
|
$form->addError(new FormError('The code you entered is incorrect')); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
105
|
|
|
return new ChallengeResponse( |
106
|
|
|
$authenticationProcess, |
107
|
|
|
null, |
108
|
|
|
false, |
109
|
|
|
true |
110
|
|
|
) |
111
|
|
|
; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$code = random_int(self::CODE_MIN, self::CODE_MAX); |
115
|
|
|
|
116
|
|
|
$email = $authenticationProcess |
|
|
|
|
117
|
|
|
->getTypedMap() |
118
|
|
|
->get('mailer', IMailer::class) |
119
|
|
|
->send($email, (string) $code) |
|
|
|
|
120
|
|
|
; |
121
|
|
|
|
122
|
|
|
$response = new Response($this->twig->render('email.html.twig', [ |
123
|
|
|
"form" => $form->createView(), |
124
|
|
|
])); |
125
|
|
|
|
126
|
|
|
return new ChallengeResponse( |
127
|
|
|
new AuthenticationProcess( |
128
|
|
|
$authenticationProcess |
|
|
|
|
129
|
|
|
->getTypedMap() |
130
|
|
|
->set( |
131
|
|
|
'email_code_hash', |
132
|
|
|
password_hash((string) $code, PASSWORD_DEFAULT), |
133
|
|
|
Scalar::_STR |
134
|
|
|
) |
135
|
|
|
), |
136
|
|
|
$response, |
137
|
|
|
$form->isSubmitted(), |
138
|
|
|
false |
139
|
|
|
) |
140
|
|
|
; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|