1 | <?php |
||
8 | class BearerTokenAuthorization implements ApiAuthorizationInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var BearerTokenRepositoryInterface |
||
12 | */ |
||
13 | private $tokenRepository; |
||
14 | |||
15 | /** |
||
16 | * @var string|boolean |
||
17 | */ |
||
18 | private $errorMessage = false; |
||
19 | |||
20 | /** |
||
21 | * @var IpDetectorInterface |
||
22 | */ |
||
23 | private $ipDetector; |
||
24 | |||
25 | /** |
||
26 | * BearerTokenAuthorization constructor. |
||
27 | * |
||
28 | * @param BearerTokenRepositoryInterface $tokenRepository |
||
29 | * @param IpDetectorInterface $ipDetector |
||
30 | */ |
||
31 | 27 | public function __construct(BearerTokenRepositoryInterface $tokenRepository, IpDetectorInterface $ipDetector) |
|
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 27 | public function authorized() |
|
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 15 | public function getErrorMessage() |
|
68 | |||
69 | /** |
||
70 | * Check if actual IP from detector satisfies @ipRestristions |
||
71 | * $ipRestrictions should contains multiple formats: |
||
72 | * '*' - accessible from anywhare |
||
73 | * '127.0.0.1' - accessible from single IP |
||
74 | * '127.0.0.1,127.0.02' - accessible from multiple IP, separator could be new line or space |
||
75 | * '127.0.0.1/32' - accessible from ip range |
||
76 | * |
||
77 | * @return boolean |
||
78 | */ |
||
79 | 15 | private function isValidIp($ipRestrictions) |
|
99 | |||
100 | /** |
||
101 | * Check if IP is in $range |
||
102 | * |
||
103 | * @param string $ip this ip will be verified |
||
104 | * @param string $range is in IP/CIDR format eg 127.0.0.1/24 |
||
105 | * @return boolean |
||
106 | */ |
||
107 | 3 | private function ipInRange($ip, $range) |
|
116 | |||
117 | |||
118 | /** |
||
119 | * Read HTTP reader with authorization token |
||
120 | * If everything is ok, it return token. In other situations returns false and set errorMessage. |
||
121 | * |
||
122 | * @return string|boolean |
||
123 | */ |
||
124 | 27 | private function readAuthorizationToken() |
|
141 | } |
||
142 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: