Skip to content

Commit

Permalink
add passphrase and key resource close
Browse files Browse the repository at this point in the history
  • Loading branch information
miladrahimi committed Nov 10, 2019
1 parent 22ba3a2 commit 381c819
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 46 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ RSA algorithms are asymmetric. A paired key is needed to sign and verify tokens.
```php
use MiladRahimi\Jwt\Cryptography\Algorithms\Rsa\RS256Signer;
use MiladRahimi\Jwt\Cryptography\Algorithms\Rsa\RS256Verifier;
use MiladRahimi\Jwt\Cryptography\Keys\PrivateKey;
use MiladRahimi\Jwt\Cryptography\Keys\PublicKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPrivateKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPublicKey;
use MiladRahimi\Jwt\Generator;
use MiladRahimi\Jwt\Parser;

$privateKey = new PrivateKey('/path/to/private.pem');
$publicKey = new PublicKey('/path/to/public.pem');
$privateKey = new RsaPrivateKey('/path/to/private.pem');
$publicKey = new RsaPublicKey('/path/to/public.pem');

$signer = new RS256Signer($privateKey);
$verifier = new RS256Verifier($publicKey);
Expand Down
18 changes: 10 additions & 8 deletions src/Cryptography/Algorithms/Rsa/AbstractRsaSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MiladRahimi\Jwt\Cryptography\Algorithms\Rsa;

use MiladRahimi\Jwt\Cryptography\Keys\PrivateKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPrivateKey;
use MiladRahimi\Jwt\Cryptography\Signer;
use MiladRahimi\Jwt\Exceptions\SigningException;

Expand All @@ -16,16 +16,16 @@ abstract class AbstractRsaSigner implements Signer
use Naming;

/**
* @var PrivateKey
* @var RsaPrivateKey
*/
protected $privateKey;

/**
* AbstractRsaSigner constructor.
*
* @param PrivateKey $publicKey
* @param RsaPrivateKey $publicKey
*/
public function __construct(PrivateKey $publicKey)
public function __construct(RsaPrivateKey $publicKey)
{
$this->setPrivateKey($publicKey);
}
Expand All @@ -38,24 +38,26 @@ public function sign(string $message): string
$signature = '';

if (openssl_sign($message, $signature, $this->privateKey->getResource(), $this->algorithm()) === true) {
$this->privateKey->close();

return $signature;
}

throw new SigningException();
}

/**
* @return PrivateKey
* @return RsaPrivateKey
*/
public function getPrivateKey(): PrivateKey
public function getPrivateKey(): RsaPrivateKey
{
return $this->privateKey;
}

/**
* @param PrivateKey $privateKey
* @param RsaPrivateKey $privateKey
*/
public function setPrivateKey(PrivateKey $privateKey)
public function setPrivateKey(RsaPrivateKey $privateKey)
{
$this->privateKey = $privateKey;
}
Expand Down
18 changes: 10 additions & 8 deletions src/Cryptography/Algorithms/Rsa/AbstractRsaVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MiladRahimi\Jwt\Cryptography\Algorithms\Rsa;

use MiladRahimi\Jwt\Cryptography\Keys\PublicKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPublicKey;
use MiladRahimi\Jwt\Cryptography\Verifier;
use MiladRahimi\Jwt\Exceptions\InvalidSignatureException;

Expand All @@ -16,16 +16,16 @@ abstract class AbstractRsaVerifier implements Verifier
use Naming;

/**
* @var PublicKey
* @var RsaPublicKey
*/
protected $publicKey;

/**
* AbstractRsaVerifier constructor.
*
* @param PublicKey $key
* @param RsaPublicKey $key
*/
public function __construct(PublicKey $key)
public function __construct(RsaPublicKey $key)
{
$this->setPublicKey($key);
}
Expand All @@ -38,20 +38,22 @@ public function verify(string $plain, string $signature)
if (openssl_verify($plain, $signature, $this->publicKey->getResource(), $this->algorithm()) !== 1) {
throw new InvalidSignatureException();
}

$this->publicKey->close();
}

/**
* @return PublicKey
* @return RsaPublicKey
*/
public function getPublicKey(): PublicKey
public function getPublicKey(): RsaPublicKey
{
return $this->publicKey;
}

/**
* @param PublicKey $publicKey
* @param RsaPublicKey $publicKey
*/
public function setPublicKey(PublicKey $publicKey)
public function setPublicKey(RsaPublicKey $publicKey)
{
$this->publicKey = $publicKey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use MiladRahimi\Jwt\Exceptions\InvalidKeyException;

/**
* Class PrivateKey
* Class RsaPrivateKey
*
* @package MiladRahimi\Jwt\Cryptography\Keys
*/
class PrivateKey
class RsaPrivateKey
{
/**
* @var resource Key file resource handler
Expand All @@ -19,12 +19,13 @@ class PrivateKey
/**
* PrivateKey constructor.
*
* @param string $fileFullPath
* @param string $filePath
* @param string $passphrase
* @throws InvalidKeyException
*/
public function __construct(string $fileFullPath)
public function __construct(string $filePath, $passphrase = '')
{
$this->resource = openssl_pkey_get_private('file:///' . $fileFullPath);
$this->resource = openssl_pkey_get_private('file:///' . $filePath, $passphrase);

if (empty($this->resource)) {
throw new InvalidKeyException();
Expand All @@ -38,4 +39,12 @@ public function getResource()
{
return $this->resource;
}

/**
* Close key resource
*/
public function close()
{
openssl_free_key($this->getResource());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use MiladRahimi\Jwt\Exceptions\InvalidKeyException;

/**
* Class PublicKey
* Class RsaPublicKey
*
* @package MiladRahimi\Jwt\Cryptography\Keys
*/
class PublicKey
class RsaPublicKey
{
/**
* @var resource Key file resource handler
Expand All @@ -19,12 +19,12 @@ class PublicKey
/**
* PublicKey constructor.
*
* @param string $fileFullPath
* @param string $filePath
* @throws InvalidKeyException
*/
public function __construct(string $fileFullPath)
public function __construct(string $filePath)
{
$this->resource = openssl_pkey_get_public('file:///' . $fileFullPath);
$this->resource = openssl_pkey_get_public('file:///' . $filePath);

if (empty($this->resource)) {
throw new InvalidKeyException();
Expand All @@ -38,4 +38,12 @@ public function getResource()
{
return $this->resource;
}

/**
* Close key resource
*/
public function close()
{
openssl_free_key($this->getResource());
}
}
6 changes: 3 additions & 3 deletions tests/Cryptography/Keys/PrivateKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MiladRahimi\Jwt\Tests\Cryptography\Keys;

use MiladRahimi\Jwt\Cryptography\Keys\PrivateKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPrivateKey;
use MiladRahimi\Jwt\Exceptions\InvalidKeyException;
use MiladRahimi\Jwt\Tests\TestCase;
use Throwable;
Expand All @@ -14,7 +14,7 @@ class PrivateKeyTest extends TestCase
*/
public function test_with_valid_key_it_should_pass()
{
$key = new PrivateKey(__DIR__ . '/../../../resources/test/keys/rsa-private.pem');
$key = new RsaPrivateKey(__DIR__ . '/../../../resources/test/keys/rsa-private.pem');
$this->assertNotNull($key->getResource());
}

Expand All @@ -24,6 +24,6 @@ public function test_with_valid_key_it_should_pass()
public function test_with_invalid_key_it_should_fail()
{
$this->expectException(InvalidKeyException::class);
new PrivateKey('Invalid Key!');
new RsaPrivateKey('Invalid Key!');
}
}
6 changes: 3 additions & 3 deletions tests/Cryptography/Keys/PublicKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MiladRahimi\Jwt\Tests\Cryptography\Keys;

use MiladRahimi\Jwt\Cryptography\Keys\PublicKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPublicKey;
use MiladRahimi\Jwt\Exceptions\InvalidKeyException;
use MiladRahimi\Jwt\Tests\TestCase;
use Throwable;
Expand All @@ -14,7 +14,7 @@ class PublicKeyTest extends TestCase
*/
public function test_with_valid_key_it_should_pass()
{
$key = new PublicKey(__DIR__ . '/../../../resources/test/keys/rsa-public.pem');
$key = new RsaPublicKey(__DIR__ . '/../../../resources/test/keys/rsa-public.pem');
$this->assertNotNull($key->getResource());
}

Expand All @@ -24,6 +24,6 @@ public function test_with_valid_key_it_should_pass()
public function test_with_invalid_key_it_should_fail()
{
$this->expectException(InvalidKeyException::class);
new PublicKey('Invalid Key!');
new RsaPublicKey('Invalid Key!');
}
}
8 changes: 4 additions & 4 deletions tests/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use MiladRahimi\Jwt\Cryptography\Algorithms\Hmac\HS256;
use MiladRahimi\Jwt\Cryptography\Algorithms\Rsa\RS256Signer;
use MiladRahimi\Jwt\Cryptography\Algorithms\Rsa\RS256Verifier;
use MiladRahimi\Jwt\Cryptography\Keys\PrivateKey;
use MiladRahimi\Jwt\Cryptography\Keys\PublicKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPrivateKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPublicKey;
use MiladRahimi\Jwt\Generator;
use MiladRahimi\Jwt\Parser;
use MiladRahimi\Jwt\Validator\DefaultValidator;
Expand Down Expand Up @@ -39,8 +39,8 @@ public function test_simple_example()
*/
public function test_rsa_algorithms()
{
$privateKey = new PrivateKey(__DIR__ . '/../resources/test/keys/rsa-private.pem');
$publicKey = new PublicKey(__DIR__ . '/../resources/test/keys/rsa-public.pem');
$privateKey = new RsaPrivateKey(__DIR__ . '/../resources/test/keys/rsa-private.pem');
$publicKey = new RsaPublicKey(__DIR__ . '/../resources/test/keys/rsa-public.pem');

$signer = new RS256Signer($privateKey);
$verifier = new RS256Verifier($publicKey);
Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace MiladRahimi\Jwt\Tests;

use MiladRahimi\Jwt\Cryptography\Algorithms\Hmac\HS256;
use MiladRahimi\Jwt\Cryptography\Keys\PrivateKey;
use MiladRahimi\Jwt\Cryptography\Keys\PublicKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPrivateKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPublicKey;
use MiladRahimi\Jwt\Cryptography\Signer;
use MiladRahimi\Jwt\Cryptography\Verifier;
use Throwable;
Expand All @@ -17,12 +17,12 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
protected $key = '12345678901234567890123456789012';

/**
* @var PrivateKey
* @var RsaPrivateKey
*/
protected $privateKey;

/**
* @var PublicKey
* @var RsaPublicKey
*/
protected $publicKey;

Expand Down Expand Up @@ -53,9 +53,9 @@ public function setUp()
{
parent::setUp();

$this->privateKey = new PrivateKey(__DIR__ . '/../resources/test/keys/rsa-private.pem');
$this->privateKey = new RsaPrivateKey(__DIR__ . '/../resources/test/keys/rsa-private.pem');

$this->publicKey = new PublicKey(__DIR__ . '/../resources/test/keys/rsa-public.pem');
$this->publicKey = new RsaPublicKey(__DIR__ . '/../resources/test/keys/rsa-public.pem');

$this->signer = $this->verifier = new HS256($this->key);

Expand Down

0 comments on commit 381c819

Please sign in to comment.