1 |
efrain |
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 |
* @internal Adds computed values to service operations that need presigned url.
|
|
|
11 |
*/
|
|
|
12 |
class PresignUrlMiddleware
|
|
|
13 |
{
|
|
|
14 |
private $client;
|
|
|
15 |
private $endpointProvider;
|
|
|
16 |
private $nextHandler;
|
|
|
17 |
/** @var array names of operations that require presign url */
|
|
|
18 |
private $commandPool;
|
|
|
19 |
/** @var array query params that are not on the operation's model to add before signing */
|
|
|
20 |
private $extraQueryParams;
|
|
|
21 |
/** @var string */
|
|
|
22 |
private $serviceName;
|
|
|
23 |
/** @var string */
|
|
|
24 |
private $presignParam;
|
|
|
25 |
/** @var bool */
|
|
|
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 = $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 function (callable $handler) use ($endpointProvider, $client, $options) {
|
|
|
54 |
$f = new PresignUrlMiddleware($options, $endpointProvider, $client, $handler);
|
|
|
55 |
return $f;
|
|
|
56 |
};
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public function __invoke(CommandInterface $cmd, RequestInterface $request = null)
|
|
|
60 |
{
|
|
|
61 |
if (in_array($cmd->getName(), $this->commandPool)
|
|
|
62 |
&& (!isset($cmd->{'__skip' . $cmd->getName()}))
|
|
|
63 |
) {
|
|
|
64 |
$cmd['DestinationRegion'] = $this->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($this->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 |
// Avoid infinite recursion by flagging the new command.
|
|
|
87 |
$newCmd->{'__skip' . $cmdName} = true;
|
|
|
88 |
|
|
|
89 |
// Serialize a request for the operation.
|
|
|
90 |
$request = \Aws\serialize($newCmd);
|
|
|
91 |
// Create the new endpoint for the target endpoint.
|
|
|
92 |
if ($this->endpointProvider instanceof \Aws\EndpointV2\EndpointProviderV2) {
|
|
|
93 |
$providerArgs = array_merge(
|
|
|
94 |
$this->client->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 |
// Set the request to hit the target endpoint.
|
|
|
106 |
$uri = $request->getUri()->withHost((new Uri($endpoint))->getHost());
|
|
|
107 |
$request = $request->withUri($uri);
|
|
|
108 |
|
|
|
109 |
// Create a presigned URL for our generated request.
|
|
|
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}={$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 |
}
|