| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\S3; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Credentials\CredentialsInterface; |
| 6 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\Uri; |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\Signature\SignatureTrait; |
| 8 |
use Dudlewebs\WPMCS\s3\Aws\Signature\SignatureV4 as SignatureV4; |
| 9 |
use Dudlewebs\WPMCS\s3\Aws\Api\TimestampShape as TimestampShape; |
| 10 |
/** |
| 11 |
* Encapsulates the logic for getting the data for an S3 object POST upload form |
| 12 |
* |
| 13 |
* @link http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html |
| 14 |
* @link http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html |
| 15 |
*/ |
| 16 |
class PostObjectV4 |
| 17 |
{ |
| 18 |
use SignatureTrait; |
| 19 |
private $client; |
| 20 |
private $bucket; |
| 21 |
private $formAttributes; |
| 22 |
private $formInputs; |
| 23 |
/** |
| 24 |
* Constructs the PostObject. |
| 25 |
* |
| 26 |
* The options array accepts the following keys: |
| 27 |
* @link http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html |
| 28 |
* |
| 29 |
* @param S3ClientInterface $client Client used with the POST object |
| 30 |
* @param string $bucket Bucket to use |
| 31 |
* @param array $formInputs Associative array of form input |
| 32 |
* fields. |
| 33 |
* @param array $options Policy condition options |
| 34 |
* @param mixed $expiration Upload expiration time value. By |
| 35 |
* default: 1 hour valid period. |
| 36 |
*/ |
| 37 |
public function __construct(S3ClientInterface $client, $bucket, array $formInputs, array $options = [], $expiration = '+1 hours') |
| 38 |
{ |
| 39 |
$this->client = $client; |
| 40 |
$this->bucket = $bucket; |
| 41 |
// setup form attributes |
| 42 |
$this->formAttributes = ['action' => $this->generateUri(), 'method' => 'POST', 'enctype' => 'multipart/form-data']; |
| 43 |
$credentials = $this->client->getCredentials()->wait(); |
| 44 |
if ($securityToken = $credentials->getSecurityToken()) { |
| 45 |
$options[] = ['x-amz-security-token' => $securityToken]; |
| 46 |
$formInputs['X-Amz-Security-Token'] = $securityToken; |
| 47 |
} |
| 48 |
// setup basic policy |
| 49 |
$policy = ['expiration' => TimestampShape::format($expiration, 'iso8601'), 'conditions' => $options]; |
| 50 |
// setup basic formInputs |
| 51 |
$this->formInputs = $formInputs + ['key' => '${filename}']; |
| 52 |
// finalize policy and signature |
| 53 |
$this->formInputs += $this->getPolicyAndSignature($credentials, $policy); |
| 54 |
} |
| 55 |
/** |
| 56 |
* Gets the S3 client. |
| 57 |
* |
| 58 |
* @return S3ClientInterface |
| 59 |
*/ |
| 60 |
public function getClient() |
| 61 |
{ |
| 62 |
return $this->client; |
| 63 |
} |
| 64 |
/** |
| 65 |
* Gets the bucket name. |
| 66 |
* |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
public function getBucket() |
| 70 |
{ |
| 71 |
return $this->bucket; |
| 72 |
} |
| 73 |
/** |
| 74 |
* Gets the form attributes as an array. |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function getFormAttributes() |
| 79 |
{ |
| 80 |
return $this->formAttributes; |
| 81 |
} |
| 82 |
/** |
| 83 |
* Set a form attribute. |
| 84 |
* |
| 85 |
* @param string $attribute Form attribute to set. |
| 86 |
* @param string $value Value to set. |
| 87 |
*/ |
| 88 |
public function setFormAttribute($attribute, $value) |
| 89 |
{ |
| 90 |
$this->formAttributes[$attribute] = $value; |
| 91 |
} |
| 92 |
/** |
| 93 |
* Gets the form inputs as an array. |
| 94 |
* |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
public function getFormInputs() |
| 98 |
{ |
| 99 |
return $this->formInputs; |
| 100 |
} |
| 101 |
/** |
| 102 |
* Set a form input. |
| 103 |
* |
| 104 |
* @param string $field Field name to set |
| 105 |
* @param string $value Value to set. |
| 106 |
*/ |
| 107 |
public function setFormInput($field, $value) |
| 108 |
{ |
| 109 |
$this->formInputs[$field] = $value; |
| 110 |
} |
| 111 |
private function generateUri() |
| 112 |
{ |
| 113 |
$uri = new Uri($this->client->getEndpoint()); |
| 114 |
if ($this->client->getConfig('use_path_style_endpoint') === \true || $uri->getScheme() === 'https' && \strpos($this->bucket, '.') !== \false) { |
| 115 |
// Use path-style URLs |
| 116 |
$uri = $uri->withPath("/{$this->bucket}"); |
| 117 |
} else { |
| 118 |
// Use virtual-style URLs if haven't been set up already |
| 119 |
if (\strpos($uri->getHost(), $this->bucket . '.') !== 0) { |
| 120 |
$uri = $uri->withHost($this->bucket . '.' . $uri->getHost()); |
| 121 |
} |
| 122 |
} |
| 123 |
return (string) $uri; |
| 124 |
} |
| 125 |
protected function getPolicyAndSignature(CredentialsInterface $credentials, array $policy) |
| 126 |
{ |
| 127 |
$ldt = \gmdate(SignatureV4::ISO8601_BASIC); |
| 128 |
$sdt = \substr($ldt, 0, 8); |
| 129 |
$policy['conditions'][] = ['X-Amz-Date' => $ldt]; |
| 130 |
$region = $this->client->getRegion(); |
| 131 |
$scope = $this->createScope($sdt, $region, 's3'); |
| 132 |
$creds = "{$credentials->getAccessKeyId()}/{$scope}"; |
| 133 |
$policy['conditions'][] = ['X-Amz-Credential' => $creds]; |
| 134 |
$policy['conditions'][] = ['X-Amz-Algorithm' => "AWS4-HMAC-SHA256"]; |
| 135 |
$jsonPolicy64 = \base64_encode(\json_encode($policy)); |
| 136 |
$key = $this->getSigningKey($sdt, $region, 's3', $credentials->getSecretKey()); |
| 137 |
return ['X-Amz-Credential' => $creds, 'X-Amz-Algorithm' => "AWS4-HMAC-SHA256", 'X-Amz-Date' => $ldt, 'Policy' => $jsonPolicy64, 'X-Amz-Signature' => \bin2hex(\hash_hmac('sha256', $jsonPolicy64, $key, \true))]; |
| 138 |
} |
| 139 |
} |
| 140 |
|