diff --git a/src/Signer/Key.php b/src/Signer/Key.php index 8a97e409..7b727aff 100644 --- a/src/Signer/Key.php +++ b/src/Signer/Key.php @@ -7,7 +7,9 @@ namespace Lcobucci\JWT\Signer; +use Exception; use InvalidArgumentException; +use SplFileObject; /** * @author Luís Otávio Cobucci Oblonczyk @@ -54,17 +56,17 @@ private function setContent($content) * * @return string * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ private function readFile($content) { - $file = substr($content, 7); + try { + $file = new SplFileObject(substr($content, 7)); - if (!is_readable($file)) { - throw new \InvalidArgumentException('You must inform a valid key file'); + return $file->fread($file->getSize()); + } catch (Exception $exception) { + throw new InvalidArgumentException('You must inform a valid key file', 0, $exception); } - - return file_get_contents($file); } /** diff --git a/test/unit/Signer/KeyTest.php b/test/unit/Signer/KeyTest.php index 0f75589d..58dd8e82 100644 --- a/test/unit/Signer/KeyTest.php +++ b/test/unit/Signer/KeyTest.php @@ -23,7 +23,10 @@ public function configureRootDir() vfsStream::setup( 'root', null, - ['test.pem' => 'testing'] + [ + 'test.pem' => 'testing', + 'emptyFolder' => [] + ] ); } @@ -70,6 +73,20 @@ public function constructShouldRaiseExceptionWhenFileDoesNotExists() new Key('file://' . vfsStream::url('root/test2.pem')); } + /** + * @test + * + * @expectedException \InvalidArgumentException + * + * @covers Lcobucci\JWT\Signer\Key::__construct + * @covers Lcobucci\JWT\Signer\Key::setContent + * @covers Lcobucci\JWT\Signer\Key::readFile + */ + public function constructShouldRaiseExceptionWhenFileGetContentsFailed() + { + new Key('file://' . vfsStream::url('root/emptyFolder')); + } + /** * @test *