From b3b0e66fb35d6e187ac7136e3afd7dbfe04d64d2 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Wed, 27 Sep 2017 21:32:28 +0200 Subject: [PATCH] ConnectionConfiguration - add $verifySslCertificate property - if is set to true, the SSL certificate verification is turned on, otherwise - turned off It's useful while running application with custom, non-official SSL certificate, e.g. while development process. --- .../Configuration/ConnectionConfiguration.php | 45 ++++++++++++++++--- .../Manager/JsonRpcClientManager.php | 10 +++++ .../ConnectionConfigurationTest.php | 14 +++++- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/Meritoo/LimeSurvey/ApiClient/Configuration/ConnectionConfiguration.php b/src/Meritoo/LimeSurvey/ApiClient/Configuration/ConnectionConfiguration.php index 0087f26..04227be 100644 --- a/src/Meritoo/LimeSurvey/ApiClient/Configuration/ConnectionConfiguration.php +++ b/src/Meritoo/LimeSurvey/ApiClient/Configuration/ConnectionConfiguration.php @@ -59,15 +59,26 @@ class ConnectionConfiguration */ private $debugMode = false; + /** + * If is set to true, the SSL certificate verification is turned on. Otherwise - turned off. + * It's useful while running application with custom, non-official SSL certificate, e.g. while development process. + * + * @var bool + */ + private $verifySslCertificate = true; + /** * Class constructor * - * @param string $baseUrl Base url. Protocol & domain. - * @param string $username Name of user used to authenticate to LimeSurvey - * @param string $password Password used to authenticate to LimeSurvey - * @param bool $debugMode (optional) If is set to true, the "debug" mode is turned on. Otherwise - turned off. + * @param string $baseUrl Base url. Protocol & domain. + * @param string $username Name of user used to authenticate to LimeSurvey + * @param string $password Password used to authenticate to LimeSurvey + * @param bool $debugMode (optional) If is set to true, the "debug" mode is turned on. Otherwise - + * turned off. + * @param bool $verifySslCertificate (optional) If is set to true, the SSL certificate verification is turned + * on. Otherwise - turned off. */ - public function __construct($baseUrl, $username, $password, $debugMode = false) + public function __construct($baseUrl, $username, $password, $debugMode = false, $verifySslCertificate = true) { $this ->setBaseUrl($baseUrl) @@ -201,6 +212,30 @@ class ConnectionConfiguration return $this; } + /** + * Returns information if the SSL certificate verification is turned on + * + * @return bool + */ + public function isVerifySslCertificateOn() + { + return $this->verifySslCertificate; + } + + /** + * Sets information if the SSL certificate verification is turned on + * + * @param bool $verifySslCertificate (optional) If is set to true, the SSL certificate verification is turned on. + * Otherwise - turned off. + * @return $this + */ + public function setVerifySslCertificate($verifySslCertificate = true) + { + $this->verifySslCertificate = $verifySslCertificate; + + return $this; + } + /* * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * Additional / extra methods (neither getters, nor setters) diff --git a/src/Meritoo/LimeSurvey/ApiClient/Manager/JsonRpcClientManager.php b/src/Meritoo/LimeSurvey/ApiClient/Manager/JsonRpcClientManager.php index 26e1417..c41b8d2 100644 --- a/src/Meritoo/LimeSurvey/ApiClient/Manager/JsonRpcClientManager.php +++ b/src/Meritoo/LimeSurvey/ApiClient/Manager/JsonRpcClientManager.php @@ -90,6 +90,16 @@ class JsonRpcClientManager ->getHttpClient() ->withDebug(); } + + /* + * The SSL certificate verification is turned off? + */ + if (!$this->connectionConfiguration->isVerifySslCertificateOn()) { + $this + ->rpcClient + ->getHttpClient() + ->withoutSslVerification(); + } } return $this->rpcClient; diff --git a/tests/Meritoo/LimeSurvey/Test/ApiClient/Configuration/ConnectionConfigurationTest.php b/tests/Meritoo/LimeSurvey/Test/ApiClient/Configuration/ConnectionConfigurationTest.php index 54ec1f2..99dffc0 100644 --- a/tests/Meritoo/LimeSurvey/Test/ApiClient/Configuration/ConnectionConfigurationTest.php +++ b/tests/Meritoo/LimeSurvey/Test/ApiClient/Configuration/ConnectionConfigurationTest.php @@ -31,7 +31,7 @@ class ConnectionConfigurationTest extends BaseTestCase public function testConstructorVisibilityAndArguments() { - static::assertConstructorVisibilityAndArguments(ConnectionConfiguration::class, OopVisibilityType::IS_PUBLIC, 4, 3); + static::assertConstructorVisibilityAndArguments(ConnectionConfiguration::class, OopVisibilityType::IS_PUBLIC, 5, 3); } /** @@ -101,6 +101,18 @@ class ConnectionConfigurationTest extends BaseTestCase static::assertTrue($this->simpleConfiguration->isDebugModeOn()); } + public function testSetVerifySslCertificate() + { + $this->simpleConfiguration->setVerifySslCertificate(); + static::assertTrue($this->simpleConfiguration->isVerifySslCertificateOn()); + + $this->simpleConfiguration->setVerifySslCertificate(false); + static::assertFalse($this->simpleConfiguration->isVerifySslCertificateOn()); + + $this->simpleConfiguration->setVerifySslCertificate(true); + static::assertTrue($this->simpleConfiguration->isVerifySslCertificateOn()); + } + public function testGetFullUrl() { $this->simpleConfiguration->setRemoteControlUrl('lorem/ipsum');