| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
use Aws\Signature\SignatureV4; |
| 5 |
use Aws\Endpoint\EndpointProvider; |
| 6 |
use GuzzleHttp\Psr7\Uri; |
| 7 |
use Psr\Http\Message\RequestInterface; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class PresignUrlMiddleware |
| 13 |
{ |
| 14 |
private \WeakReference $client; |
| 15 |
private $endpointProvider; |
| 16 |
private $nextHandler; |
| 17 |
|
| 18 |
private $commandPool; |
| 19 |
|
| 20 |
private $extraQueryParams; |
| 21 |
|
| 22 |
private $serviceName; |
| 23 |
|
| 24 |
private $presignParam; |
| 25 |
|
| 26 |
private $requireDifferentRegion; |
| 27 |
|
| 28 |
public function __construct( |
| 29 |
array $options, |
| 30 |
$endpointProvider, |
| 31 |
AwsClientInterface $client, |
| 32 |
callable $nextHandler |
| 33 |
) { |
| 34 |
$this->endpointProvider = $endpointProvider; |
| 35 |
$this->client = \WeakReference::create($client); |
| 36 |
$this->nextHandler = $nextHandler; |
| 37 |
$this->commandPool = $options['operations']; |
| 38 |
$this->serviceName = $options['service']; |
| 39 |
$this->presignParam = !empty($options['presign_param']) |
| 40 |
? $options['presign_param'] |
| 41 |
: 'PresignedUrl'; |
| 42 |
$this->extraQueryParams = !empty($options['extra_query_params']) |
| 43 |
? $options['extra_query_params'] |
| 44 |
: []; |
| 45 |
$this->requireDifferentRegion = !empty($options['require_different_region']); |
| 46 |
} |
| 47 |
|
| 48 |
public static function wrap( |
| 49 |
AwsClientInterface $client, |
| 50 |
$endpointProvider, |
| 51 |
array $options = [] |
| 52 |
) { |
| 53 |
return static function (callable $handler) use ($endpointProvider, $client, $options) { |
| 54 |
return new PresignUrlMiddleware($options, $endpointProvider, $client, $handler); |
| 55 |
}; |
| 56 |
} |
| 57 |
|
| 58 |
public function __invoke(CommandInterface $cmd, ?RequestInterface $request = null) |
| 59 |
{ |
| 60 |
if (in_array($cmd->getName(), $this->commandPool) |
| 61 |
&& (!isset($cmd['__skip' . $cmd->getName()])) |
| 62 |
) { |
| 63 |
$client = $this->client->get(); |
| 64 |
$cmd['DestinationRegion'] = $client->getRegion(); |
| 65 |
if (!empty($cmd['SourceRegion']) && !empty($cmd[$this->presignParam])) { |
| 66 |
goto nexthandler; |
| 67 |
} |
| 68 |
if (!$this->requireDifferentRegion |
| 69 |
|| (!empty($cmd['SourceRegion']) |
| 70 |
&& $cmd['SourceRegion'] !== $cmd['DestinationRegion']) |
| 71 |
) { |
| 72 |
$cmd[$this->presignParam] = $this->createPresignedUrl($client, $cmd); |
| 73 |
} |
| 74 |
} |
| 75 |
nexthandler: |
| 76 |
$nextHandler = $this->nextHandler; |
| 77 |
return $nextHandler($cmd, $request); |
| 78 |
} |
| 79 |
|
| 80 |
private function createPresignedUrl( |
| 81 |
AwsClientInterface $client, |
| 82 |
CommandInterface $cmd |
| 83 |
) { |
| 84 |
$cmdName = $cmd->getName(); |
| 85 |
$newCmd = $client->getCommand($cmdName, $cmd->toArray()); |
| 86 |
|
| 87 |
$newCmd['__skip' . $cmdName] = true; |
| 88 |
|
| 89 |
|
| 90 |
$request = \Aws\serialize($newCmd); |
| 91 |
|
| 92 |
if ($this->endpointProvider instanceof \Aws\EndpointV2\EndpointProviderV2) { |
| 93 |
$providerArgs = array_merge( |
| 94 |
$this->client->get()->getEndpointProviderArgs(), |
| 95 |
['Region' => $cmd['SourceRegion']] |
| 96 |
); |
| 97 |
$endpoint = $this->endpointProvider->resolveEndpoint($providerArgs)->getUrl(); |
| 98 |
} else { |
| 99 |
$endpoint = EndpointProvider::resolve($this->endpointProvider, [ |
| 100 |
'region' => $cmd['SourceRegion'], |
| 101 |
'service' => $this->serviceName, |
| 102 |
])['endpoint']; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
$uri = $request->getUri()->withHost((new Uri($endpoint))->getHost()); |
| 107 |
$request = $request->withUri($uri); |
| 108 |
|
| 109 |
|
| 110 |
$signer = new SignatureV4($this->serviceName, $cmd['SourceRegion']); |
| 111 |
|
| 112 |
$currentQueryParams = (string) $request->getBody(); |
| 113 |
$paramsToAdd = false; |
| 114 |
if (!empty($this->extraQueryParams[$cmdName])) { |
| 115 |
foreach ($this->extraQueryParams[$cmdName] as $param) { |
| 116 |
if (!strpos($currentQueryParams, $param)) { |
| 117 |
$paramsToAdd = "&{$param}=" . urlencode($cmd[$param]); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return (string) $signer->presign( |
| 123 |
SignatureV4::convertPostToGet($request, $paramsToAdd ?: ""), |
| 124 |
$client->getCredentials()->wait(), |
| 125 |
'+1 hour' |
| 126 |
)->getUri(); |
| 127 |
} |
| 128 |
} |