|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\AssembleResponseEvent; |
|
6
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\OnCredentialExceptionThrownEvent; |
|
7
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Exception\CredentialException; |
|
8
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Ma27ApiKeyAuthenticationEvents; |
|
9
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Model\User\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
|
|
|
* @return JsonResponse |
|
27
|
|
|
* |
|
28
|
|
|
* @throws HttpException If the login fails. |
|
29
|
|
|
*/ |
|
30
|
6 |
|
public function requestApiKeyAction(Request $request) |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var \Ma27\ApiKeyAuthenticationBundle\Model\Login\AuthenticationHandlerInterface $authenticationHandler */ |
|
33
|
6 |
|
$authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler'); |
|
34
|
|
|
/** @var ClassMetadata $metadata */ |
|
35
|
6 |
|
$metadata = $this->get('ma27_api_key_authentication.class_metadata'); |
|
36
|
|
|
/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */ |
|
37
|
6 |
|
$dispatcher = $this->get('event_dispatcher'); |
|
38
|
|
|
|
|
39
|
6 |
|
$credentials = array(); |
|
40
|
6 |
|
if ($request->request->has('login')) { |
|
41
|
6 |
|
$credentials[$metadata->getPropertyName(ClassMetadata::LOGIN_PROPERTY)] = $request->request->get('login'); |
|
42
|
6 |
|
} |
|
43
|
|
|
|
|
44
|
6 |
|
if ($request->request->has('password')) { |
|
45
|
6 |
|
$credentials[$metadata->getPropertyName(ClassMetadata::PASSWORD_PROPERTY)] = $request->request->get('password'); |
|
46
|
6 |
|
} |
|
47
|
|
|
|
|
48
|
6 |
|
$exception = null; |
|
49
|
6 |
|
$user = null; |
|
50
|
|
|
try { |
|
51
|
6 |
|
$user = $authenticationHandler->authenticate($credentials); |
|
52
|
6 |
|
} catch (CredentialException $ex) { |
|
53
|
2 |
|
$dispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::CREDENTIAL_EXCEPTION_THROWN, new OnCredentialExceptionThrownEvent($user)); |
|
54
|
|
|
|
|
55
|
2 |
|
$exception = $ex; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @var AssembleResponseEvent $result */ |
|
59
|
6 |
|
$result = $dispatcher->dispatch( |
|
60
|
6 |
|
Ma27ApiKeyAuthenticationEvents::ASSEMBLE_RESPONSE, |
|
61
|
6 |
|
new AssembleResponseEvent($user, $exception) |
|
62
|
6 |
|
); |
|
63
|
|
|
|
|
64
|
6 |
|
if (!$response = $result->getResponse()) { |
|
65
|
|
|
throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, 'Cannot assemble the response!', $exception); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
6 |
|
return $response; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Removes an api key. |
|
73
|
|
|
* |
|
74
|
|
|
* @param Request $request |
|
75
|
|
|
* |
|
76
|
|
|
* @return JsonResponse |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function removeSessionAction(Request $request) |
|
79
|
|
|
{ |
|
80
|
|
|
/** @var \Ma27\ApiKeyAuthenticationBundle\Model\Login\AuthenticationHandlerInterface $authenticationHandler */ |
|
81
|
2 |
|
$authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler'); |
|
82
|
|
|
/** @var \Doctrine\Common\Persistence\ObjectManager $om */ |
|
83
|
2 |
|
$om = $this->get($this->container->getParameter('ma27_api_key_authentication.object_manager')); |
|
84
|
|
|
/** @var ClassMetadata $metadata */ |
|
85
|
2 |
|
$metadata = $this->get('ma27_api_key_authentication.class_metadata'); |
|
86
|
|
|
|
|
87
|
2 |
|
if (!$header = (string) $request->headers->get($this->container->getParameter('ma27_api_key_authentication.key_header'))) { |
|
88
|
1 |
|
return new JsonResponse(array('message' => 'Missing api key header!'), 400); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
$repository = $om->getRepository($this->container->getParameter('ma27_api_key_authentication.model_name')); |
|
92
|
1 |
|
$user = $repository->findOneBy(array($metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => (string) $header)); |
|
93
|
|
|
|
|
94
|
1 |
|
$authenticationHandler->removeSession($user); |
|
95
|
|
|
|
|
96
|
1 |
|
return new JsonResponse(array(), 204); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|