1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xervice\Api\Communication\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Xervice\Api\Business\Exception\AuthorizationException; |
8
|
|
|
use Xervice\Api\Business\Model\Authenticator\ApiAuthenticatorInterface; |
9
|
|
|
|
10
|
|
|
abstract class AbstractSecureApiController extends AbstractApiController |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
14
|
|
|
* @param string $method |
15
|
|
|
* @param mixed ...$params |
16
|
|
|
* |
17
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
18
|
|
|
* @throws \Xervice\Api\Business\Exception\ApiException |
19
|
|
|
* @throws \Xervice\Api\Business\Exception\AuthorizationException |
20
|
|
|
*/ |
21
|
3 |
|
public function apiAction(Request $request, string $method, ...$params): Response |
22
|
|
|
{ |
23
|
3 |
|
$this->authorize($request); |
24
|
|
|
|
25
|
|
|
return parent::apiAction($request, $method, ...$params); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
30
|
|
|
* |
31
|
|
|
* @throws \Xervice\Api\Business\Exception\AuthorizationException |
32
|
|
|
*/ |
33
|
3 |
|
protected function authorize(Request $request): void |
34
|
|
|
{ |
35
|
3 |
|
$this->getAuthenticator()->authenticate($request); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return \Xervice\Api\Business\Model\Authenticator\ApiAuthenticatorInterface |
40
|
|
|
* @throws \Xervice\Api\Business\Exception\AuthorizationException |
41
|
|
|
*/ |
42
|
3 |
|
private function getAuthenticator(): ApiAuthenticatorInterface |
43
|
|
|
{ |
44
|
3 |
|
$authenticator = $this->getService('apiAuthenticator'); |
45
|
|
|
|
46
|
3 |
|
if (!$authenticator instanceof ApiAuthenticatorInterface) { |
47
|
|
|
throw new AuthorizationException( |
48
|
|
|
sprintf( |
49
|
|
|
'No API authenticator service found. %s given', |
50
|
|
|
\get_class($authenticator) |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
return $authenticator; |
56
|
|
|
} |
57
|
|
|
} |