1 | <?php |
||
10 | class BearerTokenAuthorization implements ApiAuthorizationInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var BearerTokenRepositoryInterface |
||
14 | */ |
||
15 | private $tokenRepository; |
||
16 | |||
17 | /** |
||
18 | * @var string|null |
||
19 | */ |
||
20 | private $errorMessage = null; |
||
21 | |||
22 | /** |
||
23 | * @var IpDetectorInterface |
||
24 | */ |
||
25 | private $ipDetector; |
||
26 | |||
27 | /** |
||
28 | * BearerTokenAuthorization constructor. |
||
29 | * |
||
30 | * @param BearerTokenRepositoryInterface $tokenRepository |
||
31 | * @param IpDetectorInterface $ipDetector |
||
32 | */ |
||
33 | 33 | public function __construct(BearerTokenRepositoryInterface $tokenRepository, IpDetectorInterface $ipDetector) |
|
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 33 | public function authorized(): bool |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 18 | public function getErrorMessage(): ?string |
|
70 | |||
71 | /** |
||
72 | * Check if actual IP from detector satisfies @ipRestristions |
||
73 | * $ipRestrictions should contains multiple formats: |
||
74 | * '*' - accessible from anywhare |
||
75 | * '127.0.0.1' - accessible from single IP |
||
76 | * '127.0.0.1,127.0.02' - accessible from multiple IP, separator could be new line or space |
||
77 | * '127.0.0.1/32' - accessible from ip range |
||
78 | * null - disabled access |
||
79 | * |
||
80 | * @return boolean |
||
81 | */ |
||
82 | 18 | private function isValidIp(?string $ipRestrictions): bool |
|
105 | |||
106 | /** |
||
107 | * Check if IP is in $range |
||
108 | * |
||
109 | * @param string $ip this ip will be verified |
||
110 | * @param string $range is in IP/CIDR format eg 127.0.0.1/24 |
||
111 | * @return boolean |
||
112 | */ |
||
113 | 3 | private function ipInRange(string $ip, string $range): bool |
|
122 | |||
123 | /** |
||
124 | * Read HTTP reader with authorization token |
||
125 | * If everything is ok, it return token. In other situations returns false and set errorMessage. |
||
126 | * |
||
127 | * @return string|null |
||
128 | */ |
||
129 | 33 | private function readAuthorizationToken(): ?string |
|
146 | } |
||
147 |