updated-packages
This commit is contained in:
@@ -16,6 +16,8 @@ class PresignUrlMiddleware
|
||||
private $nextHandler;
|
||||
/** @var array names of operations that require presign url */
|
||||
private $commandPool;
|
||||
/** @var array query params that are not on the operation's model to add before signing */
|
||||
private $extraQueryParams;
|
||||
/** @var string */
|
||||
private $serviceName;
|
||||
/** @var string */
|
||||
@@ -25,7 +27,7 @@ class PresignUrlMiddleware
|
||||
|
||||
public function __construct(
|
||||
array $options,
|
||||
callable $endpointProvider,
|
||||
$endpointProvider,
|
||||
AwsClientInterface $client,
|
||||
callable $nextHandler
|
||||
) {
|
||||
@@ -34,13 +36,18 @@ class PresignUrlMiddleware
|
||||
$this->nextHandler = $nextHandler;
|
||||
$this->commandPool = $options['operations'];
|
||||
$this->serviceName = $options['service'];
|
||||
$this->presignParam = $options['presign_param'];
|
||||
$this->presignParam = !empty($options['presign_param'])
|
||||
? $options['presign_param']
|
||||
: 'PresignedUrl';
|
||||
$this->extraQueryParams = !empty($options['extra_query_params'])
|
||||
? $options['extra_query_params']
|
||||
: [];
|
||||
$this->requireDifferentRegion = !empty($options['require_different_region']);
|
||||
}
|
||||
|
||||
public static function wrap(
|
||||
AwsClientInterface $client,
|
||||
callable $endpointProvider,
|
||||
$endpointProvider,
|
||||
array $options = []
|
||||
) {
|
||||
return function (callable $handler) use ($endpointProvider, $client, $options) {
|
||||
@@ -55,6 +62,9 @@ class PresignUrlMiddleware
|
||||
&& (!isset($cmd->{'__skip' . $cmd->getName()}))
|
||||
) {
|
||||
$cmd['DestinationRegion'] = $this->client->getRegion();
|
||||
if (!empty($cmd['SourceRegion']) && !empty($cmd[$this->presignParam])) {
|
||||
goto nexthandler;
|
||||
}
|
||||
if (!$this->requireDifferentRegion
|
||||
|| (!empty($cmd['SourceRegion'])
|
||||
&& $cmd['SourceRegion'] !== $cmd['DestinationRegion'])
|
||||
@@ -62,9 +72,9 @@ class PresignUrlMiddleware
|
||||
$cmd[$this->presignParam] = $this->createPresignedUrl($this->client, $cmd);
|
||||
}
|
||||
}
|
||||
|
||||
$f = $this->nextHandler;
|
||||
return $f($cmd, $request);
|
||||
nexthandler:
|
||||
$nextHandler = $this->nextHandler;
|
||||
return $nextHandler($cmd, $request);
|
||||
}
|
||||
|
||||
private function createPresignedUrl(
|
||||
@@ -79,19 +89,38 @@ class PresignUrlMiddleware
|
||||
// Serialize a request for the operation.
|
||||
$request = \Aws\serialize($newCmd);
|
||||
// Create the new endpoint for the target endpoint.
|
||||
$endpoint = EndpointProvider::resolve($this->endpointProvider, [
|
||||
'region' => $cmd['SourceRegion'],
|
||||
'service' => $this->serviceName,
|
||||
])['endpoint'];
|
||||
if ($this->endpointProvider instanceof \Aws\EndpointV2\EndpointProviderV2) {
|
||||
$providerArgs = array_merge(
|
||||
$this->client->getEndpointProviderArgs(),
|
||||
['Region' => $cmd['SourceRegion']]
|
||||
);
|
||||
$endpoint = $this->endpointProvider->resolveEndpoint($providerArgs)->getUrl();
|
||||
} else {
|
||||
$endpoint = EndpointProvider::resolve($this->endpointProvider, [
|
||||
'region' => $cmd['SourceRegion'],
|
||||
'service' => $this->serviceName,
|
||||
])['endpoint'];
|
||||
}
|
||||
|
||||
// Set the request to hit the target endpoint.
|
||||
$uri = $request->getUri()->withHost((new Uri($endpoint))->getHost());
|
||||
$request = $request->withUri($uri);
|
||||
|
||||
// Create a presigned URL for our generated request.
|
||||
$signer = new SignatureV4($this->serviceName, $cmd['SourceRegion']);
|
||||
|
||||
$currentQueryParams = (string) $request->getBody();
|
||||
$paramsToAdd = false;
|
||||
if (!empty($this->extraQueryParams[$cmdName])) {
|
||||
foreach ($this->extraQueryParams[$cmdName] as $param) {
|
||||
if (!strpos($currentQueryParams, $param)) {
|
||||
$paramsToAdd = "&{$param}={$cmd[$param]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (string) $signer->presign(
|
||||
SignatureV4::convertPostToGet($request),
|
||||
SignatureV4::convertPostToGet($request, $paramsToAdd ?: ""),
|
||||
$client->getCredentials()->wait(),
|
||||
'+1 hour'
|
||||
)->getUri();
|
||||
|
||||
Reference in New Issue
Block a user