1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Security; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
6
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\Routing\RouterInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
11
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
13
|
|
|
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; |
14
|
|
|
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException; |
15
|
|
|
use Symfony\Component\Security\Core\Security; |
16
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
17
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
18
|
|
|
use Symfony\Component\Security\Csrf\CsrfToken; |
19
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
20
|
|
|
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator; |
21
|
|
|
use Symfony\Component\Security\Http\Util\TargetPathTrait; |
22
|
|
|
|
23
|
|
|
class FormAuthenticator extends AbstractFormLoginAuthenticator |
24
|
|
|
{ |
25
|
|
|
use TargetPathTrait; |
26
|
|
|
|
27
|
|
|
private $router; |
28
|
|
|
private $csrfTokenManager; |
29
|
|
|
private $passwordEncoder; |
30
|
|
|
|
31
|
|
|
public function __construct(RouterInterface $router, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder) |
32
|
|
|
{ |
33
|
|
|
$this->router = $router; |
34
|
|
|
$this->csrfTokenManager = $csrfTokenManager; |
35
|
|
|
$this->passwordEncoder = $passwordEncoder; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function supports(Request $request) |
39
|
|
|
{ |
40
|
|
|
return 'login' === $request->attributes->get('_route') |
41
|
|
|
&& $request->isMethod('POST'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getCredentials(Request $request) |
45
|
|
|
{ |
46
|
|
|
$credentials = [ |
47
|
|
|
'username' => $request->request->get('username'), |
48
|
|
|
'password' => $request->request->get('password'), |
49
|
|
|
'csrf_token' => $request->request->get('_csrf_token'), |
50
|
|
|
]; |
51
|
|
|
$request->getSession()->set( |
52
|
|
|
Security::LAST_USERNAME, |
53
|
|
|
$credentials['username'] |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
return $credentials; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider) |
60
|
|
|
{ |
61
|
|
|
$token = new CsrfToken('authenticate', $credentials['csrf_token']); |
62
|
|
|
if (!$this->csrfTokenManager->isTokenValid($token)) { |
63
|
|
|
throw new InvalidCsrfTokenException(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Load / create our user however you need. |
67
|
|
|
// You can do this by calling the user provider, or with custom logic here. |
68
|
|
|
$user = $userProvider->loadUserByUsername($credentials['username']); |
69
|
|
|
|
70
|
|
|
if (!$user) { |
|
|
|
|
71
|
|
|
// fail authentication with a custom error |
72
|
|
|
throw new CustomUserMessageAuthenticationException('Username could not be found.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $user; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function checkCredentials($credentials, UserInterface $user) |
79
|
|
|
{ |
80
|
|
|
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
84
|
|
|
{ |
85
|
|
|
return new RedirectResponse($this->router->generate('home')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function start(Request $request, AuthenticationException $authException = null) |
89
|
|
|
{ |
90
|
|
|
if ('json' === $request->getRequestFormat()) { |
91
|
|
|
return new JsonResponse(['error' => 401], Response::HTTP_UNAUTHORIZED); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return parent::start($request, $authException); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function getLoginUrl() |
98
|
|
|
{ |
99
|
|
|
return $this->router->generate('login'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|