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