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