← All changes
|
app/Services/FileSystem/Drivers/S3/S3BucketList.php
+50
-10
1.3.20
→
1.6.6
View file →
| @@ -14,25 +14,37 @@ | ||
| 14 | 14 | private string $signature; |
| 15 | 15 | private $timeStamp; |
| 16 | 16 | private $date; |
| 17 | 17 | private string $requestUrl; |
| 18 | + private ?string $sessionToken = null; | |
| 18 | 19 | |
| 19 | 20 | |
| 20 | - public static function get(string $secret, string $accessKey, string $region) | |
| 21 | + public static function get(string $secret, string $accessKey, string $region, ?string $sessionToken = null) | |
| 21 | 22 | { |
| 22 | - return (new static($secret, $accessKey, $region))->getList(); | |
| 23 | + $validation = S3InputValidator::validateRegion($region); | |
| 24 | + if (is_wp_error($validation)) { | |
| 25 | + return $validation; | |
| 26 | + } | |
| 27 | + | |
| 28 | + return (new static($secret, $accessKey, $region, $sessionToken))->getList(); | |
| 23 | 29 | } |
| 24 | 30 | |
| 25 | - public function __construct(string $secret, string $accessKey, string $region) | |
| 31 | + public function __construct(string $secret, string $accessKey, string $region, ?string $sessionToken = null) | |
| 26 | 32 | { |
| 27 | 33 | $this->secretKey = $secret; |
| 28 | 34 | $this->accessKey = $accessKey; |
| 29 | 35 | $this->region = $region; |
| 36 | + $this->sessionToken = $sessionToken; | |
| 30 | 37 | |
| 31 | 38 | $this->httpMethod = "GET"; |
| 32 | 39 | $this->timeStamp = gmdate('Ymd\THis\Z'); |
| 33 | 40 | $this->date = substr($this->timeStamp, 0, 8); |
| 34 | - $this->requestUrl = "https://s3.amazonaws.com"; | |
| 41 | + if ($region !== 'us-east-1' && $region) { | |
| 42 | + $this->requestUrl = "https://s3.{$region}.amazonaws.com"; | |
| 43 | + } else { | |
| 44 | + $this->requestUrl = "https://s3.amazonaws.com"; | |
| 45 | + } | |
| 46 | + | |
| 35 | 47 | $this->generateSignature(); |
| 36 | 48 | } |
| 37 | 49 | |
| 38 | 50 | public function getList() |
| @@ -45,8 +57,11 @@ | ||
| 45 | 57 | 'method' => $this->httpMethod, |
| 46 | 58 | 'headers' => $this->getHeaders() |
| 47 | 59 | ]); |
| 48 | 60 | |
| 61 | + if (is_wp_error($response)) { | |
| 62 | + return $response; | |
| 63 | + } | |
| 49 | 64 | |
| 50 | 65 | $responseCode = wp_remote_retrieve_response_code($response); |
| 51 | 66 | |
| 52 | 67 | if ($responseCode == '200') { |
| @@ -100,21 +115,34 @@ | ||
| 100 | 115 | } |
| 101 | 116 | |
| 102 | 117 | private function createCanonicalUrl(): string |
| 103 | 118 | { |
| 104 | - return "$this->httpMethod\n" . | |
| 119 | + $payload = "$this->httpMethod\n" . | |
| 105 | 120 | "/\n\n" . |
| 106 | 121 | "host:{$this->getHost()}\n" . |
| 107 | 122 | "x-amz-content-sha256:{$this->getContentHash()}\n" . |
| 108 | - "x-amz-date:{$this->timeStamp}\n\n" . | |
| 109 | - "host;x-amz-content-sha256;x-amz-date\n" . | |
| 123 | + "x-amz-date:{$this->timeStamp}\n"; | |
| 124 | + | |
| 125 | + if ($this->sessionToken) { | |
| 126 | + $payload .= "x-amz-security-token:{$this->sessionToken}\n"; | |
| 127 | + } | |
| 128 | + | |
| 129 | + $payload .= "\n"; | |
| 130 | + | |
| 131 | + $signedHeaders = "host;x-amz-content-sha256;x-amz-date"; | |
| 132 | + if ($this->sessionToken) { | |
| 133 | + $signedHeaders .= ";x-amz-security-token"; | |
| 134 | + } | |
| 135 | + | |
| 136 | + $payload .= $signedHeaders . "\n" . | |
| 110 | 137 | "{$this->getContentHash()}"; |
| 111 | 138 | |
| 139 | + return $payload; | |
| 112 | 140 | } |
| 113 | 141 | |
| 114 | 142 | private function getHost(): string |
| 115 | 143 | { |
| 116 | - return "s3.amazonaws.com"; | |
| 144 | + return parse_url($this->requestUrl, PHP_URL_HOST) ?: "s3.amazonaws.com"; | |
| 117 | 145 | } |
| 118 | 146 | |
| 119 | 147 | private function createStringToSign(): string |
| 120 | 148 | { |
| @@ -136,11 +164,23 @@ | ||
| 136 | 164 | } |
| 137 | 165 | |
| 138 | 166 | public function getHeaders(): array |
| 139 | 167 | { |
| 140 | - return [ | |
| 168 | + $headers = [ | |
| 141 | 169 | "x-amz-content-sha256" => $this->getContentHash(), |
| 142 | 170 | 'x-amz-date' => $this->timeStamp, |
| 143 | - 'Authorization' => "AWS4-HMAC-SHA256 Credential={$this->accessKey}/{$this->date}/{$this->region}/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature={$this->getSignature()}" | |
| 144 | 171 | ]; |
| 172 | + | |
| 173 | + if ($this->sessionToken) { | |
| 174 | + $headers['x-amz-security-token'] = $this->sessionToken; | |
| 175 | + } | |
| 176 | + | |
| 177 | + $signedHeaders = "host;x-amz-content-sha256;x-amz-date"; | |
| 178 | + if ($this->sessionToken) { | |
| 179 | + $signedHeaders .= ";x-amz-security-token"; | |
| 180 | + } | |
| 181 | + | |
| 182 | + $headers['Authorization'] = "AWS4-HMAC-SHA256 Credential={$this->accessKey}/{$this->date}/{$this->region}/s3/aws4_request, SignedHeaders={$signedHeaders}, Signature={$this->getSignature()}"; | |
| 183 | + | |
| 184 | + return $headers; | |
| 145 | 185 | } |
| 146 | 186 | } |