|
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\Form\Constraint\ValidNewPassword; |
|
10
|
|
|
use LM\AuthAbstractor\Implementation\ChallengeResponse; |
|
11
|
|
|
use LM\AuthAbstractor\Model\AuthenticationProcess; |
|
12
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationProcess; |
|
13
|
|
|
use LM\Common\Enum\Scalar; |
|
14
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
|
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType; |
|
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\RepeatedType; |
|
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
18
|
|
|
use LM\AuthAbstractor\Model\IChallengeResponse; |
|
19
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
use Twig_Environment; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* A challenge allowing the user to update their passwords. The requirements for |
|
25
|
|
|
* the password are defined in the application configuration. |
|
26
|
|
|
*/ |
|
27
|
|
|
class PasswordUpdateChallenge implements IChallenge |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var IApplicationConfiguration */ |
|
30
|
|
|
private $appConfig; |
|
31
|
|
|
|
|
32
|
|
|
/** @var ValidNewPassword */ |
|
33
|
|
|
private $constraint; |
|
34
|
|
|
|
|
35
|
|
|
/** @var FormFactoryInterface */ |
|
36
|
|
|
private $formFactory; |
|
37
|
|
|
|
|
38
|
|
|
/** @var HttpFoundationFactory */ |
|
39
|
|
|
private $httpFoundationFactory; |
|
40
|
|
|
|
|
41
|
|
|
/** @var Twig_Environment */ |
|
42
|
|
|
private $twig; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @internal |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( |
|
48
|
|
|
IApplicationConfiguration $appConfig, |
|
49
|
|
|
ValidNewPassword $constraint, |
|
50
|
|
|
FormFactoryInterface $formFactory, |
|
51
|
|
|
HttpFoundationFactory $httpFoundationFactory, |
|
52
|
|
|
Twig_Environment $twig |
|
53
|
|
|
) { |
|
54
|
|
|
$this->appConfig = $appConfig; |
|
55
|
|
|
$this->constraint = $constraint; |
|
56
|
|
|
$this->formFactory = $formFactory; |
|
57
|
|
|
$this->httpFoundationFactory = $httpFoundationFactory; |
|
58
|
|
|
$this->twig = $twig; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function process( |
|
62
|
|
|
IAuthenticationProcess $process, |
|
63
|
|
|
?ServerRequestInterface $httpRequest |
|
64
|
|
|
): IChallengeResponse { |
|
65
|
|
|
$form = $this |
|
66
|
|
|
->formFactory |
|
67
|
|
|
->createBuilder() |
|
68
|
|
|
->add('password', RepeatedType::class, [ |
|
69
|
|
|
'constraints' => [ |
|
70
|
|
|
$this->constraint, |
|
71
|
|
|
], |
|
72
|
|
|
'type' => PasswordType::class, |
|
73
|
|
|
'invalid_message' => 'The password fields must match.', |
|
74
|
|
|
'required' => true, |
|
75
|
|
|
'first_options' => ['label' => 'Password'], |
|
76
|
|
|
'second_options' => ['label' => 'Repeat Password'], |
|
77
|
|
|
]) |
|
78
|
|
|
->add('submit', SubmitType::class) |
|
79
|
|
|
->getForm() |
|
80
|
|
|
; |
|
81
|
|
|
|
|
82
|
|
|
if (null !== $httpRequest) { |
|
83
|
|
|
$form->handleRequest($this->httpFoundationFactory->createRequest($httpRequest)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
87
|
|
|
$newDm = $process |
|
|
|
|
|
|
88
|
|
|
->getTypedMap() |
|
89
|
|
|
->add( |
|
90
|
|
|
'new_password', |
|
91
|
|
|
password_hash($form->get('password')->getData(), PASSWORD_DEFAULT), |
|
92
|
|
|
Scalar::_STR |
|
93
|
|
|
) |
|
94
|
|
|
; |
|
95
|
|
|
|
|
96
|
|
|
return new ChallengeResponse( |
|
97
|
|
|
new AuthenticationProcess($newDm), |
|
98
|
|
|
null, |
|
99
|
|
|
false, |
|
100
|
|
|
true |
|
101
|
|
|
) |
|
102
|
|
|
; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$response = new Response($this->twig->render('password_update.html.twig', [ |
|
106
|
|
|
'form' => $form->createView(), |
|
107
|
|
|
])); |
|
108
|
|
|
|
|
109
|
|
|
return new ChallengeResponse( |
|
110
|
|
|
$process, |
|
111
|
|
|
$response, |
|
112
|
|
|
$form->isSubmitted(), |
|
113
|
|
|
false |
|
114
|
|
|
) |
|
115
|
|
|
; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|