Skip to content

Commit

Permalink
Merge pull request #209 from misantron/issue-207-patch
Browse files Browse the repository at this point in the history
Add content check after key file reading
  • Loading branch information
lcobucci authored Jul 29, 2018
2 parents ac083fa + 8e80bec commit 01da822
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/Signer/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

namespace Lcobucci\JWT\Signer;

use Exception;
use InvalidArgumentException;
use SplFileObject;

/**
* @author Luís Otávio Cobucci Oblonczyk <[email protected]>
Expand Down Expand Up @@ -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);
}

/**
Expand Down
19 changes: 18 additions & 1 deletion test/unit/Signer/KeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public function configureRootDir()
vfsStream::setup(
'root',
null,
['test.pem' => 'testing']
[
'test.pem' => 'testing',
'emptyFolder' => []
]
);
}

Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 01da822

Please sign in to comment.