1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Challenge; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use LM\AuthAbstractor\Configuration\IApplicationConfiguration; |
9
|
|
|
use LM\AuthAbstractor\Implementation\Member; |
10
|
|
|
use LM\AuthAbstractor\Model\AuthenticationProcess; |
11
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationProcess; |
12
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType; |
14
|
|
|
use LM\AuthAbstractor\Form\Constraint\ValidNewPassword; |
15
|
|
|
use LM\AuthAbstractor\Implementation\ChallengeResponse; |
16
|
|
|
use LM\AuthAbstractor\Model\IChallengeResponse; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\RepeatedType; |
18
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
19
|
|
|
use Symfony\Component\Form\FormError; |
20
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use Twig_Environment; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
use LM\AuthAbstractor\Enum\Persistence\Operation; |
25
|
|
|
use LM\Common\Model\ArrayObject; |
26
|
|
|
use LM\AuthAbstractor\Model\PersistOperation; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A challenge for registering credentials (username + password) from the user. |
30
|
|
|
*/ |
31
|
|
|
class CredentialRegistrationChallenge implements IChallenge |
32
|
|
|
{ |
33
|
|
|
/** @var IApplicationConfiguration */ |
34
|
|
|
private $appConfig; |
35
|
|
|
|
36
|
|
|
/** @var ValidNewPassword */ |
37
|
|
|
private $constraint; |
38
|
|
|
|
39
|
|
|
/** @var FormFactoryInterface */ |
40
|
|
|
private $formFactory; |
41
|
|
|
|
42
|
|
|
/** @var HttpFoundationFactory */ |
43
|
|
|
private $httpFoundationFactory; |
44
|
|
|
|
45
|
|
|
/** @var Twig_Environment */ |
46
|
|
|
private $twig; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @internal |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
IApplicationConfiguration $appConfig, |
53
|
|
|
ValidNewPassword $constraint, |
54
|
|
|
FormFactoryInterface $formFactory, |
55
|
|
|
HttpFoundationFactory $httpFoundationFactory, |
56
|
|
|
Twig_Environment $twig |
57
|
|
|
) { |
58
|
|
|
$this->appConfig = $appConfig; |
59
|
|
|
$this->constraint = $constraint; |
60
|
|
|
$this->formFactory = $formFactory; |
61
|
|
|
$this->httpFoundationFactory = $httpFoundationFactory; |
62
|
|
|
$this->twig = $twig; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @internal |
67
|
|
|
*/ |
68
|
|
|
public function process( |
69
|
|
|
IAuthenticationProcess $process, |
70
|
|
|
?ServerRequestInterface $httpRequest |
71
|
|
|
): IChallengeResponse { |
72
|
|
|
$form = $this |
73
|
|
|
->formFactory |
74
|
|
|
->createBuilder() |
75
|
|
|
->add('username') |
76
|
|
|
->add('password', RepeatedType::class, [ |
77
|
|
|
'constraints' => [ |
78
|
|
|
$this->constraint, |
79
|
|
|
], |
80
|
|
|
'type' => PasswordType::class, |
81
|
|
|
'invalid_message' => 'The password fields must match.', |
82
|
|
|
'required' => true, |
83
|
|
|
'first_options' => ['label' => 'Password'], |
84
|
|
|
'second_options' => ['label' => 'Repeat Password'], |
85
|
|
|
]) |
86
|
|
|
->add('submit', SubmitType::class) |
87
|
|
|
->getForm() |
88
|
|
|
; |
89
|
|
|
|
90
|
|
|
if (null !== $httpRequest) { |
91
|
|
|
$form->handleRequest($this->httpFoundationFactory->createRequest($httpRequest)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($form->isSubmitted() && $this->appConfig->isExistingMember($form['username']->getData())) { |
95
|
|
|
$form |
96
|
|
|
->get('username') |
97
|
|
|
->addError(new FormError('The username is already taken.')) |
98
|
|
|
; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
102
|
|
|
$persistOperations = $process |
|
|
|
|
103
|
|
|
->getTypedMap() |
104
|
|
|
->has('persist_operations') ? $process |
|
|
|
|
105
|
|
|
->getTypedMap() |
106
|
|
|
->get('persist_operations', ArrayObject::class) |
107
|
|
|
: new ArrayObject([], PersistOperation::class); |
|
|
|
|
108
|
|
|
// var_dump(Request::createFromGlobals()); |
109
|
|
|
$member = new Member( |
110
|
|
|
password_hash($form->get('password')->getData(), PASSWORD_DEFAULT), |
111
|
|
|
$form->get('username')->getData() |
112
|
|
|
); |
113
|
|
|
$newDm = $process |
|
|
|
|
114
|
|
|
->getTypedMap() |
115
|
|
|
->add( |
116
|
|
|
'member', |
117
|
|
|
$member, |
118
|
|
|
Member::class |
119
|
|
|
) |
120
|
|
|
->set( |
121
|
|
|
'persist_operations', |
122
|
|
|
$persistOperations->add( |
123
|
|
|
new PersistOperation($member, new Operation(Operation::CREATE)), |
124
|
|
|
PersistOperation::class |
125
|
|
|
), |
126
|
|
|
ArrayObject::class |
127
|
|
|
) |
128
|
|
|
; |
129
|
|
|
|
130
|
|
|
return new ChallengeResponse( |
131
|
|
|
new AuthenticationProcess($newDm), |
132
|
|
|
null, |
133
|
|
|
false, |
134
|
|
|
true |
135
|
|
|
) |
136
|
|
|
; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$response = new Response($this->twig->render('credential_registration.html.twig', [ |
140
|
|
|
'form' => $form->createView(), |
141
|
|
|
])); |
142
|
|
|
|
143
|
|
|
return new ChallengeResponse( |
144
|
|
|
$process, |
145
|
|
|
$response, |
146
|
|
|
false, |
147
|
|
|
false |
148
|
|
|
) |
149
|
|
|
; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|