1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Service\Security; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Repository\UserRepository; |
6
|
|
|
use AppBundle\Entity\User; |
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
13
|
|
|
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; |
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
|
|
|
/** |
19
|
|
|
* @author Vehsamrak |
20
|
|
|
*/ |
21
|
|
|
class TokenAuthenticator extends AbstractGuardAuthenticator |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
const TOKEN_HEADER = 'AUTH-TOKEN'; |
25
|
|
|
|
26
|
|
|
/** @var UserRepository */ |
27
|
|
|
private $userRepository; |
28
|
|
|
|
29
|
44 |
|
public function __construct(UserRepository $userRepository) |
30
|
|
|
{ |
31
|
44 |
|
$this->userRepository = $userRepository; |
32
|
44 |
|
} |
33
|
|
|
|
34
|
|
|
/** {@inheritDoc} */ |
35
|
44 |
|
public function getCredentials(Request $request) |
36
|
|
|
{ |
37
|
44 |
|
$token = $request->headers->get(self::TOKEN_HEADER); |
38
|
|
|
|
39
|
44 |
|
if (!$token) { |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return [ |
44
|
44 |
|
'token' => $token, |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** {@inheritDoc} */ |
49
|
44 |
|
public function getUser($credentials, UserProviderInterface $userProvider) |
50
|
|
|
{ |
51
|
44 |
|
$token = $credentials['token']; |
52
|
|
|
|
53
|
44 |
|
$user = $this->userRepository->findUserByToken($token); |
54
|
|
|
|
55
|
44 |
|
if (!$user) { |
56
|
|
|
throw new CustomUserMessageAuthenticationException( |
57
|
|
|
sprintf('User with token "%s" was not found.', $token) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
44 |
|
return $user; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** {@inheritDoc} */ |
65
|
|
|
public function start(Request $request, AuthenticationException $authException = null) |
66
|
|
|
{ |
67
|
|
|
$data = [ |
68
|
|
|
'errors' => [ |
69
|
|
|
sprintf('Authentication required. Use header "%s" with user token.', self::TOKEN_HEADER), |
70
|
|
|
], |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** {@inheritDoc} */ |
77
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
78
|
|
|
{ |
79
|
|
|
$data = [ |
80
|
|
|
'errors' => [ |
81
|
|
|
strtr($exception->getMessageKey(), $exception->getMessageData()), |
82
|
|
|
], |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
return new JsonResponse($data, Response::HTTP_FORBIDDEN); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** {@inheritDoc} */ |
89
|
44 |
|
public function checkCredentials($credentials, UserInterface $user) |
90
|
|
|
{ |
91
|
44 |
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** {@inheritDoc} */ |
95
|
44 |
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
96
|
|
|
{ |
97
|
44 |
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** {@inheritDoc} */ |
101
|
|
|
public function supportsRememberMe() |
102
|
|
|
{ |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|