| 1 |
<?php |
| 2 |
namespace Aws\S3; |
| 3 |
|
| 4 |
use Aws\CommandInterface; |
| 5 |
use Psr\Http\Message\RequestInterface; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class SSECMiddleware |
| 12 |
{ |
| 13 |
private $endpointScheme; |
| 14 |
private $nextHandler; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public static function wrap($endpointScheme) |
| 24 |
{ |
| 25 |
return function (callable $handler) use ($endpointScheme) { |
| 26 |
return new self($endpointScheme, $handler); |
| 27 |
}; |
| 28 |
} |
| 29 |
|
| 30 |
public function __construct($endpointScheme, callable $nextHandler) |
| 31 |
{ |
| 32 |
$this->nextHandler = $nextHandler; |
| 33 |
$this->endpointScheme = $endpointScheme; |
| 34 |
} |
| 35 |
|
| 36 |
public function __invoke( |
| 37 |
CommandInterface $command, |
| 38 |
?RequestInterface $request = null |
| 39 |
) { |
| 40 |
|
| 41 |
if (($command['SSECustomerKey'] || $command['CopySourceSSECustomerKey']) |
| 42 |
&& $this->endpointScheme !== 'https' |
| 43 |
) { |
| 44 |
throw new \RuntimeException('You must configure your S3 client to ' |
| 45 |
. 'use HTTPS in order to use the SSE-C features.'); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
if ($command['SSECustomerKey']) { |
| 50 |
$this->prepareSseParams($command); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
if ($command['CopySourceSSECustomerKey']) { |
| 55 |
$this->prepareSseParams($command, 'CopySource'); |
| 56 |
} |
| 57 |
|
| 58 |
$f = $this->nextHandler; |
| 59 |
return $f($command, $request); |
| 60 |
} |
| 61 |
|
| 62 |
private function prepareSseParams(CommandInterface $command, $prefix = '') |
| 63 |
{ |
| 64 |
|
| 65 |
$key = $command[$prefix . 'SSECustomerKey']; |
| 66 |
$command[$prefix . 'SSECustomerKey'] = base64_encode($key); |
| 67 |
|
| 68 |
|
| 69 |
if ($md5 = $command[$prefix . 'SSECustomerKeyMD5']) { |
| 70 |
$command[$prefix . 'SSECustomerKeyMD5'] = base64_encode($md5); |
| 71 |
} else { |
| 72 |
$command[$prefix . 'SSECustomerKeyMD5'] = base64_encode(md5($key, true)); |
| 73 |
} |
| 74 |
} |
| 75 |
} |