diff --git a/src/autoload.php b/src/autoload.php index 1ca06af3..0dc3882c 100644 --- a/src/autoload.php +++ b/src/autoload.php @@ -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', diff --git a/src/services/key/PublicKey.php b/src/services/key/PublicKey.php index 17dd07b8..cf4ce415 100644 --- a/src/services/key/PublicKey.php +++ b/src/services/key/PublicKey.php @@ -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; @@ -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); @@ -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) + ); + } } /** diff --git a/src/services/key/gpg/GnupgKeyDownloader.php b/src/services/key/gpg/GnupgKeyDownloader.php index c3aecd2a..c16f2a20 100644 --- a/src/services/key/gpg/GnupgKeyDownloader.php +++ b/src/services/key/gpg/GnupgKeyDownloader.php @@ -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)); diff --git a/src/shared/exceptions/PublicKeyException.php b/src/shared/exceptions/PublicKeyException.php new file mode 100644 index 00000000..53337caa --- /dev/null +++ b/src/shared/exceptions/PublicKeyException.php @@ -0,0 +1,6 @@ +: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( @@ -44,10 +47,13 @@ public function testInvokesCurlWithExpectedParams() { } public function testReturnsExpectedKey() { + $keyinfo = 'uid:Sebastian Bergmann :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()) @@ -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() {