config
1 month ago
Amazon.php
4 years ago
Helper.php
1 month ago
InstalledPlugin.php
3 years ago
InstalledTheme.php
4 years ago
Login.php
1 month ago
Product.php
4 years ago
ProductActions.php
4 years ago
ProductState.php
1 year ago
WpAjaxUpgraderSkin.php
4 years ago
Amazon.php
180 lines
| 1 | <?php |
| 2 | namespace Tenweb_Authorization { |
| 3 | |
| 4 | class Amazon |
| 5 | { |
| 6 | |
| 7 | private $key; |
| 8 | private $secret; |
| 9 | private $token; |
| 10 | private $file; |
| 11 | |
| 12 | private $service = 's3'; |
| 13 | private $region = 'us-west-2'; |
| 14 | private $bucket = TENWEB_S3_BUCKET; |
| 15 | private $payload = "";//UNSIGNED-PAYLOAD |
| 16 | |
| 17 | private $signedHeaders = ""; |
| 18 | private $canonicalHeaders = ""; |
| 19 | |
| 20 | private $authHeader = ""; |
| 21 | private $headers = array(); |
| 22 | |
| 23 | private $scope; |
| 24 | private $longDate; |
| 25 | private $shortDate; |
| 26 | private $host; |
| 27 | private $method = 'GET'; |
| 28 | private $queryString = ''; |
| 29 | private $canonicalQueryString = ''; |
| 30 | |
| 31 | private $signature = ""; |
| 32 | |
| 33 | public function __construct($key, $secret, $token, $file, $bucket = TENWEB_S3_BUCKET, $region = 'us-west-2') |
| 34 | { |
| 35 | $this->key = $key; |
| 36 | $this->secret = $secret; |
| 37 | $this->token = $token; |
| 38 | $this->file = ltrim($file, '/'); |
| 39 | $this->bucket = $bucket; |
| 40 | $this->region = $region; |
| 41 | |
| 42 | $this->host = $this->bucket . '.' . $this->service . '.' . $this->region . '.amazonaws.com'; |
| 43 | |
| 44 | } |
| 45 | |
| 46 | public function setMethod($method) |
| 47 | { |
| 48 | $this->method = $method; |
| 49 | } |
| 50 | |
| 51 | public function setPayload($payload) |
| 52 | { |
| 53 | $this->payload = $payload; |
| 54 | } |
| 55 | |
| 56 | public function setExtraHeaders($headers) |
| 57 | { |
| 58 | $this->headers = $headers; |
| 59 | } |
| 60 | |
| 61 | public function setQueryString($query_string_array) |
| 62 | { |
| 63 | $this->queryString = ''; |
| 64 | $this->canonicalQueryString = ''; |
| 65 | ksort($query_string_array); |
| 66 | foreach ($query_string_array as $key => $value) { |
| 67 | $this->canonicalQueryString .= rawurlencode($key) . '=' . rawurlencode($value) . '&'; |
| 68 | $this->queryString .= trim($key) . '=' . trim($value) . '&'; |
| 69 | } |
| 70 | $this->queryString = rtrim($this->queryString, '&'); |
| 71 | $this->canonicalQueryString = rtrim($this->canonicalQueryString, '&'); |
| 72 | } |
| 73 | |
| 74 | public function getRequestData() |
| 75 | { |
| 76 | $this->longDate = gmdate('Ymd\THis\Z'); |
| 77 | $this->shortDate = substr($this->longDate, 0, 8); |
| 78 | |
| 79 | $this->createScope(); |
| 80 | $this->setHeaders(); |
| 81 | |
| 82 | $this->calculateSignature(); |
| 83 | |
| 84 | $this->setAuthorizationHeader(); |
| 85 | |
| 86 | $headers = $this->headers; |
| 87 | $headers['Authorization'] = $this->authHeader; |
| 88 | |
| 89 | $url = 'https://' . $this->host . '/' . $this->file; |
| 90 | if ($this->queryString) { |
| 91 | $url .= '?' . $this->queryString; |
| 92 | } |
| 93 | $result = array( |
| 94 | 'headers' => $headers, |
| 95 | 'url' => $url |
| 96 | ); |
| 97 | |
| 98 | return $result; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | private function setAuthorizationHeader() |
| 103 | { |
| 104 | $credentials = $this->key . '/' . $this->scope; |
| 105 | $this->authHeader = "AWS4-HMAC-SHA256 " |
| 106 | . "Credential=" . $credentials . ", " |
| 107 | . "SignedHeaders=" . $this->signedHeaders . ", " |
| 108 | . "Signature=" . $this->signature; |
| 109 | } |
| 110 | |
| 111 | private function calculateSignature() |
| 112 | { |
| 113 | $toSign = $this->getStringToSign(); |
| 114 | |
| 115 | $dateKey = hash_hmac('sha256', $this->shortDate, "AWS4" . $this->secret, true); |
| 116 | $regionKey = hash_hmac('sha256', $this->region, $dateKey, true); |
| 117 | $serviceKey = hash_hmac('sha256', $this->service, $regionKey, true); |
| 118 | $signingKey = hash_hmac('sha256', 'aws4_request', $serviceKey, true); |
| 119 | |
| 120 | $signature = hash_hmac('sha256', $toSign, $signingKey); |
| 121 | $this->signature = $signature; |
| 122 | } |
| 123 | |
| 124 | private function getStringToSign() |
| 125 | { |
| 126 | $canonical = $this->getCanonicalRequest(); |
| 127 | |
| 128 | $strToSign = "AWS4-HMAC-SHA256" . "\n" |
| 129 | . $this->longDate . "\n" |
| 130 | . $this->scope . "\n" |
| 131 | . hash('sha256', $canonical); |
| 132 | |
| 133 | return $strToSign; |
| 134 | } |
| 135 | |
| 136 | private function getCanonicalRequest() |
| 137 | { |
| 138 | $canonicalURI = '/' . $this->file; |
| 139 | $canonicalQueryString = $this->canonicalQueryString; |
| 140 | $canonicalHeaders = $this->canonicalHeaders; |
| 141 | $signedHeaders = $this->signedHeaders; |
| 142 | $payload = hash('sha256', $this->payload); |
| 143 | |
| 144 | $canonical = $this->method . "\n" . $canonicalURI . "\n" . $canonicalQueryString . "\n" . $canonicalHeaders . "\n" . $signedHeaders . "\n" . $payload; |
| 145 | |
| 146 | return $canonical; |
| 147 | } |
| 148 | |
| 149 | private function setHeaders() |
| 150 | { |
| 151 | $this->headers += array( |
| 152 | 'host' => $this->host, |
| 153 | 'x-amz-content-sha256' => hash('sha256', $this->payload), |
| 154 | 'x-amz-date' => $this->longDate, |
| 155 | 'x-amz-security-token' => $this->token, |
| 156 | //'x-amz-server-side-encryption' => 'AES256' |
| 157 | ); |
| 158 | |
| 159 | ksort($this->headers); |
| 160 | |
| 161 | $this->canonicalHeaders = ""; |
| 162 | $this->signedHeaders = ""; |
| 163 | |
| 164 | foreach ($this->headers as $header => $value) { |
| 165 | $this->canonicalHeaders .= strtolower($header) . ":" . trim($value) . "\n"; |
| 166 | $this->signedHeaders .= strtolower($header) . ';'; |
| 167 | } |
| 168 | |
| 169 | $this->signedHeaders = rtrim($this->signedHeaders, ';'); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | private function createScope() |
| 174 | { |
| 175 | $this->scope = $this->shortDate . "/" . $this->region . "/" . $this->service . "/aws4_request"; |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | } |
| 180 |