Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/S3/SSECMiddleware.php

raw symbols SCIP
1 <?php
2 namespace Aws\S3;
3
4 use Aws\CommandInterface;
5 use Psr\Http\Message\RequestInterface;
6
7 /**
8 * Simplifies the SSE-C process by encoding and hashing the key.
9 * @internal
10 */
11 class SSECMiddleware
12 {
13 private $endpointScheme;
14 private $nextHandler;
15
16 /**
17 * Provide the URI scheme of the client sending requests.
18 *
19 * @param string $endpointScheme URI scheme (http/https).
20 *
21 * @return callable
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 // Allows only HTTPS connections when using SSE-C
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 // Prepare the normal SSE-CPK headers
49 if ($command['SSECustomerKey']) {
50 $this->prepareSseParams($command);
51 }
52
53 // If it's a copy operation, prepare the SSE-CPK headers for the source.
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 // Base64 encode the provided key
65 $key = $command[$prefix . 'SSECustomerKey'];
66 $command[$prefix . 'SSECustomerKey'] = base64_encode($key);
67
68 // Base64 the provided MD5 or, generate an MD5 if not provided
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 }