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