Skip to content

Commit

Permalink
Merge pull request #151 from phar-io/fix-for-130-and-147
Browse files Browse the repository at this point in the history
Fixes #130 / #147
  • Loading branch information
theseer authored Jun 26, 2018
2 parents fa21963 + 0484596 commit dc159c0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function($class) {
'phario\\phive\\phivexmlconfig' => '/shared/config/PhiveXmlConfig.php',
'phario\\phive\\phivexmlconfigfilelocator' => '/shared/config/PhiveXmlConfigFileLocator.php',
'phario\\phive\\publickey' => '/services/key/PublicKey.php',
'phario\\phive\\publickeyexception' => '/shared/exceptions/PublicKeyException.php',
'phario\\phive\\purgecommand' => '/commands/purge/PurgeCommand.php',
'phario\\phive\\purgecontext' => '/commands/purge/PurgeContext.php',
'phario\\phive\\ratelimit' => '/shared/http/RateLimit.php',
Expand Down
11 changes: 11 additions & 0 deletions src/services/key/PublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class PublicKey {
* @param string $id
* @param string $public
* @param string $info
*
* @throws PublicKeyException
*/
public function __construct($id, $info, $public) {
$this->id = $id;
Expand Down Expand Up @@ -84,6 +86,9 @@ public function getFingerprint() {
return str_replace(' ', '', $this->fingerprint);
}

/**
* @throws PublicKeyException
*/
private function parseInfo($info) {
foreach (explode("\n", $info) as $line) {
$parts = explode(':', $line);
Expand All @@ -107,6 +112,12 @@ private function parseInfo($info) {
}
}
}

if (empty($this->uids) || $this->fingerprint === NULL || $this->bits === NULL || $this->created === NULL) {
throw new PublicKeyException(
sprintf('Failed to parse provided key info: %s', $info)
);
}
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/services/key/gpg/GnupgKeyDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public function download($keyId) {

$this->output->writeInfo('Successfully downloaded key');

return new PublicKey($keyId, $keyInfo->getBody(), $publicKey->getBody());
try {
return new PublicKey($keyId, $keyInfo->getBody(), $publicKey->getBody());
} catch (PublicKeyException $e) {
throw new DownloadFailedException($e->getMessage(), $e->getCode(), $e);
}
}
}
throw new DownloadFailedException(sprintf('PublicKey %s not found on key servers', $keyId));
Expand Down
6 changes: 6 additions & 0 deletions src/shared/exceptions/PublicKeyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace PharIo\Phive;

class PublicKeyException extends \Exception implements Exception {

}
15 changes: 10 additions & 5 deletions tests/unit/services/key/gpg/GnupgKeyDownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public function setUp() {
}

public function testInvokesCurlWithExpectedParams() {
$keyinfo = 'uid:Sebastian Bergmann <sebastian@php.net>:1405755775::' . "\n";
$keyinfo .= 'pub:D8406D0D82947747293778314AA394086372C20A:1:4096:1405754086::';

$response = $this->prophesize(HttpResponse::class);
$response->getHttpCode()->willReturn(200);
$response->getBody()->willReturn('Some PublicKey');
$response->getBody()->willReturn($keyinfo);
$response->isNotFound()->willReturn(false);

$this->curl->get(
Expand All @@ -44,10 +47,13 @@ public function testInvokesCurlWithExpectedParams() {
}

public function testReturnsExpectedKey() {
$keyinfo = 'uid:Sebastian Bergmann <sebastian@php.net>:1405755775::' . "\n";
$keyinfo .= 'pub:D8406D0D82947747293778314AA394086372C20A:1:4096:1405754086::';

$response = $this->prophesize(HttpResponse::class);
$response->getHttpCode()->willReturn(200);
$response->getBody()->willReturn('Some Key Info');
$response->getBody()->willReturn('Some Public Key Data');
$response->getBody()->willReturn($keyinfo);
//$response->getBody()->willReturn('Some Public Key Data');
$response->isNotFound()->willReturn(false);

$this->curl->get(Argument::any())
Expand All @@ -57,8 +63,7 @@ public function testReturnsExpectedKey() {
$this->curl->reveal(), ['example.com'], $this->output->reveal()
);

$key = new PublicKey('12345678', 'Some Key Info', 'Some Public Key Data');
$this->assertEquals($key, $downloader->download('12345678'));
$this->assertInstanceOf(PublicKey::class, $downloader->download('12345678'));
}

public function testThrowsExceptionIfKeyWasNotFound() {
Expand Down

0 comments on commit dc159c0

Please sign in to comment.