1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Security; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
9
|
|
|
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; |
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
11
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
12
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
13
|
|
|
use App\Security\UserProvider; |
14
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
15
|
|
|
|
16
|
|
|
class TokenAuthenticator extends AbstractGuardAuthenticator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var TranslatorInterface |
20
|
|
|
*/ |
21
|
|
|
private $translator; |
22
|
|
|
|
23
|
7 |
|
public function __construct(TranslatorInterface $translator) |
24
|
|
|
{ |
25
|
7 |
|
$this->translator = $translator; |
26
|
7 |
|
} |
27
|
|
|
|
28
|
7 |
|
public function supports(Request $request) |
29
|
|
|
{ |
30
|
7 |
|
return $request->query->has('api_token') && !empty($request->query->get('api_token')); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
public function getCredentials(Request $request) |
34
|
|
|
{ |
35
|
1 |
|
return $request->query->get('api_token'); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function getUser($apiToken, UserProviderInterface $userProvider) |
39
|
|
|
{ |
40
|
1 |
|
if (!$userProvider instanceof UserProvider) { |
41
|
|
|
throw new \InvalidArgumentException( |
42
|
|
|
$this->translator->trans('invalid_user_provider', [ |
43
|
|
|
'actual' => get_class($userProvider), |
44
|
|
|
], 'exceptions') |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var $userProvider UserProvider |
50
|
|
|
*/ |
51
|
1 |
|
return $userProvider->loadUserByToken($apiToken); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function checkCredentials($credentials, UserInterface $user) |
55
|
|
|
{ |
56
|
|
|
// check credentials - e.g. make sure the password is valid |
57
|
|
|
// no credential check is needed in this case |
58
|
|
|
|
59
|
|
|
// return true to cause authentication success |
60
|
1 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
64
|
|
|
{ |
65
|
|
|
$data = [ |
66
|
|
|
'error' => $this->translator->trans('api_token_authentication_failure', [], 'error'), |
67
|
|
|
'error_description' => $this->translator->trans('api_token_authentication_failure_description', [], 'error'), |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
return new JsonResponse($data, Response::HTTP_FORBIDDEN); |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
public function start(Request $request, AuthenticationException $authException = null) |
74
|
|
|
{ |
75
|
|
|
$data = [ |
76
|
3 |
|
'error' => $this->translator->trans('api_token_authentication_required', [], 'error'), |
77
|
3 |
|
'error_description' => $this->translator->trans('api_token_authentication_required_description', [], 'error'), |
78
|
|
|
]; |
79
|
|
|
|
80
|
3 |
|
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function supportsRememberMe() |
84
|
|
|
{ |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
89
|
|
|
{ |
90
|
1 |
|
return null; |
91
|
|
|
} |
92
|
|
|
} |