|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebServCo\Api; |
|
3
|
|
|
|
|
4
|
|
|
use \WebServCo\Framework\Exceptions\NotImplementedException; |
|
5
|
|
|
|
|
6
|
|
|
abstract class AbstractSecurity |
|
7
|
|
|
{ |
|
8
|
|
|
protected $allowedMethods; |
|
9
|
|
|
protected $clientContentTypes; |
|
10
|
|
|
protected $supportedContentTypes; |
|
11
|
|
|
protected $request; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(\WebServCo\Framework\Interfaces\RequestInterface $request) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->allowedMethods = []; |
|
16
|
|
|
$this->supportedContentTypes = []; |
|
17
|
|
|
$this->request = $request; |
|
18
|
|
|
|
|
19
|
|
|
$this->clientContentTypes = $this->request->getAcceptContentTypes(); |
|
20
|
|
|
if (array_key_exists(0, $this->clientContentTypes)) { |
|
21
|
|
|
unset($this->clientContentTypes[0]); // $q == 0 means, that mime-type isn’t supported! |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function verify() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->verifySsl(); |
|
28
|
|
|
$this->verifyMethod(); |
|
29
|
|
|
$this->verifyContentType(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getClientContentTypes() |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->clientContentTypes; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function setAllowedMethods(array $allowedMethods) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->allowedMethods = $allowedMethods; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function setSupportedContentTypes(array $supportedContentTypes) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->supportedContentTypes = $supportedContentTypes; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
abstract public function verifyAuthorization(); |
|
48
|
|
|
|
|
49
|
|
|
protected function verifyContentType() |
|
50
|
|
|
{ |
|
51
|
|
|
if (empty($this->supportedContentTypes)) { |
|
52
|
|
|
throw new NotImplementedException('Content type support not implemented'); |
|
53
|
|
|
} |
|
54
|
|
|
$intersection = array_intersect($this->clientContentTypes, $this->supportedContentTypes); |
|
55
|
|
|
if (empty($intersection)) { |
|
56
|
|
|
throw new \WebServCo\Framework\Exceptions\UnsupportedMediaTypeException('Unsupported content type'); |
|
57
|
|
|
} |
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function verifyMethod() |
|
62
|
|
|
{ |
|
63
|
|
|
if (empty($this->allowedMethods)) { |
|
64
|
|
|
throw new NotImplementedException('Method not implemented'); |
|
65
|
|
|
} |
|
66
|
|
|
if (!in_array($this->request->getMethod(), $this->allowedMethods)) { |
|
67
|
|
|
throw new \WebServCo\Framework\Exceptions\MethodNotAllowedException('Unsupported method'); |
|
68
|
|
|
} |
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function verifySsl() |
|
73
|
|
|
{ |
|
74
|
|
|
$schema = $this->request->getSchema(); |
|
75
|
|
|
if ('https' != $schema) { |
|
76
|
|
|
throw new \WebServCo\Framework\Exceptions\SslRequiredException('SSL required'); |
|
77
|
|
|
} |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|