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 \Ma27\ApiKeyAuthenticationBundle\Service\Auth\AuthenticationHandlerInterface $authenticationHandler */ |
||
33 | 12 | $authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler'); |
|
34 | /** @var ClassMetadata $metadata */ |
||
35 | 12 | $metadata = $this->get('ma27_api_key_authentication.class_metadata'); |
|
36 | /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */ |
||
37 | 12 | $dispatcher = $this->get('event_dispatcher'); |
|
38 | |||
39 | 12 | $credentials = array(); |
|
40 | 12 | if ($request->request->has('login')) { |
|
41 | 12 | $credentials[$metadata->getPropertyName(ClassMetadata::LOGIN_PROPERTY)] = $request->request->get('login'); |
|
42 | } |
||
43 | |||
44 | 12 | if ($request->request->has('password')) { |
|
45 | 12 | $credentials[$metadata->getPropertyName(ClassMetadata::PASSWORD_PROPERTY)] = $request->request->get('password'); |
|
46 | } |
||
47 | |||
48 | 12 | $exception = null; |
|
49 | 12 | $user = null; |
|
50 | try { |
||
51 | 12 | $user = $authenticationHandler->authenticate($credentials); |
|
52 | 4 | } catch (CredentialException $ex) { |
|
53 | 4 | $dispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::CREDENTIAL_EXCEPTION_THROWN, new OnCredentialExceptionThrownEvent($ex, $user)); |
|
54 | |||
55 | 4 | $exception = $ex; |
|
56 | } |
||
57 | |||
58 | /** @var OnAssembleResponseEvent $result */ |
||
59 | 12 | $result = $dispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::ASSEMBLE_RESPONSE, new OnAssembleResponseEvent($user, $exception)); |
|
60 | 12 | if (!$response = $result->getResponse()) { |
|
61 | throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, 'Cannot assemble the response!', $exception); |
||
62 | } |
||
63 | |||
64 | 12 | return $response; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Removes an api key. |
||
69 | * |
||
70 | * @param Request $request |
||
71 | * |
||
72 | * @return JsonResponse |
||
73 | */ |
||
74 | 4 | public function removeSessionAction(Request $request) |
|
94 | } |
||
95 |