Skip to content

Commit

Permalink
Remove in-repo certs, require ca-bundle dep and use it in http client
Browse files Browse the repository at this point in the history
Signed-off-by: Kristian Feldsam <feldsam@gmail.com>
  • Loading branch information
feldsam committed Jun 5, 2024
1 parent 8eb9a0d commit 9fd291f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 67 deletions.
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
"require": {
"php": "~7.4||~8.0",
"ext-curl": "*",
"guzzlehttp/guzzle": "~6.1|~7.0"
"guzzlehttp/guzzle": "~6.1|~7.0",
"composer/ca-bundle": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "9.5.10",
"squizlabs/php_codesniffer": "3.6.1"
},
"suggest": {
"composer/ca-bundle": "Provides regularly updated root certificates list"
},
"autoload": {
"psr-4": {
"FioApi\\": "src/FioApi"
Expand Down
43 changes: 11 additions & 32 deletions src/FioApi/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,34 @@

namespace FioApi;

use Composer\CaBundle\CaBundle;
use FioApi\Exceptions\InternalErrorException;
use FioApi\Exceptions\TooGreedyException;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;

class Downloader
{
/** @var UrlBuilder */
protected $urlBuilder;

/** @var \GuzzleHttp\Client */
/** @var Client */
protected $client;

/** @var string */
protected $certificatePath;

public function __construct(
string $token,
\GuzzleHttp\ClientInterface $client = null
) {
public function __construct(string $token, ClientInterface $client = null)
{
$this->urlBuilder = new UrlBuilder($token);
$this->client = $client;
}

public function setCertificatePath(string $path)
{
$this->certificatePath = $path;
}

public function getCertificatePath(): string
{
if ($this->certificatePath) {
return $this->certificatePath;
}

if (class_exists('\Composer\CaBundle\CaBundle')) {
return \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath();
} elseif (class_exists('\Kdyby\CurlCaBundle\CertificateHelper')) {
return \Kdyby\CurlCaBundle\CertificateHelper::getCaInfoFile();
}

//Key downloaded from https://www.geotrust.com/resources/root-certificates/
return __DIR__ . '/keys/Geotrust_PCA_G3_Root.pem';
}

public function getClient(): ClientInterface
{
if (!$this->client) {
$this->client = new \GuzzleHttp\Client();
$this->client = new Client([
RequestOptions::VERIFY => CaBundle::getSystemCaRootBundlePath()
]);
}
return $this->client;
}
Expand Down Expand Up @@ -79,7 +58,7 @@ public function setLastId(string $id): void
$url = $this->urlBuilder->buildSetLastIdUrl($id);

try {
$client->request('get', $url, ['verify' => $this->getCertificatePath()]);
$client->request('get', $url);
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
$this->handleException($e);
}
Expand All @@ -91,7 +70,7 @@ private function downloadTransactionsList(string $url): TransactionList

try {
/** @var ResponseInterface $response */
$response = $client->request('get', $url, ['verify' => $this->getCertificatePath()]);
$response = $client->request('get', $url);
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
$this->handleException($e);
}
Expand Down
24 changes: 0 additions & 24 deletions src/FioApi/keys/Geotrust_PCA_G3_Root.pem

This file was deleted.

7 changes: 0 additions & 7 deletions tests/FioApi/DownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,4 @@ public function testDownloaderSetsLastId()

$this->assertSame('https://fioapi.fio.cz/v1/rest/set-last-id/validToken/123456/', (string) $request->getUri());
}

public function testDownloaderSetCertificatePath()
{
$downloader = new Downloader('validToken');
$downloader->setCertificatePath('foo.pem');
$this->assertSame('foo.pem', $downloader->getCertificatePath());
}
}

0 comments on commit 9fd291f

Please sign in to comment.