| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Signature; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Credentials\CredentialsInterface; |
| 6 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 7 |
/** |
| 8 |
* Amazon S3 signature version 4 support. |
| 9 |
*/ |
| 10 |
class S3SignatureV4 extends SignatureV4 |
| 11 |
{ |
| 12 |
/** |
| 13 |
* S3-specific signing logic |
| 14 |
* |
| 15 |
* {@inheritdoc} |
| 16 |
*/ |
| 17 |
use SignatureTrait; |
| 18 |
public function signRequest(RequestInterface $request, CredentialsInterface $credentials, $signingService = null) |
| 19 |
{ |
| 20 |
// Always add a x-amz-content-sha-256 for data integrity |
| 21 |
if (!$request->hasHeader('x-amz-content-sha256')) { |
| 22 |
$request = $request->withHeader('x-amz-content-sha256', $this->getPayload($request)); |
| 23 |
} |
| 24 |
$useCrt = \strpos($request->getUri()->getHost(), "accesspoint.s3-global") !== \false; |
| 25 |
if (!$useCrt) { |
| 26 |
if (\strpos($request->getUri()->getHost(), "s3-object-lambda")) { |
| 27 |
return parent::signRequest($request, $credentials, "s3-object-lambda"); |
| 28 |
} |
| 29 |
return parent::signRequest($request, $credentials); |
| 30 |
} |
| 31 |
$signingService = $signingService ?: 's3'; |
| 32 |
return $this->signWithV4a($credentials, $request, $signingService); |
| 33 |
} |
| 34 |
/** |
| 35 |
* Always add a x-amz-content-sha-256 for data integrity. |
| 36 |
* |
| 37 |
* {@inheritdoc} |
| 38 |
*/ |
| 39 |
public function presign(RequestInterface $request, CredentialsInterface $credentials, $expires, array $options = []) |
| 40 |
{ |
| 41 |
if (!$request->hasHeader('x-amz-content-sha256')) { |
| 42 |
$request = $request->withHeader('X-Amz-Content-Sha256', $this->getPresignedPayload($request)); |
| 43 |
} |
| 44 |
if (\strpos($request->getUri()->getHost(), "accesspoint.s3-global")) { |
| 45 |
$request = $request->withHeader("x-amz-region-set", "*"); |
| 46 |
} |
| 47 |
return parent::presign($request, $credentials, $expires, $options); |
| 48 |
} |
| 49 |
/** |
| 50 |
* Override used to allow pre-signed URLs to be created for an |
| 51 |
* in-determinate request payload. |
| 52 |
*/ |
| 53 |
protected function getPresignedPayload(RequestInterface $request) |
| 54 |
{ |
| 55 |
return SignatureV4::UNSIGNED_PAYLOAD; |
| 56 |
} |
| 57 |
/** |
| 58 |
* Amazon S3 does not double-encode the path component in the canonical request |
| 59 |
*/ |
| 60 |
protected function createCanonicalizedPath($path) |
| 61 |
{ |
| 62 |
// Only remove one slash in case of keys that have a preceding slash |
| 63 |
if (\substr($path, 0, 1) === '/') { |
| 64 |
$path = \substr($path, 1); |
| 65 |
} |
| 66 |
return '/' . $path; |
| 67 |
} |
| 68 |
} |
| 69 |
|