1 | <?php |
||
11 | abstract class TokenAuthorization implements ApiAuthorizationInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var TokenRepositoryInterface |
||
15 | */ |
||
16 | protected $tokenRepository; |
||
17 | |||
18 | /** |
||
19 | * @var string|null |
||
20 | */ |
||
21 | protected $errorMessage = null; |
||
22 | |||
23 | /** |
||
24 | * @var IpDetectorInterface |
||
25 | */ |
||
26 | protected $ipDetector; |
||
27 | |||
28 | /** |
||
29 | * @param TokenRepositoryInterface $tokenRepository |
||
30 | * @param IpDetectorInterface $ipDetector |
||
31 | */ |
||
32 | public function __construct(TokenRepositoryInterface $tokenRepository, IpDetectorInterface $ipDetector) |
||
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | */ |
||
41 | public function authorized(): bool |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function getErrorMessage(): ?string |
||
69 | |||
70 | /** |
||
71 | * Check if actual IP from detector satisfies @ipRestristions |
||
72 | * $ipRestrictions should contains multiple formats: |
||
73 | * '*' - accessible from anywhare |
||
74 | * '127.0.0.1' - accessible from single IP |
||
75 | * '127.0.0.1,127.0.02' - accessible from multiple IP, separator could be new line or space |
||
76 | * '127.0.0.1/32' - accessible from ip range |
||
77 | * null - disabled access |
||
78 | * |
||
79 | * @return boolean |
||
80 | */ |
||
81 | private function isValidIp(?string $ipRestrictions): bool |
||
104 | |||
105 | /** |
||
106 | * Check if IP is in $range |
||
107 | * |
||
108 | * @param string $ip this ip will be verified |
||
109 | * @param string $range is in IP/CIDR format eg 127.0.0.1/24 |
||
110 | * @return boolean |
||
111 | */ |
||
112 | private function ipInRange(string $ip, string $range): bool |
||
121 | |||
122 | abstract protected function readAuthorizationToken(): ?string; |
||
123 | } |
||
124 |