|
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\Model\AuthenticationProcess; |
|
10
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationProcess; |
|
11
|
|
|
use LM\Common\Model\StringObject; |
|
12
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
|
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
15
|
|
|
use LM\AuthAbstractor\Implementation\ChallengeResponse; |
|
16
|
|
|
use Symfony\Component\Form\FormError; |
|
17
|
|
|
use LM\AuthAbstractor\Model\IChallengeResponse; |
|
18
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
20
|
|
|
use Twig_Environment; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* A challenge for asking the user to enter an existing username, which can |
|
24
|
|
|
* be used by following challenges (e.g. PasswordChallenge or U2fChallenge). |
|
25
|
|
|
*/ |
|
26
|
|
|
class ExistingUsernameChallenge implements IChallenge |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var IApplicationConfiguration */ |
|
29
|
|
|
private $appConfig; |
|
30
|
|
|
|
|
31
|
|
|
/** @var FormFactoryInterface */ |
|
32
|
|
|
private $formFactory; |
|
33
|
|
|
|
|
34
|
|
|
/** @var HttpFoundationFactory */ |
|
35
|
|
|
private $httpFoundationFactory; |
|
36
|
|
|
|
|
37
|
|
|
/** @var Twig_Environment */ |
|
38
|
|
|
private $twig; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @internal |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
IApplicationConfiguration $appConfig, |
|
45
|
|
|
FormFactoryInterface $formFactory, |
|
46
|
|
|
HttpFoundationFactory $httpFoundationFactory, |
|
47
|
|
|
Twig_Environment $twig |
|
48
|
|
|
) { |
|
49
|
|
|
$this->appConfig = $appConfig; |
|
50
|
|
|
$this->formFactory = $formFactory; |
|
51
|
|
|
$this->httpFoundationFactory = $httpFoundationFactory; |
|
52
|
|
|
$this->twig = $twig; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @internal |
|
57
|
|
|
*/ |
|
58
|
|
|
public function process( |
|
59
|
|
|
IAuthenticationProcess $process, |
|
60
|
|
|
?ServerRequestInterface $httpRequest |
|
61
|
|
|
): IChallengeResponse { |
|
62
|
|
|
$form = $this |
|
63
|
|
|
->formFactory |
|
64
|
|
|
->createBuilder() |
|
65
|
|
|
->add('username', TextType::class) |
|
66
|
|
|
->add('submit', SubmitType::class) |
|
67
|
|
|
->getForm() |
|
68
|
|
|
; |
|
69
|
|
|
|
|
70
|
|
|
if (null !== $httpRequest) { |
|
71
|
|
|
$form->handleRequest($this->httpFoundationFactory->createRequest($httpRequest)); |
|
72
|
|
|
} |
|
73
|
|
|
if ($form->isSubmitted() && !$this->appConfig->isExistingMember($form['username']->getData())) { |
|
74
|
|
|
$form |
|
75
|
|
|
->get('username') |
|
76
|
|
|
->addError(new FormError('The username is invalid.')) |
|
77
|
|
|
; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
81
|
|
|
$newDm = $process |
|
|
|
|
|
|
82
|
|
|
->getTypedMap() |
|
83
|
|
|
->add( |
|
84
|
|
|
'username', |
|
85
|
|
|
new StringObject($form->get('username')->getData()), |
|
86
|
|
|
StringObject::class |
|
87
|
|
|
) |
|
88
|
|
|
; |
|
89
|
|
|
|
|
90
|
|
|
return new ChallengeResponse( |
|
91
|
|
|
new AuthenticationProcess($newDm), |
|
92
|
|
|
null, |
|
93
|
|
|
false, |
|
94
|
|
|
true |
|
95
|
|
|
) |
|
96
|
|
|
; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$response = new Response($this->twig->render('existing_username.html.twig', [ |
|
100
|
|
|
"form" => $form->createView(), |
|
101
|
|
|
])); |
|
102
|
|
|
|
|
103
|
|
|
return new ChallengeResponse( |
|
104
|
|
|
$process, |
|
105
|
|
|
$response, |
|
106
|
|
|
$form->isSubmitted(), |
|
107
|
|
|
false |
|
108
|
|
|
) |
|
109
|
|
|
; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|