Skip to content

Commit

Permalink
Begin v2 fork. See #17
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Apr 9, 2018
1 parent d0f22c0 commit e5faace
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 22 deletions.
4 changes: 2 additions & 2 deletions docs/features/LocalCACertBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use ParagonIE\Certainty\LocalCACertBuilder;
use ParagonIE\Certainty\RemoteFetch;
use ParagonIE\ConstantTime\Hex;

$latest = (new RemoteFetch())->getLatestBundle();
$latest = (new RemoteFetch('/path/to/certainty/data'))->getLatestBundle();

LocalCACertBuilder::fromBundle($latest)
->setSigningKey(Hex::decode('your hex-encoded secret key goes here'))
Expand Down Expand Up @@ -90,7 +90,7 @@ use ParagonIE\Certainty\LocalCACertBuilder;
use ParagonIE\Certainty\RemoteFetch;
use ParagonIE\ConstantTime\Hex;

$latest = (new RemoteFetch())->getLatestBundle();
$latest = (new RemoteFetch('/path/to/certainty/data'))->getLatestBundle();

/* This snippet is mostly identical from the previous one. */
LocalCACertBuilder::fromBundle($latest)
Expand Down
12 changes: 6 additions & 6 deletions docs/features/RemoteFetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Using the `RemoteFetch` class is rather straightforward.
<?php
use ParagonIE\Certainty\RemoteFetch;

$fetcher = new RemoteFetch();
$fetcher = new RemoteFetch('/path/to/certainty/data');
$latestCACertBundle = $fetcher->getLatestBundle();

$ch = curl_init();
Expand All @@ -28,7 +28,7 @@ curl_setopt($ch, CURLOPT_CAINFO, $latestCACertBundle->getFilePath());
use ParagonIE\Certainty\RemoteFetch;
use GuzzleHttp\Client;

$fetcher = new RemoteFetch();
$fetcher = new RemoteFetch('/path/to/certainty/data');
$latestCACertBundle = $fetcher->getLatestBundle();
$client = new Client();

Expand All @@ -43,7 +43,7 @@ $response = $client->request('POST', '/url', [
<?php
use ParagonIE\Certainty\RemoteFetch;

$fetcher = new RemoteFetch();
$fetcher = new RemoteFetch('/path/to/certainty/data');
$latestCACertBundle = $fetcher->getLatestBundle();

$context = stream_context_create([
Expand Down Expand Up @@ -93,12 +93,12 @@ object has been created.
use ParagonIE\Certainty\RemoteFetch;

// Cleaner.
$fetcher = (new RemoteFetch())
$fetcher = (new RemoteFetch('/path/to/certainty/data'))
->setCacheTimeout(new \DateInterval('PT06H'));

// Alternatively, the constructor approach:
$fetcher = new RemoteFetch(
'', // use the default save path
'/path/to/certainty/data',
RemoteFetch::DEFAULT_URL,
null, // automatically selects/configures Guzzle
new \DateInterval('PT06H') // 6 hours
Expand All @@ -117,7 +117,7 @@ Certainty supports this usage.
<?php
use ParagonIE\Certainty\RemoteFetch;

$latest = (new RemoteFetch())->getLatestBundle();
$latest = (new RemoteFetch('/path/to/certainty/data'))->getLatestBundle();

$latest->createSymlink('/path/to/cacert.pem', true);
```
Expand Down
8 changes: 3 additions & 5 deletions src/Certainty.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ class Certainty
*/
public static function getGuzzleClient(Fetch $fetch = null)
{
if (\is_null($fetch)) {
$fetch = new Fetch();
$options = [];
if (!\is_null($fetch)) {
$options['verify'] = $fetch->getLatestBundle()->getFilePath();
}
$options = [
'verify' => $fetch->getLatestBundle()->getFilePath()
];

if (\defined('CURLOPT_SSLVERSION') && \defined('CURL_SSLVERSION_TLSv1_2') && \defined('CURL_SSLVERSION_TLSv1')) {
// https://github.com/curl/curl/blob/6aa86c493bd77b70d1f5018e102bc3094290d588/include/curl/curl.h#L1927
Expand Down
11 changes: 6 additions & 5 deletions src/Fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ class Fetch
* You almost certainly want to use RemoteFetch instead.
*
* @param string $dataDir Where the certificates and configuration lives
*
* @throws FilesystemException
*/
public function __construct($dataDir = '')
public function __construct($dataDir)
{
if (!empty($dataDir) && \is_readable($dataDir)) {
$this->dataDirectory = $dataDir;
} else {
$this->dataDirectory = \dirname(__DIR__) . '/data';
if (!\is_readable($dataDir)) {
throw new FilesystemException('Directory is not readable: ' . $dataDir);
}
$this->dataDirectory = $dataDir;
}

/**
Expand Down
10 changes: 8 additions & 2 deletions test/BundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@

class BundleTest extends TestCase
{
/**
* @var string
*/
protected $defaultDir;

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

public function setUp()
{
$this->defaultDir = dirname(__DIR__) . '/data';
$this->link = __DIR__ . '/static/symlink-test';
}

Expand All @@ -37,7 +43,7 @@ public function testCreateSymlink()
return;
}

$latest = (new Fetch())->getLatestBundle();
$latest = (new Fetch($this->defaultDir))->getLatestBundle();

$latest->createSymlink($this->link, true);

Expand All @@ -55,7 +61,7 @@ public function testCreateSymlink()
*/
public function testGetters()
{
$latest = (new Fetch())->getLatestBundle();
$latest = (new Fetch($this->defaultDir))->getLatestBundle();
$this->assertTrue(\is_string($latest->getFilePath()));
$this->assertTrue(\is_string($latest->getSha256Sum()));
$this->assertTrue(\is_string($latest->getSignature()));
Expand Down
12 changes: 11 additions & 1 deletion test/CustomCASupportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
*/
class CustomCASupportTest extends TestCase
{
/**
* @var string
*/
protected $defaultDir;

public function setUp()
{
$this->defaultDir = dirname(__DIR__) . '/data';
}

public function tearDown()
{
\unlink(__DIR__ . '/static/combined.pem');
Expand All @@ -31,7 +41,7 @@ public function testCustom()
$validator = new CustomValidator();
$validator::setPublicKey(Hex::encode($publicKey));

$latest = (new Fetch())->getLatestBundle();
$latest = (new Fetch($this->defaultDir))->getLatestBundle();
LocalCACertBuilder::fromBundle($latest)
->setCustomValidator(CustomValidator::class)
->setOutputPemFile(__DIR__ . '/static/combined.pem')
Expand Down
8 changes: 7 additions & 1 deletion test/FetchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@

class FetchTest extends TestCase
{
/**
* @var string
*/
protected $defaultDir;

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

public function setUp()
{
$this->defaultDir = dirname(__DIR__) . '/data';
$this->root = __DIR__ . '/static/';
}

Expand Down Expand Up @@ -66,7 +72,7 @@ public function testLiveDataDir()
{
$this->assertInstanceOf(
Bundle::class,
(new Fetch())->getLatestBundle(),
(new Fetch($this->defaultDir))->getLatestBundle(),
'The live data directory has no valid signatures.'
);
}
Expand Down

0 comments on commit e5faace

Please sign in to comment.