|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\OnAssembleResponseEvent; |
|
6
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\OnCredentialExceptionThrownEvent; |
|
7
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Exception\CredentialException; |
|
8
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Ma27ApiKeyAuthenticationEvents; |
|
9
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Service\Mapping\ClassMetadata; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Controller which is responsible for the authentication routes. |
|
18
|
|
|
*/ |
|
19
|
|
|
class ApiKeyController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Requests an api key. |
|
23
|
|
|
* |
|
24
|
|
|
* @param Request $request |
|
25
|
|
|
* |
|
26
|
|
|
* @throws HttpException If the login fails. |
|
27
|
|
|
* |
|
28
|
|
|
* @return JsonResponse |
|
29
|
|
|
*/ |
|
30
|
12 |
|
public function requestApiKeyAction(Request $request) |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */ |
|
33
|
12 |
|
$dispatcher = $this->get('event_dispatcher'); |
|
34
|
|
|
|
|
35
|
12 |
|
$credentials = []; |
|
36
|
12 |
|
if ($request->request->has('login')) { |
|
37
|
12 |
|
$credentials[$this->getPropertyName(ClassMetadata::LOGIN_PROPERTY)] = $request->request->get('login'); |
|
38
|
|
|
} |
|
39
|
12 |
|
if ($request->request->has('password')) { |
|
40
|
12 |
|
$credentials[$this->getPropertyName(ClassMetadata::PASSWORD_PROPERTY)] = $request->request->get('password'); |
|
41
|
|
|
} |
|
42
|
12 |
|
[$user, $exception] = $this->processAuthentication($credentials); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** @var OnAssembleResponseEvent $result */ |
|
45
|
12 |
|
$result = $dispatcher->dispatch( |
|
46
|
12 |
|
Ma27ApiKeyAuthenticationEvents::ASSEMBLE_RESPONSE, |
|
47
|
12 |
|
new OnAssembleResponseEvent($user, $exception) |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
12 |
|
if (!$response = $result->getResponse()) { |
|
51
|
|
|
throw new HttpException( |
|
52
|
|
|
Response::HTTP_INTERNAL_SERVER_ERROR, |
|
53
|
|
|
'Cannot assemble the response!', |
|
54
|
|
|
$exception |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
12 |
|
return $response; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Removes an api key. |
|
63
|
|
|
* |
|
64
|
|
|
* @param Request $request |
|
65
|
|
|
* |
|
66
|
|
|
* @return JsonResponse |
|
67
|
|
|
*/ |
|
68
|
4 |
|
public function removeSessionAction(Request $request) |
|
69
|
|
|
{ |
|
70
|
|
|
/** @var \Doctrine\Common\Persistence\ObjectManager $om */ |
|
71
|
4 |
|
$om = $this->get($this->container->getParameter('ma27_api_key_authentication.object_manager')); |
|
72
|
|
|
|
|
73
|
4 |
|
if (!$header = (string) $request->headers->get($this->container->getParameter('ma27_api_key_authentication.key_header'))) { |
|
74
|
2 |
|
return new JsonResponse(['message' => 'Missing api key header!'], 400); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$user = $om |
|
78
|
2 |
|
->getRepository($this->container->getParameter('ma27_api_key_authentication.model_name')) |
|
79
|
2 |
|
->findOneBy([ |
|
80
|
2 |
|
$this->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => $header, |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
2 |
|
$this->get('ma27_api_key_authentication.auth_handler')->removeSession($user); |
|
84
|
|
|
|
|
85
|
2 |
|
return new JsonResponse([], 204); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Internal utility to handle the authentication process based on the credentials. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $credentials |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
12 |
|
private function processAuthentication(array $credentials) |
|
96
|
|
|
{ |
|
97
|
|
|
/** @var \Ma27\ApiKeyAuthenticationBundle\Service\Auth\AuthenticationHandlerInterface $authenticationHandler */ |
|
98
|
12 |
|
$authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler'); |
|
99
|
|
|
/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */ |
|
100
|
12 |
|
$dispatcher = $this->get('event_dispatcher'); |
|
101
|
|
|
|
|
102
|
|
|
try { |
|
103
|
12 |
|
$user = $authenticationHandler->authenticate($credentials); |
|
104
|
4 |
|
} catch (CredentialException $ex) { |
|
105
|
4 |
|
$userOrNull = $user ?? null; |
|
106
|
4 |
|
$dispatcher->dispatch( |
|
107
|
4 |
|
Ma27ApiKeyAuthenticationEvents::CREDENTIAL_EXCEPTION_THROWN, |
|
108
|
4 |
|
new OnCredentialExceptionThrownEvent($ex, $userOrNull) |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
4 |
|
return [$userOrNull, $ex]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
8 |
|
return [$user, null]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns the actual property name by the given metadata alias. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $internalMetadataAlias |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
12 |
|
private function getPropertyName($internalMetadataAlias) |
|
125
|
|
|
{ |
|
126
|
12 |
|
return $this->get('ma27_api_key_authentication.class_metadata')->getPropertyName($internalMetadataAlias); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.