media-cloud-sync
/
includes
/
sdk
/
google
/
ramsey
/
uuid
/
src
/
Generator
/
DceSecurityGenerator.php
DceSecurityGenerator.php in Media Cloud Sync 1.0.1, at includes/sdk/google/ramsey/uuid/src/Generator/DceSecurityGenerator.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the ramsey/uuid library |
| 5 | * |
| 6 | * For the full copyright and license information, please view the LICENSE |
| 7 | * file that was distributed with this source code. |
| 8 | * |
| 9 | * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | */ |
| 12 | declare (strict_types=1); |
| 13 | namespace Dudlewebs\WPMCS\Ramsey\Uuid\Generator; |
| 14 | |
| 15 | use Dudlewebs\WPMCS\Ramsey\Uuid\Converter\NumberConverterInterface; |
| 16 | use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\DceSecurityException; |
| 17 | use Dudlewebs\WPMCS\Ramsey\Uuid\Provider\DceSecurityProviderInterface; |
| 18 | use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Hexadecimal; |
| 19 | use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Integer as IntegerObject; |
| 20 | use Dudlewebs\WPMCS\Ramsey\Uuid\Uuid; |
| 21 | use function hex2bin; |
| 22 | use function in_array; |
| 23 | use function pack; |
| 24 | use function str_pad; |
| 25 | use function strlen; |
| 26 | use function substr_replace; |
| 27 | use const STR_PAD_LEFT; |
| 28 | /** |
| 29 | * DceSecurityGenerator generates strings of binary data based on a local |
| 30 | * domain, local identifier, node ID, clock sequence, and the current time |
| 31 | */ |
| 32 | class DceSecurityGenerator implements DceSecurityGeneratorInterface |
| 33 | { |
| 34 | private const DOMAINS = [Uuid::DCE_DOMAIN_PERSON, Uuid::DCE_DOMAIN_GROUP, Uuid::DCE_DOMAIN_ORG]; |
| 35 | /** |
| 36 | * Upper bounds for the clock sequence in DCE Security UUIDs. |
| 37 | */ |
| 38 | private const CLOCK_SEQ_HIGH = 63; |
| 39 | /** |
| 40 | * Lower bounds for the clock sequence in DCE Security UUIDs. |
| 41 | */ |
| 42 | private const CLOCK_SEQ_LOW = 0; |
| 43 | /** |
| 44 | * @var NumberConverterInterface |
| 45 | */ |
| 46 | private $numberConverter; |
| 47 | /** |
| 48 | * @var TimeGeneratorInterface |
| 49 | */ |
| 50 | private $timeGenerator; |
| 51 | /** |
| 52 | * @var DceSecurityProviderInterface |
| 53 | */ |
| 54 | private $dceSecurityProvider; |
| 55 | public function __construct(NumberConverterInterface $numberConverter, TimeGeneratorInterface $timeGenerator, DceSecurityProviderInterface $dceSecurityProvider) |
| 56 | { |
| 57 | $this->numberConverter = $numberConverter; |
| 58 | $this->timeGenerator = $timeGenerator; |
| 59 | $this->dceSecurityProvider = $dceSecurityProvider; |
| 60 | } |
| 61 | public function generate(int $localDomain, ?IntegerObject $localIdentifier = null, ?Hexadecimal $node = null, ?int $clockSeq = null): string |
| 62 | { |
| 63 | if (!in_array($localDomain, self::DOMAINS)) { |
| 64 | throw new DceSecurityException('Local domain must be a valid DCE Security domain'); |
| 65 | } |
| 66 | if ($localIdentifier && $localIdentifier->isNegative()) { |
| 67 | throw new DceSecurityException('Local identifier out of bounds; it must be a value between 0 and 4294967295'); |
| 68 | } |
| 69 | if ($clockSeq > self::CLOCK_SEQ_HIGH || $clockSeq < self::CLOCK_SEQ_LOW) { |
| 70 | throw new DceSecurityException('Clock sequence out of bounds; it must be a value between 0 and 63'); |
| 71 | } |
| 72 | switch ($localDomain) { |
| 73 | case Uuid::DCE_DOMAIN_ORG: |
| 74 | if ($localIdentifier === null) { |
| 75 | throw new DceSecurityException('A local identifier must be provided for the org domain'); |
| 76 | } |
| 77 | break; |
| 78 | case Uuid::DCE_DOMAIN_PERSON: |
| 79 | if ($localIdentifier === null) { |
| 80 | $localIdentifier = $this->dceSecurityProvider->getUid(); |
| 81 | } |
| 82 | break; |
| 83 | case Uuid::DCE_DOMAIN_GROUP: |
| 84 | default: |
| 85 | if ($localIdentifier === null) { |
| 86 | $localIdentifier = $this->dceSecurityProvider->getGid(); |
| 87 | } |
| 88 | break; |
| 89 | } |
| 90 | $identifierHex = $this->numberConverter->toHex($localIdentifier->toString()); |
| 91 | // The maximum value for the local identifier is 0xffffffff, or |
| 92 | // 4294967295. This is 8 hexadecimal digits, so if the length of |
| 93 | // hexadecimal digits is greater than 8, we know the value is greater |
| 94 | // than 0xffffffff. |
| 95 | if (strlen($identifierHex) > 8) { |
| 96 | throw new DceSecurityException('Local identifier out of bounds; it must be a value between 0 and 4294967295'); |
| 97 | } |
| 98 | $domainByte = pack('n', $localDomain)[1]; |
| 99 | $identifierBytes = (string) hex2bin(str_pad($identifierHex, 8, '0', STR_PAD_LEFT)); |
| 100 | if ($node instanceof Hexadecimal) { |
| 101 | $node = $node->toString(); |
| 102 | } |
| 103 | // Shift the clock sequence 8 bits to the left, so it matches 0x3f00. |
| 104 | if ($clockSeq !== null) { |
| 105 | $clockSeq = $clockSeq << 8; |
| 106 | } |
| 107 | $bytes = $this->timeGenerator->generate($node, $clockSeq); |
| 108 | // Replace bytes in the time-based UUID with DCE Security values. |
| 109 | $bytes = substr_replace($bytes, $identifierBytes, 0, 4); |
| 110 | $bytes = substr_replace($bytes, $domainByte, 9, 1); |
| 111 | return $bytes; |
| 112 | } |
| 113 | } |
| 114 |