From 63d04fc09bfc8d6b96b52a89fb4ca55a82a645b2 Mon Sep 17 00:00:00 2001 From: miks Date: Thu, 16 May 2019 18:59:48 +0300 Subject: [PATCH] Add more verbose certificate check --- src/Gateway.php | 36 -------------------------- src/Requests/AbstractRequest.php | 22 +++++++++++++++- tests/Messages/AbstractRequestTest.php | 36 ++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 39 deletions(-) diff --git a/src/Gateway.php b/src/Gateway.php index 057e335..7cea138 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -80,10 +80,7 @@ public function getName(): string public function getDefaultParameters(): array { return [ - 'certificatePath' => '', - 'certificatePassword' => '', 'testMode' => false, - 'clientIP' => '', 'language' => 'EN' ]; } @@ -97,14 +94,6 @@ public function setCertificatePassword($value): self return $this->setParameter('certificatePassword', $value); } - /** - * @return string - */ - public function getCertificatePassword(): string - { - return $this->getParameter('certificatePassword'); - } - /** * @param string $value * @return $this @@ -114,14 +103,6 @@ public function setCertificatePath($value): self return $this->setParameter('certificatePath', $value); } - /** - * @return string - */ - public function getCertificatePath(): string - { - return $this->getParameter('certificatePath'); - } - /** * @return string */ @@ -139,23 +120,6 @@ public function setLanguage($value) return $this->setParameter('language', $value); } - /** - * @return string - */ - public function getClientIP(): string - { - return $this->getParameter('clientIP'); - } - - /** - * @param $value - * @return $this - */ - public function setClientIP($value) - { - return $this->setParameter('clientIP', $value); - } - /** * Execute SMS transaction * @param array $options diff --git a/src/Requests/AbstractRequest.php b/src/Requests/AbstractRequest.php index f66320d..84957c6 100644 --- a/src/Requests/AbstractRequest.php +++ b/src/Requests/AbstractRequest.php @@ -8,6 +8,7 @@ namespace Omnipay\FirstDataLatvia\Requests; use Omnipay\Common\Message\AbstractRequest as CommonAbstractRequest; +use Omnipay\Common\Exception\RuntimeException; use Omnipay\FirstDataLatvia\Gateway; use Omnipay\FirstDataLatvia\Responses\AbstractResponse; use Omnipay\Common\Http\ClientInterface; @@ -103,7 +104,7 @@ protected function getServerEndpoint(): string */ public function sendData($data): AbstractResponse { - $this->validate('certificatePath', 'certificatePassword'); + $this->validateCertificate(); $requestMethod = 'POST'; $requestUrl = $this->getServerEndpoint(); @@ -127,6 +128,25 @@ public function sendData($data): AbstractResponse return $responseObj; } + /** + * @throws \Omnipay\Common\Exception\RuntimeException + */ + protected function validateCertificate() + { + $this->validate('certificatePath', 'certificatePassword'); + + # check if certificate exists + if (!file_exists($this->getCertificatePath())) { + throw new RuntimeException('Unexisting certificate ' . $this->getCertificatePath()); + } + + // test certificate parse with supplied password + $pkeyid = openssl_pkey_get_private('file://' . $this->getCertificatePath(), $this->getCertificatePassword()); + if (!$pkeyid) { + throw new RuntimeException('Unable to load certificate ' . $this->getCertificatePath()); + } + } + /** * @param $response * @return array diff --git a/tests/Messages/AbstractRequestTest.php b/tests/Messages/AbstractRequestTest.php index 1c1e69c..501f4e3 100644 --- a/tests/Messages/AbstractRequestTest.php +++ b/tests/Messages/AbstractRequestTest.php @@ -57,8 +57,8 @@ public function testSendDataValidateCertificatePassword() public function testSendData() { - $this->request->setCertificatePath("x"); - $this->request->setCertificatePassword("y"); + $this->request->setCertificatePath('tests/Fixtures/keystore.pem'); + $this->request->setCertificatePassword('XXXX'); $this->request->setTestMode(false); // send request to firstdata @@ -82,6 +82,38 @@ public function testSendData() $this->assertSame($postData, $sentPostData); } + public function testSendDataMissingConfiguration() + { + $this->expectException(\Omnipay\Common\Exception\InvalidRequestException::class); + $this->expectExceptionMessage('The certificatePath parameter is required'); + + $this->request->sendData(array('some_data' => 'x')); + } + + public function testSendDatatUnexistingCertificate() + { + // initiate new unconfigured gateway + $this->request->setCertificatePath('tests/Fixtures/unexisting.pem'); + $this->request->setCertificatePassword('XXXX'); + + $this->expectException(\Omnipay\Common\Exception\RuntimeException::class); + $this->expectExceptionMessage('Unexisting certificate tests/Fixtures/unexisting.pem'); + + $this->request->sendData(array('some_data' => 'x')); + } + + public function testSendDataInvalidCertificate() + { + $this->request->setCertificatePath('tests/Fixtures/keystore.pem'); + // set wrong password + $this->request->setCertificatePassword('wrong-password'); + + $this->expectException(\Omnipay\Common\Exception\RuntimeException::class); + $this->expectExceptionMessage('Unable to load certificate tests/Fixtures/keystore.pem'); + + $this->request->sendData(array('some_data' => 'x')); + } + public function testParseResponse() { $this->assertEquals(array("asd" => "1", "xccx" => "3"), $this->request::parseResponse("asd: 1\nxccx: 3\n"));