|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Security; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
11
|
|
|
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; |
|
12
|
|
|
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException; |
|
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\Csrf\CsrfToken; |
|
17
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
|
18
|
|
|
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator; |
|
19
|
|
|
use Symfony\Component\Security\Http\Util\TargetPathTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Authenticator |
|
23
|
|
|
* @package App\Security |
|
24
|
|
|
*/ |
|
25
|
|
|
class Authenticator extends AbstractFormLoginAuthenticator |
|
26
|
|
|
{ |
|
27
|
|
|
use TargetPathTrait; |
|
28
|
|
|
|
|
29
|
|
|
public const LOGIN_ROUTE = 'app_login'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var UrlGeneratorInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $urlGenerator; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var CsrfTokenManagerInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $csrfTokenManager; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Authenticator constructor. |
|
43
|
|
|
* @param UrlGeneratorInterface $urlGenerator |
|
44
|
|
|
* @param CsrfTokenManagerInterface $csrfTokenManager |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->urlGenerator = $urlGenerator; |
|
49
|
|
|
$this->csrfTokenManager = $csrfTokenManager; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Request $request |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function supports(Request $request) |
|
57
|
|
|
{ |
|
58
|
|
|
return self::LOGIN_ROUTE === $request->attributes->get('_route') |
|
59
|
|
|
&& $request->isMethod('POST'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param Request $request |
|
64
|
|
|
* @return array|mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getCredentials(Request $request) |
|
67
|
|
|
{ |
|
68
|
|
|
$credentials = [ |
|
69
|
|
|
'email' => $request->request->get('email'), |
|
70
|
|
|
'csrf_token' => $request->request->get('_csrf_token'), |
|
71
|
|
|
]; |
|
72
|
|
|
$request->getSession()->set( |
|
73
|
|
|
Security::LAST_USERNAME, |
|
74
|
|
|
$credentials['email'] |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
return $credentials; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param mixed $credentials |
|
82
|
|
|
* @param UserProviderInterface $userProvider |
|
83
|
|
|
* @return UserInterface|null |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider) |
|
86
|
|
|
{ |
|
87
|
|
|
$token = new CsrfToken('authenticate', $credentials['csrf_token']); |
|
88
|
|
|
if (!$this->csrfTokenManager->isTokenValid($token)) { |
|
89
|
|
|
throw new InvalidCsrfTokenException(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$user = $userProvider->loadUserByUsername($credentials['email']); |
|
93
|
|
|
|
|
94
|
|
|
if (!$user) { |
|
95
|
|
|
throw new CustomUserMessageAuthenticationException('Email could not be found.'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $user; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param mixed $credentials |
|
103
|
|
|
* @param UserInterface $user |
|
104
|
|
|
* |
|
105
|
|
|
* @return true |
|
106
|
|
|
*/ |
|
107
|
|
|
public function checkCredentials($credentials, UserInterface $user) |
|
108
|
|
|
{ |
|
109
|
|
|
return true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param Request $request |
|
114
|
|
|
* @param TokenInterface $token |
|
115
|
|
|
* @param string $providerKey |
|
116
|
|
|
* |
|
117
|
|
|
* @return RedirectResponse|Response|null |
|
118
|
|
|
* |
|
119
|
|
|
* @throws Exception |
|
120
|
|
|
*/ |
|
121
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey) |
|
122
|
|
|
{ |
|
123
|
|
|
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) { |
|
124
|
|
|
return new RedirectResponse($targetPath); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return new RedirectResponse($this->urlGenerator->generate('index')); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function getLoginUrl() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->urlGenerator->generate(self::LOGIN_ROUTE); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|