|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use DI\Container; |
|
8
|
|
|
use DI\ContainerBuilder; |
|
9
|
|
|
use LM\AuthAbstractor\Configuration\IApplicationConfiguration; |
|
10
|
|
|
use LM\AuthAbstractor\Factory\AuthenticationProcessFactory; |
|
11
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationKernel; |
|
12
|
|
|
use LM\AuthAbstractor\Model\AuthenticationProcess; |
|
13
|
|
|
use LM\AuthAbstractor\Model\IAuthentifierResponse; |
|
14
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationCallback; |
|
15
|
|
|
use LM\AuthAbstractor\Exception\FinishedProcessException; |
|
16
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
17
|
|
|
use Symfony\Bridge\Twig\Extension\FormExtension; |
|
18
|
|
|
use Symfony\Bridge\Twig\Extension\TranslationExtension; |
|
19
|
|
|
use Symfony\Bridge\Twig\Form\TwigRendererEngine; |
|
20
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
21
|
|
|
use Symfony\Component\Form\Forms; |
|
22
|
|
|
use Symfony\Component\Form\Extension\Csrf\CsrfExtension; |
|
23
|
|
|
use Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator; |
|
24
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManager; |
|
25
|
|
|
use Symfony\Component\Form\FormRenderer; |
|
26
|
|
|
use Symfony\Component\Translation\Translator; |
|
27
|
|
|
use Symfony\Component\Translation\Loader\XliffFileLoader; |
|
28
|
|
|
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension; |
|
29
|
|
|
use Symfony\Component\Form\Extension\Validator\ValidatorExtension; |
|
30
|
|
|
use Twig_Environment; |
|
31
|
|
|
use Twig_FactoryRuntimeLoader; |
|
32
|
|
|
use Twig_Function; |
|
33
|
|
|
use Psr\Container\ContainerInterface; |
|
34
|
|
|
use Twig_Loader_Filesystem; |
|
35
|
|
|
use Symfony\Component\Validator\Validation; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* This is the main entry point of the library. It acts as an HTTP middleware. |
|
39
|
|
|
* |
|
40
|
|
|
* It is initialised with the configuration of the application. It can then |
|
41
|
|
|
* be passed an HTTP request among other things and it will return an HTTP |
|
42
|
|
|
* response (among other things). |
|
43
|
|
|
* |
|
44
|
|
|
* @todo Make it implement PSR-15. |
|
45
|
|
|
*/ |
|
46
|
|
|
class AuthenticationKernel implements IAuthenticationKernel |
|
47
|
|
|
{ |
|
48
|
|
|
/** @var IApplicationConfiguration */ |
|
49
|
|
|
private $appConfig; |
|
50
|
|
|
|
|
51
|
|
|
/** @var ContainerInterface */ |
|
52
|
|
|
private $container; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param IApplicationConfiguration $appConfig The configuration of the |
|
56
|
|
|
* application. It needs to be an implementation of |
|
57
|
|
|
* IApplicationConfiguration. You are not obliged to define your own |
|
58
|
|
|
* implementation, and can just use the Configuration class for convenience. |
|
59
|
|
|
* @todo Ensure container keeps and reuses objects. |
|
60
|
|
|
* @todo Form validation doesn't work. Delete? |
|
61
|
|
|
* @todo Make it possible to specify an anonymous challenge. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct(IApplicationConfiguration $appConfig) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->appConfig = $appConfig; |
|
66
|
|
|
|
|
67
|
|
|
$twigPaths = [ |
|
68
|
|
|
$appConfig->getLibdir().'/templates', |
|
69
|
|
|
$appConfig->getComposerDir().'/symfony/twig-bridge/Resources/views/Form', |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
if (null !== $appConfig->getCustomTwigDir()) { |
|
73
|
|
|
$twigPaths[] = $appConfig->getCustomTwigDir(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$loader = new Twig_Loader_Filesystem($twigPaths); |
|
77
|
|
|
$twig = new Twig_Environment($loader, [ |
|
78
|
|
|
"cache" => false, |
|
79
|
|
|
]); |
|
80
|
|
|
$assetFunction = new Twig_Function("asset", [ |
|
81
|
|
|
$appConfig, |
|
82
|
|
|
"getAssetUri", |
|
83
|
|
|
]); |
|
84
|
|
|
$twig->addFunction($assetFunction); |
|
85
|
|
|
$translator = new Translator('en'); |
|
86
|
|
|
$translator->addLoader('xlf', new XliffFileLoader()); |
|
87
|
|
|
// $translator->addResource( |
|
88
|
|
|
// 'xlf', |
|
89
|
|
|
// __DIR__.'/path/to/translations/messages.en.xlf', |
|
90
|
|
|
// 'en' |
|
91
|
|
|
// ); |
|
92
|
|
|
$twig->addExtension(new TranslationExtension($translator)); |
|
93
|
|
|
|
|
94
|
|
|
$csrfGenerator = new UriSafeTokenGenerator(); |
|
95
|
|
|
$csrfStorage = $this->appConfig->getTokenStorage(); |
|
96
|
|
|
$csrfManager = new CsrfTokenManager($csrfGenerator, $csrfStorage); |
|
97
|
|
|
|
|
98
|
|
|
$defaultFormTheme = "form_div_layout.html.twig"; |
|
99
|
|
|
|
|
100
|
|
|
$formEngine = new TwigRendererEngine([$defaultFormTheme], $twig); |
|
101
|
|
|
$twig->addRuntimeLoader(new Twig_FactoryRuntimeLoader([ |
|
102
|
|
|
FormRenderer::class => function () use ($formEngine, $csrfManager) { |
|
103
|
|
|
return new FormRenderer($formEngine, $csrfManager); |
|
104
|
|
|
}, |
|
105
|
|
|
])); |
|
106
|
|
|
$twig->addExtension(new FormExtension()); |
|
107
|
|
|
$validator = Validation::createValidator(); |
|
108
|
|
|
$formFactory = Forms::createFormFactoryBuilder() |
|
109
|
|
|
->addExtension(new CsrfExtension($csrfManager)) |
|
110
|
|
|
->addExtension(new HttpFoundationExtension()) |
|
111
|
|
|
->addExtension(new HttpFoundationExtension()) |
|
112
|
|
|
->addExtension(new ValidatorExtension($validator)) |
|
113
|
|
|
->getFormFactory() |
|
114
|
|
|
; |
|
115
|
|
|
|
|
116
|
|
|
$containerBuilder = new ContainerBuilder(); |
|
117
|
|
|
$containerBuilder->addDefinitions([ |
|
118
|
|
|
IApplicationConfiguration::class => function () use ($appConfig) { |
|
119
|
|
|
return $appConfig; |
|
120
|
|
|
}, |
|
121
|
|
|
Twig_Environment::class => function () use ($twig) { |
|
122
|
|
|
return $twig; |
|
123
|
|
|
}, |
|
124
|
|
|
FormFactoryInterface::class => function () use ($formFactory) { |
|
125
|
|
|
return $formFactory; |
|
126
|
|
|
}, |
|
127
|
|
|
]); |
|
128
|
|
|
$this->container = $containerBuilder->build(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Used for unit testing only. |
|
133
|
|
|
* |
|
134
|
|
|
* @internal |
|
135
|
|
|
* @return ContainerInterface The container of auth-abstractor. |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getContainer(): ContainerInterface |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->container; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns the authentication process factory, which can be used to create |
|
144
|
|
|
* authentication processes. This is the recommended way to create |
|
145
|
|
|
* authentication processes, as it aims to be more backwards-compatible and |
|
146
|
|
|
* is simply easier to use. |
|
147
|
|
|
* |
|
148
|
|
|
* @return AuthenticationProcessFactory An authenticationProcessFactory |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getAuthenticationProcessFactory(): AuthenticationProcessFactory |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->container->get(AuthenticationProcessFactory::class); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @todo Should check type before instantiating authentifier. |
|
157
|
|
|
* @todo Move somewhere else? |
|
158
|
|
|
*/ |
|
159
|
|
|
public function processHttpRequest( |
|
160
|
|
|
ServerRequestInterface $httpRequest, |
|
161
|
|
|
AuthenticationProcess $process, |
|
162
|
|
|
IAuthenticationCallback $callback |
|
163
|
|
|
): IAuthentifierResponse { |
|
164
|
|
|
if ($process->isFinished()) { |
|
165
|
|
|
throw new FinishedProcessException(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$processHandler = $this |
|
169
|
|
|
->container |
|
170
|
|
|
->get(AuthenticationProcessHandler::class) |
|
171
|
|
|
; |
|
172
|
|
|
|
|
173
|
|
|
$authentifierResponse = null; |
|
174
|
|
|
$lastProcess = $process; |
|
175
|
|
|
$httpResponse = null; |
|
176
|
|
|
while (null === $httpResponse) { |
|
177
|
|
|
$authentifierResponse = $processHandler->handleAuthenticationProcess( |
|
178
|
|
|
$httpRequest, |
|
179
|
|
|
$lastProcess, |
|
180
|
|
|
$callback |
|
181
|
|
|
); |
|
182
|
|
|
$lastProcess = $authentifierResponse->getAuthenticationProcess(); |
|
183
|
|
|
$httpRequest = null; |
|
184
|
|
|
$httpResponse = $authentifierResponse->getHttpResponse(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $authentifierResponse; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|