Skip to content

Commit

Permalink
Add mandatory UUID (#63)
Browse files Browse the repository at this point in the history
* Add mandatory UUID

* Fixes in tests

* additional validation and tests

* PubNub SDK v5.0.0 release.

Co-authored-by: Client Engineering Bot <60980775+Client Engineering [email protected]>
  • Loading branch information
seba-aln and Client Engineering Bot authored Jan 26, 2022
1 parent 3377dcf commit 16763cd
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 25 deletions.
9 changes: 7 additions & 2 deletions .pubnub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ You will need the publish and subscribe keys to authenticate your app. Get your
{
"require": {
<!-- include the latest version from the badge at the top -->
"pubnub/pubnub": "4.7.0"
"pubnub/pubnub": "5.0.0"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/PubNub/Exceptions/PubNubConfigurationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace PubNub\Exceptions;

class PubNubConfigurationException extends PubNubException
{

}
15 changes: 10 additions & 5 deletions src/PubNub/PNConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PubNub;

use PubNub\Exceptions\PubNubConfigurationException;
use PubNub\Exceptions\PubNubValidationException;
use Requests_Transport;

Expand Down Expand Up @@ -76,6 +77,7 @@ public static function demoKeys()
$config = new static();
$config->setSubscribeKey("demo");
$config->setPublishKey("demo");
$config->setUuid("demo");

return $config;
}
Expand Down Expand Up @@ -232,10 +234,6 @@ public function setSecure($ssl)
*/
public function getUuid()
{
if (empty($this->uuid)) {
$this->uuid = PubNubUtil::uuid();
}

return $this->uuid;
}

Expand All @@ -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;
Expand Down Expand Up @@ -391,12 +392,16 @@ public function getUseRandomIV()
public function setUseRandomIV($useRandomIV)
{
$this->useRandomIV = $useRandomIV;

if ($this->crypto != null) {
$this->crypto->setUseRandomIV($this->useRandomIV);
}

return $this;
}

private function validateNotEmptyString($value)
{
return (is_string($value) && strlen(trim($value)) > 0);
}
}
16 changes: 15 additions & 1 deletion src/PubNub/PubNub.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@
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;
use PubNub\Managers\TokenManager;

class PubNub
{
const SDK_VERSION = "4.7.0";
const SDK_VERSION = "5.0.0";
const SDK_NAME = "PubNub-PHP";

public static $MAX_SEQUENCE = 65535;
Expand Down Expand Up @@ -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);
Expand All @@ -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
*/
Expand Down
14 changes: 11 additions & 3 deletions src/PubNub/PubNubUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}

Expand Down
16 changes: 10 additions & 6 deletions tests/PubNubTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions tests/functional/EndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/PublishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
43 changes: 43 additions & 0 deletions tests/functional/UuidTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Tests\Functional;

use PubNub\Exceptions\PubNubConfigurationException;
use PubNubTestCase;
use PubNub\Exceptions\PubNubValidationException;
use PubNub\PNConfiguration;
use PubNub\PubNub;

class UuidTest extends PubNubTestCase
{
public function testValidateOnSet()
{
$this->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);
}
}
1 change: 1 addition & 0 deletions tests/integrational/PublishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public function testServerSideErrorEnvelope()
{
$pnconf = PNConfiguration::demoKeys();
$pnconf->setPublishKey("fake");
$pnconf->setUuid('fake');

$pubnub = new PubNub($pnconf);

Expand Down

0 comments on commit 16763cd

Please sign in to comment.