| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\FileSystem\Drivers\S3; |
| 4 |
|
| 5 |
class S3ConnectionVerify |
| 6 |
{ |
| 7 |
private $date; |
| 8 |
private $timeStamp; |
| 9 |
private string $accessKey; |
| 10 |
private string $bucket; |
| 11 |
private string $hashAlgorithm = 'sha256'; |
| 12 |
private string $httpMethod; |
| 13 |
private string $region; |
| 14 |
private string $secretKey; |
| 15 |
private string $signature; |
| 16 |
private string $requestUrl; |
| 17 |
|
| 18 |
public static function verify(string $secret, string $accessKey) |
| 19 |
{ |
| 20 |
$self = new static($secret, $accessKey); |
| 21 |
return $self->testConnection(); |
| 22 |
} |
| 23 |
|
| 24 |
public function __construct(string $secret, string $accessKey) |
| 25 |
{ |
| 26 |
$this->secretKey = $secret; |
| 27 |
$this->accessKey = $accessKey; |
| 28 |
$this->region = 'us-east-1'; |
| 29 |
$this->bucket = ''; |
| 30 |
|
| 31 |
$this->httpMethod = "GET"; |
| 32 |
$this->timeStamp = gmdate('Ymd\THis\Z'); |
| 33 |
$this->date = substr($this->timeStamp, 0, 8); |
| 34 |
$this->requestUrl = "https://s3.amazonaws.com/?list-type=2&encoding-type=url&max-keys=1"; |
| 35 |
|
| 36 |
$this->signature = $this->generateSignature(); |
| 37 |
} |
| 38 |
|
| 39 |
public function testConnection() |
| 40 |
{ |
| 41 |
add_filter('http_request_timeout', function () { |
| 42 |
return 30; |
| 43 |
}); |
| 44 |
|
| 45 |
$response = wp_remote_request($this->requestUrl, [ |
| 46 |
'method' => $this->httpMethod, |
| 47 |
'headers' => $this->getHeaders() |
| 48 |
]); |
| 49 |
|
| 50 |
$responseCode = wp_remote_retrieve_response_code($response); |
| 51 |
|
| 52 |
if ($responseCode == '200') { |
| 53 |
return [ |
| 54 |
'message' => __('Successfully verified S3 connection!', 'fluent-cart'), |
| 55 |
'code' => $responseCode |
| 56 |
]; |
| 57 |
} else { |
| 58 |
$error_message = __('Invalid S3 credentials', 'fluent-cart'); |
| 59 |
|
| 60 |
$responseBody = wp_remote_retrieve_body($response); |
| 61 |
if (!empty($responseBody)) { |
| 62 |
$xml = simplexml_load_string($responseBody); |
| 63 |
if ($xml && isset($xml->Code)) { |
| 64 |
$code = (string)$xml->Code; |
| 65 |
$message = (string)$xml->Message; |
| 66 |
$error_message = $message ?: $error_message; |
| 67 |
|
| 68 |
if(str_contains($error_message,'User:')) { |
| 69 |
$error_message = __('Your IAM user does not have permission to use S3 buckets', 'fluent-cart'); |
| 70 |
}else{ |
| 71 |
$error_message = __('Invalid S3 credentials', 'fluent-cart'); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
return new \WP_Error($responseCode, $error_message); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
private function generateSignature() |
| 83 |
{ |
| 84 |
return hash_hmac( |
| 85 |
$this->hashAlgorithm, |
| 86 |
$this->createStringToSign(), |
| 87 |
$this->getSigningKey() |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
private function createStringToSign(): string |
| 92 |
{ |
| 93 |
$hash = hash($this->hashAlgorithm, $this->createCanonicalUrl()); |
| 94 |
return "AWS4-HMAC-SHA256\n{$this->timeStamp}\n{$this->getScope()}\n{$hash}"; |
| 95 |
} |
| 96 |
|
| 97 |
private function createCanonicalUrl(): string |
| 98 |
{ |
| 99 |
$canonicalQuery = "encoding-type=url&list-type=2&max-keys=1"; |
| 100 |
|
| 101 |
return "$this->httpMethod\n" . |
| 102 |
"/\n" . |
| 103 |
"{$canonicalQuery}\n" . |
| 104 |
"host:{$this->getHost()}\n" . |
| 105 |
"x-amz-content-sha256:{$this->getContentHash()}\n" . |
| 106 |
"x-amz-date:{$this->timeStamp}\n\n" . |
| 107 |
"host;x-amz-content-sha256;x-amz-date\n" . |
| 108 |
"{$this->getContentHash()}"; |
| 109 |
} |
| 110 |
|
| 111 |
private function getHost(): string |
| 112 |
{ |
| 113 |
return "s3.amazonaws.com"; |
| 114 |
} |
| 115 |
|
| 116 |
private function getContentHash(): string |
| 117 |
{ |
| 118 |
return hash($this->hashAlgorithm, ""); |
| 119 |
} |
| 120 |
|
| 121 |
private function getScope(): string |
| 122 |
{ |
| 123 |
return "{$this->date}/{$this->region}/s3/aws4_request"; |
| 124 |
} |
| 125 |
|
| 126 |
private function getSigningKey() |
| 127 |
{ |
| 128 |
$dateKey = hash_hmac($this->hashAlgorithm, $this->date, "AWS4{$this->secretKey}", true); |
| 129 |
$regionKey = hash_hmac($this->hashAlgorithm, $this->region, $dateKey, true); |
| 130 |
$serviceKey = hash_hmac($this->hashAlgorithm, 's3', $regionKey, true); |
| 131 |
return hash_hmac($this->hashAlgorithm, 'aws4_request', $serviceKey, true); |
| 132 |
} |
| 133 |
|
| 134 |
private function getHeaders(): array |
| 135 |
{ |
| 136 |
return [ |
| 137 |
"x-amz-content-sha256" => $this->getContentHash(), |
| 138 |
'x-amz-date' => $this->timeStamp, |
| 139 |
'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->signature}" |
| 140 |
]; |
| 141 |
} |
| 142 |
} |