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 \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) |
75
|
|
|
{ |
76
|
|
|
/** @var \Ma27\ApiKeyAuthenticationBundle\Service\Auth\AuthenticationHandlerInterface $authenticationHandler */ |
77
|
4 |
|
$authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler'); |
78
|
|
|
/** @var \Doctrine\Common\Persistence\ObjectManager $om */ |
79
|
4 |
|
$om = $this->get($this->container->getParameter('ma27_api_key_authentication.object_manager')); |
80
|
|
|
/** @var ClassMetadata $metadata */ |
81
|
4 |
|
$metadata = $this->get('ma27_api_key_authentication.class_metadata'); |
82
|
|
|
|
83
|
4 |
|
if (!$header = (string) $request->headers->get($this->container->getParameter('ma27_api_key_authentication.key_header'))) { |
84
|
2 |
|
return new JsonResponse(array('message' => 'Missing api key header!'), 400); |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
$repository = $om->getRepository($this->container->getParameter('ma27_api_key_authentication.model_name')); |
88
|
2 |
|
$user = $repository->findOneBy(array($metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => (string) $header)); |
89
|
|
|
|
90
|
2 |
|
$authenticationHandler->removeSession($user); |
91
|
|
|
|
92
|
2 |
|
return new JsonResponse(array(), 204); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|