Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/IdempotencyTokenMiddleware.php

raw symbols SCIP
1 <?php
2 namespace Aws;
3
4 use Aws\Api\Service;
5 use Psr\Http\Message\RequestInterface;
6
7 /**
8 * @internal Middleware that auto fills parameters with `idempotencyToken` trait
9 */
10 class IdempotencyTokenMiddleware
11 {
12 /** @var Service */
13 private $service;
14
15 /** @var string */
16 private $bytesGenerator;
17
18 /** @var callable */
19 private $nextHandler;
20
21 /**
22 * Creates a middleware that populates operation parameter
23 * with trait 'idempotencyToken' enabled with a random UUIDv4
24 *
25 * One of following functions needs to be available
26 * in order to generate random bytes used for UUID
27 * (SDK will attempt to utilize function in following order):
28 * - random_bytes (requires PHP 7.0 or above)
29 * - openssl_random_pseudo_bytes (requires 'openssl' module enabled)
30 * - mcrypt_create_iv (requires 'mcrypt' module enabled)
31 *
32 * You may also supply a custom bytes generator as an optional second
33 * parameter.
34 *
35 * @param \Aws\Api\Service $service
36 * @param callable|null $bytesGenerator
37 *
38 * @return callable
39 */
40 public static function wrap(
41 Service $service,
42 ?callable $bytesGenerator = null
43 ) {
44 return function (callable $handler) use ($service, $bytesGenerator) {
45 return new self($handler, $service, $bytesGenerator);
46 };
47 }
48
49 public function __construct(
50 callable $nextHandler,
51 Service $service,
52 ?callable $bytesGenerator = null
53 ) {
54 $this->bytesGenerator = $bytesGenerator
55 ?: $this->findCompatibleRandomSource();
56 $this->service = $service;
57 $this->nextHandler = $nextHandler;
58 }
59
60 public function __invoke(
61 CommandInterface $command,
62 ?RequestInterface $request = null
63 ) {
64 $handler = $this->nextHandler;
65 if ($this->bytesGenerator) {
66 $operation = $this->service->getOperation($command->getName());
67 $members = $operation->getInput()->getMembers();
68 foreach ($members as $member => $value) {
69 if ($value['idempotencyToken']) {
70 $bytes = call_user_func($this->bytesGenerator, 16);
71 // populating UUIDv4 only when the parameter is not set
72 $command[$member] = $command[$member]
73 ?: $this->getUuidV4($bytes);
74 // only one member could have the trait enabled
75 break;
76 }
77 }
78 }
79 return $handler($command, $request);
80 }
81
82 /**
83 * This function generates a random UUID v4 string,
84 * which is used as auto filled token value.
85 *
86 * @param string $bytes 16 bytes of pseudo-random bytes
87 * @return string
88 * More information about UUID v4, see:
89 * https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
90 * https://tools.ietf.org/html/rfc4122#page-14
91 */
92 private static function getUuidV4($bytes)
93 {
94 // set version to 0100
95 $bytes[6] = chr(ord($bytes[6]) & 0x0f | 0x40);
96 // set bits 6-7 to 10
97 $bytes[8] = chr(ord($bytes[8]) & 0x3f | 0x80);
98 return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
99 }
100
101 /**
102 * This function decides the PHP function used in generating random bytes.
103 *
104 * @return callable|null
105 */
106 private function findCompatibleRandomSource()
107 {
108 if (function_exists('random_bytes')) {
109 return 'random_bytes';
110 }
111
112 if (function_exists('openssl_random_pseudo_bytes')) {
113 return 'openssl_random_pseudo_bytes';
114 }
115
116 if (function_exists('mcrypt_create_iv')) {
117 return 'mcrypt_create_iv';
118 }
119 }
120 }