|
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 ClassMetadata $metadata */ |
|
33
|
12 |
|
$metadata = $this->get('ma27_api_key_authentication.class_metadata'); |
|
34
|
|
|
/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */ |
|
35
|
12 |
|
$dispatcher = $this->get('event_dispatcher'); |
|
36
|
|
|
|
|
37
|
12 |
|
$credentials = array(); |
|
38
|
12 |
|
if ($request->request->has('login')) { |
|
39
|
12 |
|
$credentials[$metadata->getPropertyName(ClassMetadata::LOGIN_PROPERTY)] = $request->request->get('login'); |
|
40
|
|
|
} |
|
41
|
12 |
|
if ($request->request->has('password')) { |
|
42
|
12 |
|
$credentials[$metadata->getPropertyName(ClassMetadata::PASSWORD_PROPERTY)] = $request->request->get('password'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
12 |
|
[$user, $exception] = $this->processAuthentication($credentials); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** @var OnAssembleResponseEvent $result */ |
|
48
|
12 |
|
$result = $dispatcher->dispatch( |
|
49
|
12 |
|
Ma27ApiKeyAuthenticationEvents::ASSEMBLE_RESPONSE, |
|
50
|
12 |
|
new OnAssembleResponseEvent($user, $exception) |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
12 |
|
if (!$response = $result->getResponse()) { |
|
54
|
|
|
throw new HttpException( |
|
55
|
|
|
Response::HTTP_INTERNAL_SERVER_ERROR, |
|
56
|
|
|
'Cannot assemble the response!', |
|
57
|
|
|
$exception |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
12 |
|
return $response; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Removes an api key. |
|
66
|
|
|
* |
|
67
|
|
|
* @param Request $request |
|
68
|
|
|
* |
|
69
|
|
|
* @return JsonResponse |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function removeSessionAction(Request $request) |
|
72
|
|
|
{ |
|
73
|
|
|
/** @var \Ma27\ApiKeyAuthenticationBundle\Service\Auth\AuthenticationHandlerInterface $authenticationHandler */ |
|
74
|
4 |
|
$authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler'); |
|
75
|
|
|
/** @var \Doctrine\Common\Persistence\ObjectManager $om */ |
|
76
|
4 |
|
$om = $this->get($this->container->getParameter('ma27_api_key_authentication.object_manager')); |
|
77
|
|
|
/** @var ClassMetadata $metadata */ |
|
78
|
4 |
|
$metadata = $this->get('ma27_api_key_authentication.class_metadata'); |
|
79
|
|
|
|
|
80
|
4 |
|
if (!$header = (string) $request->headers->get($this->container->getParameter('ma27_api_key_authentication.key_header'))) { |
|
81
|
2 |
|
return new JsonResponse(array('message' => 'Missing api key header!'), 400); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
$repository = $om->getRepository($this->container->getParameter('ma27_api_key_authentication.model_name')); |
|
85
|
2 |
|
$user = $repository->findOneBy(array($metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => (string) $header)); |
|
86
|
|
|
|
|
87
|
2 |
|
$authenticationHandler->removeSession($user); |
|
88
|
|
|
|
|
89
|
2 |
|
return new JsonResponse(array(), 204); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Internal utility to handle the authentication process based on the credentials. |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $credentials |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
12 |
|
private function processAuthentication(array $credentials) |
|
100
|
|
|
{ |
|
101
|
|
|
/** @var \Ma27\ApiKeyAuthenticationBundle\Service\Auth\AuthenticationHandlerInterface $authenticationHandler */ |
|
102
|
12 |
|
$authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler'); |
|
103
|
|
|
/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */ |
|
104
|
12 |
|
$dispatcher = $this->get('event_dispatcher'); |
|
105
|
|
|
|
|
106
|
|
|
try { |
|
107
|
12 |
|
$user = $authenticationHandler->authenticate($credentials); |
|
108
|
4 |
|
} catch (CredentialException $ex) { |
|
109
|
4 |
|
$userOrNull = $user ?? null; |
|
110
|
4 |
|
$dispatcher->dispatch( |
|
111
|
4 |
|
Ma27ApiKeyAuthenticationEvents::CREDENTIAL_EXCEPTION_THROWN, |
|
112
|
4 |
|
new OnCredentialExceptionThrownEvent($ex, $userOrNull) |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
4 |
|
return [$userOrNull, $ex]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
8 |
|
return [$user, null]; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
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.