diff --git a/.pubnub.yml b/.pubnub.yml index 664976a2..86370e61 100644 --- a/.pubnub.yml +++ b/.pubnub.yml @@ -3,6 +3,11 @@ version: 4.7.0 schema: 1 scm: github.com/pubnub/php changelog: + - date: 2022-01-26 + version: v5.0.0 + changes: + - type: improvement + text: "BREAKING CHANGES: Disable automated uuid generation and make it mandatory to specify before `PubNub` instance creation." - date: 2021-12-16 version: v4.7.0 changes: @@ -360,8 +365,8 @@ sdks: - x86-64 - distribution-type: library distribution-repository: GitHub release - package-name: php-4.7.0.zip - location: https://github.com/pubnub/php/releases/tag/v4.7.0 + package-name: php-5.0.0.zip + location: https://github.com/pubnub/php/releases/tag/v5.0.0 requires: - name: rmccue/requests min-version: 1.0.0 diff --git a/CHANGELOG.md b/CHANGELOG.md index 2144244d..3fe826ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## v5.0.0 +January 26 2022 + +#### Modified +- BREAKING CHANGES: Disable automated uuid generation and make it mandatory to specify before `PubNub` instance creation. + ## v4.7.0 December 16 2021 diff --git a/README.md b/README.md index 468ac20f..962a469b 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ You will need the publish and subscribe keys to authenticate your app. Get your { "require": { - "pubnub/pubnub": "4.7.0" + "pubnub/pubnub": "5.0.0" } } ``` diff --git a/composer.json b/composer.json index 9d38e20f..cae3605e 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "keywords": ["api", "real-time", "realtime", "real time", "ajax", "push"], "homepage": "http://www.pubnub.com/", "license": "MIT", - "version": "4.7.0", + "version": "5.0.0", "authors": [ { "name": "PubNub", diff --git a/src/PubNub/Exceptions/PubNubConfigurationException.php b/src/PubNub/Exceptions/PubNubConfigurationException.php new file mode 100644 index 00000000..7892425f --- /dev/null +++ b/src/PubNub/Exceptions/PubNubConfigurationException.php @@ -0,0 +1,8 @@ +setSubscribeKey("demo"); $config->setPublishKey("demo"); + $config->setUuid("demo"); return $config; } @@ -232,10 +234,6 @@ public function setSecure($ssl) */ public function getUuid() { - if (empty($this->uuid)) { - $this->uuid = PubNubUtil::uuid(); - } - return $this->uuid; } @@ -245,6 +243,9 @@ public function getUuid() */ public function setUuid($uuid) { + if (!$this->validateNotEmptyString($uuid)) { + throw new PubNubConfigurationException("UUID should not be empty"); + } $this->uuid = $uuid; return $this; @@ -391,7 +392,7 @@ public function getUseRandomIV() public function setUseRandomIV($useRandomIV) { $this->useRandomIV = $useRandomIV; - + if ($this->crypto != null) { $this->crypto->setUseRandomIV($this->useRandomIV); } @@ -399,4 +400,8 @@ public function setUseRandomIV($useRandomIV) return $this; } + private function validateNotEmptyString($value) + { + return (is_string($value) && strlen(trim($value)) > 0); + } } diff --git a/src/PubNub/PubNub.php b/src/PubNub/PubNub.php index 65619471..2f795dbe 100644 --- a/src/PubNub/PubNub.php +++ b/src/PubNub/PubNub.php @@ -42,6 +42,7 @@ use PubNub\Endpoints\Push\RemoveChannelsFromPush; use PubNub\Endpoints\Push\RemoveDeviceFromPush; use PubNub\Endpoints\Time; +use PubNub\Exceptions\PubNubConfigurationException; use PubNub\Managers\BasePathManager; use PubNub\Managers\SubscriptionManager; use PubNub\Managers\TelemetryManager; @@ -49,7 +50,7 @@ class PubNub { - const SDK_VERSION = "4.7.0"; + const SDK_VERSION = "5.0.0"; const SDK_NAME = "PubNub-PHP"; public static $MAX_SEQUENCE = 65535; @@ -82,6 +83,7 @@ class PubNub */ public function __construct($initialConfig) { + $this->validateConfig($initialConfig); $this->configuration = $initialConfig; $this->basePathManager = new BasePathManager($initialConfig); $this->subscriptionManager = new SubscriptionManager($this); @@ -99,6 +101,18 @@ public static function demo() return new static(PNConfiguration::demoKeys()); } + /** + * @param $configuration PNConfiguration + * + * @throws PubNubConfigurationException + */ + private function validateConfig(PNConfiguration $configuration) + { + if (empty($configuration->getUuid())) { + throw new PubNubConfigurationException('UUID should not be empty'); + } + } + /** * @param SubscribeCallback $listener */ diff --git a/src/PubNub/PubNubUtil.php b/src/PubNub/PubNubUtil.php index 39b15f39..063c5d9a 100755 --- a/src/PubNub/PubNubUtil.php +++ b/src/PubNub/PubNubUtil.php @@ -113,9 +113,17 @@ public static function uuid() if (function_exists('com_create_guid') === true) { return trim(com_create_guid(), '{}'); } else { - return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), - mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), - mt_rand(0, 65535)); + return sprintf( + '%04X%04X-%04X-%04X-%04X-%04X%04X%04X', + mt_rand(0, 65535), + mt_rand(0, 65535), + mt_rand(0, 65535), + mt_rand(16384, 20479), + mt_rand(32768, 49151), + mt_rand(0, 65535), + mt_rand(0, 65535), + mt_rand(0, 65535) + ); } } diff --git a/tests/PubNubTestCase.php b/tests/PubNubTestCase.php index bdfac931..1c5c4ead 100755 --- a/tests/PubNubTestCase.php +++ b/tests/PubNubTestCase.php @@ -34,11 +34,11 @@ abstract class PubNubTestCase extends TestCase protected $encodedSdkName; protected function fakeSignature($params, $httpMethod, $timestamp, $publishKey, $path, $secretKey) { - + $params['timestamp'] = (string) $timestamp; - + ksort($params); - + $signedInput = $httpMethod . "\n" . $publishKey @@ -47,14 +47,14 @@ protected function fakeSignature($params, $httpMethod, $timestamp, $publishKey, . "\n" . PubNubUtil::preparePamParams($params) . "\n"; - + $signature = 'v2.' . PubNubUtil::signSha256( $secretKey, $signedInput ); - + $signature = preg_replace('/=+$/', '', $signature); - + return $signature; } @@ -65,22 +65,26 @@ public function setUp() $publishKeyPam = getenv("PUBLISH_PAM_KEY"); $subscribeKeyPam = getenv("SUBSCRIBE_PAM_KEY"); $secretKeyPam = getenv("SECRET_PAM_KEY"); + $uuidMock = getenv("UUID_MOCK"); parent::setUp(); $this->config = new PNConfiguration(); $this->config->setSubscribeKey($subscribeKey); $this->config->setPublishKey($publishKey); + $this->config->setUuid($uuidMock); $this->config_enc = new PNConfiguration(); $this->config_enc->setSubscribeKey($subscribeKey); $this->config_enc->setPublishKey($publishKey); $this->config_enc->setCipherKey(static::CIPHER_KEY); + $this->config_enc->setUuid($uuidMock); $this->config_pam = new PNConfiguration(); $this->config_pam->setSubscribeKey($subscribeKeyPam); $this->config_pam->setPublishKey($publishKeyPam); $this->config_pam->setSecretKey($secretKeyPam); + $this->config_pam->setUuid($uuidMock); $this->pubnub = new PubNub($this->config); $this->pubnub_enc = new PubNub($this->config_enc); diff --git a/tests/functional/EndpointTest.php b/tests/functional/EndpointTest.php index df926353..d9e6f066 100644 --- a/tests/functional/EndpointTest.php +++ b/tests/functional/EndpointTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use PubNub\Endpoints\Endpoint; +use PubNub\Exceptions\PubNubConfigurationException; use PubNub\Exceptions\PubNubValidationException; use PubNub\PNConfiguration; use PubNub\PubNub; @@ -15,7 +16,7 @@ class EndpointTest extends TestCase public function testValidatesSubscribeKeyNotSet() { - $pubnub = new PubNub(new PNConfiguration()); + $pubnub = new PubNub((new PNConfiguration())->setUuid('fake')); $endpoint = new EndpointImplementation($pubnub); try { @@ -28,7 +29,7 @@ public function testValidatesSubscribeKeyNotSet() public function testValidatesSubscribeKeyEmptyString() { - $pubnub = new PubNub((new PNConfiguration())->setSubscribeKey("")); + $pubnub = new PubNub((new PNConfiguration())->setUuid('fake')->setSubscribeKey("")); $endpoint = new EndpointImplementation($pubnub); try { @@ -41,7 +42,7 @@ public function testValidatesSubscribeKeyEmptyString() public function testValidatesPublishKeyNull() { - $pubnub = new PubNub(new PNConfiguration()); + $pubnub = new PubNub((new PNConfiguration())->setUuid('fake')); $endpoint = new EndpointImplementation($pubnub); try { @@ -54,7 +55,7 @@ public function testValidatesPublishKeyNull() public function testValidatesPublishKeyEmptyString() { - $pubnub = new PubNub((new PNConfiguration())->setPublishKey("")); + $pubnub = new PubNub((new PNConfiguration())->setUuid('fake')->setPublishKey("")); $endpoint = new EndpointImplementation($pubnub); try { diff --git a/tests/functional/PublishTest.php b/tests/functional/PublishTest.php index 5f120441..ec5b1fcc 100644 --- a/tests/functional/PublishTest.php +++ b/tests/functional/PublishTest.php @@ -17,7 +17,7 @@ class PublishTest extends \PubNubTestCase public function testValidatesMessageNotEmpty() { - $pubnub = new PubNub(new PNConfiguration()); + $pubnub = new PubNub((new PNConfiguration())->setUuid('fake')); $publish = new Publish($pubnub); try { @@ -30,7 +30,7 @@ public function testValidatesMessageNotEmpty() public function testValidatesChannelNotEmpty() { - $pubnub = new PubNub(new PNConfiguration()); + $pubnub = new PubNub((new PNConfiguration())->setUuid('fake')); $publish = new Publish($pubnub); try { diff --git a/tests/functional/UuidTest.php b/tests/functional/UuidTest.php new file mode 100644 index 00000000..bd40b07e --- /dev/null +++ b/tests/functional/UuidTest.php @@ -0,0 +1,43 @@ +expectException(PubNubConfigurationException::class); + $this->expectExceptionMessage("UUID should not be empty"); + $config = new PNConfiguration(); + $config->setPublishKey('fake') + ->setSubscribeKey('fake') + ->setUuid([123]); + } + + public function testValidateOnSetWhitespace() + { + $this->expectException(PubNubConfigurationException::class); + $this->expectExceptionMessage("UUID should not be empty"); + $config = new PNConfiguration(); + $config->setPublishKey('fake') + ->setSubscribeKey('fake') + ->setUuid(" "); + } + + public function testValidateOnInit() + { + $this->expectException(PubNubConfigurationException::class); + $this->expectExceptionMessage("UUID should not be empty"); + $config = new PNConfiguration(); + $config->setPublishKey('fake') + ->setSubscribeKey('fake'); + + new PubNub($config); + } +} diff --git a/tests/integrational/PublishTest.php b/tests/integrational/PublishTest.php index 48c69518..fadcece4 100644 --- a/tests/integrational/PublishTest.php +++ b/tests/integrational/PublishTest.php @@ -157,6 +157,7 @@ public function testServerSideErrorEnvelope() { $pnconf = PNConfiguration::demoKeys(); $pnconf->setPublishKey("fake"); + $pnconf->setUuid('fake'); $pubnub = new PubNub($pnconf);