1 | <?php |
||
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) |
|
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) |
|
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.