| 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 <[email protected]> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
|
| 13 |
declare(strict_types=1); |
| 14 |
|
| 15 |
namespace Ramsey\Uuid\Generator; |
| 16 |
|
| 17 |
use Ramsey\Uuid\Converter\NumberConverterInterface; |
| 18 |
use Ramsey\Uuid\Exception\DceSecurityException; |
| 19 |
use Ramsey\Uuid\Provider\DceSecurityProviderInterface; |
| 20 |
use Ramsey\Uuid\Type\Hexadecimal; |
| 21 |
use Ramsey\Uuid\Type\Integer as IntegerObject; |
| 22 |
use Ramsey\Uuid\Uuid; |
| 23 |
|
| 24 |
use function hex2bin; |
| 25 |
use function in_array; |
| 26 |
use function pack; |
| 27 |
use function str_pad; |
| 28 |
use function strlen; |
| 29 |
use function substr_replace; |
| 30 |
|
| 31 |
use const STR_PAD_LEFT; |
| 32 |
|
| 33 |
/** |
| 34 |
* DceSecurityGenerator generates strings of binary data based on a local |
| 35 |
* domain, local identifier, node ID, clock sequence, and the current time |
| 36 |
*/ |
| 37 |
class DceSecurityGenerator implements DceSecurityGeneratorInterface |
| 38 |
{ |
| 39 |
private const DOMAINS = [ |
| 40 |
Uuid::DCE_DOMAIN_PERSON, |
| 41 |
Uuid::DCE_DOMAIN_GROUP, |
| 42 |
Uuid::DCE_DOMAIN_ORG, |
| 43 |
]; |
| 44 |
|
| 45 |
/** |
| 46 |
* Upper bounds for the clock sequence in DCE Security UUIDs. |
| 47 |
*/ |
| 48 |
private const CLOCK_SEQ_HIGH = 63; |
| 49 |
|
| 50 |
/** |
| 51 |
* Lower bounds for the clock sequence in DCE Security UUIDs. |
| 52 |
*/ |
| 53 |
private const CLOCK_SEQ_LOW = 0; |
| 54 |
|
| 55 |
/** |
| 56 |
* @var NumberConverterInterface |
| 57 |
*/ |
| 58 |
private $numberConverter; |
| 59 |
|
| 60 |
/** |
| 61 |
* @var TimeGeneratorInterface |
| 62 |
*/ |
| 63 |
private $timeGenerator; |
| 64 |
|
| 65 |
/** |
| 66 |
* @var DceSecurityProviderInterface |
| 67 |
*/ |
| 68 |
private $dceSecurityProvider; |
| 69 |
|
| 70 |
public function __construct( |
| 71 |
NumberConverterInterface $numberConverter, |
| 72 |
TimeGeneratorInterface $timeGenerator, |
| 73 |
DceSecurityProviderInterface $dceSecurityProvider |
| 74 |
) { |
| 75 |
$this->numberConverter = $numberConverter; |
| 76 |
$this->timeGenerator = $timeGenerator; |
| 77 |
$this->dceSecurityProvider = $dceSecurityProvider; |
| 78 |
} |
| 79 |
|
| 80 |
public function generate( |
| 81 |
int $localDomain, |
| 82 |
?IntegerObject $localIdentifier = null, |
| 83 |
?Hexadecimal $node = null, |
| 84 |
?int $clockSeq = null |
| 85 |
): string { |
| 86 |
if (!in_array($localDomain, self::DOMAINS)) { |
| 87 |
throw new DceSecurityException( |
| 88 |
'Local domain must be a valid DCE Security domain' |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
if ($localIdentifier && $localIdentifier->isNegative()) { |
| 93 |
throw new DceSecurityException( |
| 94 |
'Local identifier out of bounds; it must be a value between 0 and 4294967295' |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
if ($clockSeq > self::CLOCK_SEQ_HIGH || $clockSeq < self::CLOCK_SEQ_LOW) { |
| 99 |
throw new DceSecurityException( |
| 100 |
'Clock sequence out of bounds; it must be a value between 0 and 63' |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
switch ($localDomain) { |
| 105 |
case Uuid::DCE_DOMAIN_ORG: |
| 106 |
if ($localIdentifier === null) { |
| 107 |
throw new DceSecurityException( |
| 108 |
'A local identifier must be provided for the org domain' |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
break; |
| 113 |
case Uuid::DCE_DOMAIN_PERSON: |
| 114 |
if ($localIdentifier === null) { |
| 115 |
$localIdentifier = $this->dceSecurityProvider->getUid(); |
| 116 |
} |
| 117 |
|
| 118 |
break; |
| 119 |
case Uuid::DCE_DOMAIN_GROUP: |
| 120 |
default: |
| 121 |
if ($localIdentifier === null) { |
| 122 |
$localIdentifier = $this->dceSecurityProvider->getGid(); |
| 123 |
} |
| 124 |
|
| 125 |
break; |
| 126 |
} |
| 127 |
|
| 128 |
$identifierHex = $this->numberConverter->toHex($localIdentifier->toString()); |
| 129 |
|
| 130 |
// The maximum value for the local identifier is 0xffffffff, or |
| 131 |
// 4294967295. This is 8 hexadecimal digits, so if the length of |
| 132 |
// hexadecimal digits is greater than 8, we know the value is greater |
| 133 |
// than 0xffffffff. |
| 134 |
if (strlen($identifierHex) > 8) { |
| 135 |
throw new DceSecurityException( |
| 136 |
'Local identifier out of bounds; it must be a value between 0 and 4294967295' |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
$domainByte = pack('n', $localDomain)[1]; |
| 141 |
$identifierBytes = (string) hex2bin(str_pad($identifierHex, 8, '0', STR_PAD_LEFT)); |
| 142 |
|
| 143 |
if ($node instanceof Hexadecimal) { |
| 144 |
$node = $node->toString(); |
| 145 |
} |
| 146 |
|
| 147 |
// Shift the clock sequence 8 bits to the left, so it matches 0x3f00. |
| 148 |
if ($clockSeq !== null) { |
| 149 |
$clockSeq = $clockSeq << 8; |
| 150 |
} |
| 151 |
|
| 152 |
$bytes = $this->timeGenerator->generate($node, $clockSeq); |
| 153 |
|
| 154 |
// Replace bytes in the time-based UUID with DCE Security values. |
| 155 |
$bytes = substr_replace($bytes, $identifierBytes, 0, 4); |
| 156 |
$bytes = substr_replace($bytes, $domainByte, 9, 1); |
| 157 |
|
| 158 |
return $bytes; |
| 159 |
} |
| 160 |
} |
| 161 |
|