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\Model\IChallengeResponse; |
9
|
|
|
use LM\AuthAbstractor\Model\AuthenticationProcess; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use LM\Common\Enum\Scalar; |
12
|
|
|
use LM\AuthAbstractor\Implementation\ChallengeResponse; |
13
|
|
|
use Twig_Environment; |
14
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
15
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
17
|
|
|
use LM\AuthAbstractor\Configuration\IApplicationConfiguration; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* A challenge for asking the user for their email address. |
22
|
|
|
* |
23
|
|
|
* @todo There should be an alternative, combined with |
24
|
|
|
* CredentialRegistrationChallenge. |
25
|
|
|
*/ |
26
|
|
|
class EmailRegistrationChallenge implements IChallenge |
27
|
|
|
{ |
28
|
|
|
/** @var int */ |
29
|
|
|
const CODE_MIN = 0; |
30
|
|
|
|
31
|
|
|
/** @var int */ |
32
|
|
|
const CODE_MAX = 999999; |
33
|
|
|
|
34
|
|
|
/** @var IApplicationConfiguration */ |
35
|
|
|
private $appConfig; |
36
|
|
|
|
37
|
|
|
/** @var FormFactoryInterface */ |
38
|
|
|
private $formFactory; |
39
|
|
|
|
40
|
|
|
/** @var HttpFoundationFactory */ |
41
|
|
|
private $httpFoundationFactory; |
42
|
|
|
|
43
|
|
|
/** @var Twig_Environment */ |
44
|
|
|
private $twig; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @internal |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
IApplicationConfiguration $appConfig, |
51
|
|
|
FormFactoryInterface $formFactory, |
52
|
|
|
HttpFoundationFactory $httpFoundationFactory, |
53
|
|
|
Twig_Environment $twig |
54
|
|
|
) { |
55
|
|
|
$this->appConfig = $appConfig; |
56
|
|
|
$this->formFactory = $formFactory; |
57
|
|
|
$this->httpFoundationFactory = $httpFoundationFactory; |
58
|
|
|
$this->twig = $twig; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @internal |
63
|
|
|
*/ |
64
|
|
|
public function process( |
65
|
|
|
IAuthenticationProcess $authenticationProcess, |
66
|
|
|
?ServerRequestInterface $httpRequest |
67
|
|
|
): IChallengeResponse { |
68
|
|
|
$form = $this |
69
|
|
|
->formFactory |
70
|
|
|
->createBuilder() |
71
|
|
|
->add('email') |
72
|
|
|
->add('submit', SubmitType::class) |
73
|
|
|
->getForm() |
74
|
|
|
; |
75
|
|
|
|
76
|
|
|
if (null !== $httpRequest) { |
77
|
|
|
$form->handleRequest( |
78
|
|
|
$this->httpFoundationFactory->createRequest($httpRequest) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
83
|
|
|
return new ChallengeResponse( |
84
|
|
|
new AuthenticationProcess( |
85
|
|
|
$authenticationProcess |
|
|
|
|
86
|
|
|
->getTypedMap() |
87
|
|
|
->set( |
88
|
|
|
'email', |
89
|
|
|
$form['email']->getData(), |
90
|
|
|
Scalar::_STR |
91
|
|
|
) |
92
|
|
|
), |
93
|
|
|
null, |
94
|
|
|
false, |
95
|
|
|
true |
96
|
|
|
) |
97
|
|
|
; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$response = new Response($this->twig->render('email_registration.html.twig', [ |
101
|
|
|
"form" => $form->createView(), |
102
|
|
|
])); |
103
|
|
|
|
104
|
|
|
return new ChallengeResponse( |
105
|
|
|
$authenticationProcess, |
106
|
|
|
$response, |
107
|
|
|
false, |
108
|
|
|
false |
109
|
|
|
) |
110
|
|
|
; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|