1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Challenge; |
6
|
|
|
|
7
|
|
|
use LM\AuthAbstractor\Configuration\IApplicationConfiguration; |
8
|
|
|
use LM\AuthAbstractor\Model\AuthenticationProcess; |
9
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationProcess; |
10
|
|
|
use LM\Common\Model\StringObject; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
12
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
13
|
|
|
use LM\AuthAbstractor\Model\IChallengeResponse; |
14
|
|
|
use LM\AuthAbstractor\Implementation\ChallengeResponse; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
18
|
|
|
use Symfony\Component\Form\FormError; |
19
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
20
|
|
|
use Twig_Environment; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A challenge providing credential-based authentication (username + password). |
24
|
|
|
*/ |
25
|
|
|
class CredentialChallenge implements IChallenge |
26
|
|
|
{ |
27
|
|
|
/** @var IApplicationConfiguration */ |
28
|
|
|
private $appConfig; |
29
|
|
|
|
30
|
|
|
/** @var FormFactoryInterface */ |
31
|
|
|
private $formFactory; |
32
|
|
|
|
33
|
|
|
/** @var HttpFoundationFactory */ |
34
|
|
|
private $httpFoundationFactory; |
35
|
|
|
|
36
|
|
|
/** @var Twig_Environment */ |
37
|
|
|
private $twig; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @internal |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
IApplicationConfiguration $appConfig, |
44
|
|
|
FormFactoryInterface $formFactory, |
45
|
|
|
HttpFoundationFactory $httpFoundationFactory, |
46
|
|
|
Twig_Environment $twig |
47
|
|
|
) { |
48
|
|
|
$this->appConfig = $appConfig; |
49
|
|
|
$this->formFactory = $formFactory; |
50
|
|
|
$this->httpFoundationFactory = $httpFoundationFactory; |
51
|
|
|
$this->twig = $twig; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @internal |
56
|
|
|
*/ |
57
|
|
|
public function process( |
58
|
|
|
IAuthenticationProcess $process, |
59
|
|
|
?ServerRequestInterface $httpRequest |
60
|
|
|
): IChallengeResponse { |
61
|
|
|
$form = $this |
62
|
|
|
->formFactory |
63
|
|
|
->createBuilder() |
64
|
|
|
->add('username') |
65
|
|
|
->add('password', PasswordType::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()) { |
74
|
|
|
if (!$this->appConfig->isExistingMember($form['username']->getData())) { |
75
|
|
|
$form->addError(new FormError('Invalid credentials')); |
76
|
|
|
} else { |
77
|
|
|
$member = $this->appConfig->getMember($form['username']->getData()); |
78
|
|
|
if (!password_verify($form['password']->getData(), $member->getHashedPassword())) { |
79
|
|
|
$form->addError(new FormError('Invalid credentials')); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
84
|
|
|
$authProcess = new AuthenticationProcess($process |
|
|
|
|
85
|
|
|
->getTypedMap() |
86
|
|
|
->add( |
87
|
|
|
'username', |
88
|
|
|
new StringObject($form['username']->getData()), |
89
|
|
|
StringObject::class |
90
|
|
|
)) |
91
|
|
|
; |
92
|
|
|
|
93
|
|
|
return new ChallengeResponse( |
94
|
|
|
$authProcess, |
95
|
|
|
null, |
96
|
|
|
false, |
97
|
|
|
true |
98
|
|
|
) |
99
|
|
|
; |
100
|
|
|
} |
101
|
|
|
$httpResponse = new Response($this->twig->render("credential_authentication.html.twig", [ |
102
|
|
|
"form" => $form->createView(), |
103
|
|
|
])); |
104
|
|
|
|
105
|
|
|
return new ChallengeResponse( |
106
|
|
|
$process, |
107
|
|
|
$httpResponse, |
108
|
|
|
$form->isSubmitted(), |
109
|
|
|
false |
110
|
|
|
) |
111
|
|
|
; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|