|
1
|
|
|
<?php |
|
2
|
|
|
namespace AppBundle\Security; |
|
3
|
|
|
|
|
4
|
|
|
use AppBundle\Form\Type\LoginFormType; |
|
5
|
|
|
use AppBundle\Service\UserService; |
|
6
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
12
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
13
|
|
|
use Symfony\Component\Security\Core\Security; |
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
15
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
16
|
|
|
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; |
|
17
|
|
|
|
|
18
|
|
|
class FormAuthenticator extends AbstractGuardAuthenticator |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var FormFactoryInterface */ |
|
21
|
|
|
private $formFactory; |
|
22
|
|
|
|
|
23
|
|
|
/** @var RouterInterface */ |
|
24
|
|
|
private $router; |
|
25
|
|
|
|
|
26
|
|
|
/** @var UserService */ |
|
27
|
|
|
private $userService; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(FormFactoryInterface $formFactory, RouterInterface $router, UserService $userService) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->formFactory = $formFactory; |
|
32
|
|
|
$this->router = $router; |
|
33
|
|
|
$this->userService = $userService; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
//region GuardAuthenticatorInterface |
|
37
|
|
|
|
|
38
|
|
|
//region AuthenticationEntryPointInterface |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritdoc |
|
42
|
|
|
* |
|
43
|
|
|
* @param Request $request The request that resulted in an AuthenticationException |
|
44
|
|
|
* @param AuthenticationException $authException The exception that started the authentication process |
|
45
|
|
|
* |
|
46
|
|
|
* @return Response |
|
47
|
|
|
*/ |
|
48
|
|
|
public function start(Request $request, AuthenticationException $authException = null) |
|
49
|
|
|
{ |
|
50
|
|
|
return new RedirectResponse($this->getLoginPath()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
//endregion |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
* |
|
58
|
|
|
* @param Request $request |
|
59
|
|
|
* |
|
60
|
|
|
* @return UserCredentials|null |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getCredentials(Request $request) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!$request->isMethod('POST')) { |
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($request->getPathInfo() !== $this->getLoginPath()) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$form = $this->formFactory->create(LoginFormType::class, null, []); |
|
73
|
|
|
$form->handleRequest($request); |
|
74
|
|
|
|
|
75
|
|
|
$username = $form->get('username')->getData(); |
|
76
|
|
|
$this->setLastAuthenticationUsername($request, $username); |
|
77
|
|
|
|
|
78
|
|
|
$credentials = new UserCredentials( |
|
79
|
|
|
$username, |
|
80
|
|
|
$form->get('password')->getData(), |
|
81
|
|
|
$form->get('type')->getData() |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
return $credentials; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritdoc |
|
89
|
|
|
* |
|
90
|
|
|
* @param UserCredentials $credentials |
|
91
|
|
|
* @param UserProviderInterface $userProvider |
|
92
|
|
|
* |
|
93
|
|
|
* @throws AuthenticationException |
|
94
|
|
|
* |
|
95
|
|
|
* @return UserInterface|null |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider) |
|
98
|
|
|
{ |
|
99
|
|
|
return $userProvider->loadUserByUsername($credentials->getUsernameCompound()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritdoc |
|
104
|
|
|
* |
|
105
|
|
|
* @param UserCredentials $credentials |
|
106
|
|
|
* @param UserInterface $user |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public function checkCredentials($credentials, UserInterface $user) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->userService->validatePassword($user, $credentials->getPassword()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Called when authentication executed, but failed (e.g. wrong username password). |
|
117
|
|
|
* |
|
118
|
|
|
* @inheritdoc |
|
119
|
|
|
* |
|
120
|
|
|
* @param Request $request |
|
121
|
|
|
* @param AuthenticationException $exception |
|
122
|
|
|
* |
|
123
|
|
|
* @return Response|null |
|
124
|
|
|
*/ |
|
125
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->setLastAuthenticationError($request, $exception); |
|
128
|
|
|
|
|
129
|
|
|
return new RedirectResponse($this->getLoginPath()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Called when authentication executed and was successful! |
|
134
|
|
|
* |
|
135
|
|
|
* @inheritdoc |
|
136
|
|
|
* |
|
137
|
|
|
* @param Request $request |
|
138
|
|
|
* @param TokenInterface $token |
|
139
|
|
|
* @param string $providerKey The provider (i.e. firewall) key |
|
140
|
|
|
* |
|
141
|
|
|
* @return Response|null |
|
142
|
|
|
*/ |
|
143
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
|
144
|
|
|
{ |
|
145
|
|
|
$targetPath = $this->getTargetPath($request, $providerKey); |
|
146
|
|
|
|
|
147
|
|
|
if (empty($targetPath)) { |
|
148
|
|
|
$targetPath = $this->getHomepagePath(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return new RedirectResponse($targetPath); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Authenticator supports "remember me" functionality |
|
156
|
|
|
* |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
|
|
public function supportsRememberMe() |
|
160
|
|
|
{ |
|
161
|
|
|
return true; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
//endregion |
|
165
|
|
|
|
|
166
|
|
|
//region Page URLs |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Homepage URI |
|
170
|
|
|
* |
|
171
|
|
|
* @return string |
|
172
|
|
|
*/ |
|
173
|
|
|
private function getHomepagePath() |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->router->generate('homepage'); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Login page URI |
|
180
|
|
|
* |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
private function getLoginPath() |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->router->generate('login'); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
//endregion |
|
189
|
|
|
|
|
190
|
|
|
//region Authentication utils |
|
191
|
|
|
|
|
192
|
|
|
private function getTargetPath(Request $request, $providerKey) |
|
193
|
|
|
{ |
|
194
|
|
|
return $request->getSession()->get('_security.'.$providerKey.'.target_path'); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
private function setLastAuthenticationError(Request $request, AuthenticationException $exception) |
|
198
|
|
|
{ |
|
199
|
|
|
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
private function setLastAuthenticationUsername(Request $request, $username) |
|
203
|
|
|
{ |
|
204
|
|
|
$request->getSession()->set(Security::LAST_USERNAME, $username); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
//endregion |
|
208
|
|
|
} |
|
209
|
|
|
|