updated-packages
This commit is contained in:
@@ -13,7 +13,7 @@ use Lcobucci\JWT\Signature;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class BaseSignerTest extends \PHPUnit_Framework_TestCase
|
||||
class BaseSignerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var BaseSigner|\PHPUnit_Framework_MockObject_MockObject
|
||||
|
||||
@@ -1,178 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface;
|
||||
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
|
||||
use Mdanter\Ecc\Math\MathAdapterInterface;
|
||||
use Mdanter\Ecc\Serializer\PrivateKey\PrivateKeySerializerInterface;
|
||||
use Mdanter\Ecc\Serializer\PublicKey\PublicKeySerializerInterface;
|
||||
use Lcobucci\JWT\Signer\Key;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 3.0.4
|
||||
*/
|
||||
class KeyParserTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var MathAdapterInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $adapter;
|
||||
|
||||
/**
|
||||
* @var PrivateKeySerializerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $privateKeySerializer;
|
||||
|
||||
/**
|
||||
* @var PublicKeySerializerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $publicKeySerializer;
|
||||
|
||||
/**
|
||||
* @before
|
||||
*/
|
||||
public function createDependencies()
|
||||
{
|
||||
$this->adapter = $this->getMock(MathAdapterInterface::class);
|
||||
$this->privateKeySerializer = $this->getMock(PrivateKeySerializerInterface::class);
|
||||
$this->publicKeySerializer = $this->getMock(PublicKeySerializerInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
|
||||
*/
|
||||
public function constructShouldConfigureDependencies()
|
||||
{
|
||||
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
|
||||
|
||||
$this->assertAttributeSame($this->privateKeySerializer, 'privateKeySerializer', $parser);
|
||||
$this->assertAttributeSame($this->publicKeySerializer, 'publicKeySerializer', $parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPrivateKey
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
|
||||
*/
|
||||
public function getPrivateKeyShouldAskSerializerToParseTheKey()
|
||||
{
|
||||
$privateKey = $this->getMock(PrivateKeyInterface::class);
|
||||
|
||||
$keyContent = 'MHcCAQEEIBGpMoZJ64MMSzuo5JbmXpf9V4qSWdLIl/8RmJLcfn/qoAoGC'
|
||||
. 'CqGSM49AwEHoUQDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpnd0wxa2iF'
|
||||
. 'ruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==';
|
||||
|
||||
$this->privateKeySerializer->expects($this->once())
|
||||
->method('parse')
|
||||
->with($keyContent)
|
||||
->willReturn($privateKey);
|
||||
|
||||
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
|
||||
$this->assertSame($privateKey, $parser->getPrivateKey($this->getPrivateKey()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPrivateKey
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
|
||||
*/
|
||||
public function getPrivateKeyShouldRaiseExceptionWhenAWrongKeyWasGiven()
|
||||
{
|
||||
$this->privateKeySerializer->expects($this->never())
|
||||
->method('parse');
|
||||
|
||||
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
|
||||
$parser->getPrivateKey($this->getPublicKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPublicKey
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
|
||||
*/
|
||||
public function getPublicKeyShouldAskSerializerToParseTheKey()
|
||||
{
|
||||
$publicKey = $this->getMock(PublicKeyInterface::class);
|
||||
|
||||
$keyContent = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpn'
|
||||
. 'd0wxa2iFruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==';
|
||||
|
||||
$this->publicKeySerializer->expects($this->once())
|
||||
->method('parse')
|
||||
->with($keyContent)
|
||||
->willReturn($publicKey);
|
||||
|
||||
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
|
||||
$this->assertSame($publicKey, $parser->getPublicKey($this->getPublicKey()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPublicKey
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
|
||||
*/
|
||||
public function getPublicKeyShouldRaiseExceptionWhenAWrongKeyWasGiven()
|
||||
{
|
||||
$this->publicKeySerializer->expects($this->never())
|
||||
->method('parse');
|
||||
|
||||
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
|
||||
$parser->getPublicKey($this->getPrivateKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Key
|
||||
*/
|
||||
private function getPrivateKey()
|
||||
{
|
||||
return new Key(
|
||||
"-----BEGIN EC PRIVATE KEY-----\n"
|
||||
. "MHcCAQEEIBGpMoZJ64MMSzuo5JbmXpf9V4qSWdLIl/8RmJLcfn/qoAoGCCqGSM49\n"
|
||||
. "AwEHoUQDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpnd0wxa2iFruiI2tsEdGFTLTsy\n"
|
||||
. "U+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==\n"
|
||||
. "-----END EC PRIVATE KEY-----"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Key
|
||||
*/
|
||||
private function getPublicKey()
|
||||
{
|
||||
return new Key(
|
||||
"-----BEGIN PUBLIC KEY-----\n"
|
||||
. "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpn\n"
|
||||
. "d0wxa2iFruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==\n"
|
||||
. "-----END PUBLIC KEY-----"
|
||||
);
|
||||
}
|
||||
}
|
||||
127
vendor/lcobucci/jwt/test/unit/Signer/Ecdsa/MultibyteStringConverterTest.php
vendored
Normal file
127
vendor/lcobucci/jwt/test/unit/Signer/Ecdsa/MultibyteStringConverterTest.php
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use function bin2hex;
|
||||
use function hex2bin;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter
|
||||
*/
|
||||
final class MultibyteStringConverterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider pointsConversionData
|
||||
*
|
||||
* @covers ::toAsn1
|
||||
* @covers ::octetLength
|
||||
* @covers ::preparePositiveInteger
|
||||
*/
|
||||
public function toAsn1ShouldReturnThePointsInAnAsn1SequenceFormat(
|
||||
$r,
|
||||
$s,
|
||||
$asn1
|
||||
) {
|
||||
$converter = new MultibyteStringConverter();
|
||||
$message = hex2bin($r . $s);
|
||||
|
||||
self::assertSame($asn1, bin2hex($converter->toAsn1($message, strlen($r))));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers ::toAsn1
|
||||
* @covers ::octetLength
|
||||
*/
|
||||
public function toAsn1ShouldRaiseExceptionWhenPointsDoNotHaveCorrectLength()
|
||||
{
|
||||
$converter = new MultibyteStringConverter();
|
||||
|
||||
self::expectException(InvalidArgumentException::class);
|
||||
$converter->toAsn1('a very wrong string', 64);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider pointsConversionData
|
||||
*
|
||||
* @covers ::fromAsn1
|
||||
* @covers ::readAsn1Content
|
||||
* @covers ::readAsn1Integer
|
||||
* @covers ::retrievePositiveInteger
|
||||
*/
|
||||
public function fromAsn1ShouldReturnTheConcatenatedPoints($r, $s, $asn1)
|
||||
{
|
||||
$converter = new MultibyteStringConverter();
|
||||
$message = hex2bin($asn1);
|
||||
|
||||
self::assertSame($r . $s, bin2hex($converter->fromAsn1($message, strlen($r))));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function pointsConversionData()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'efd48b2aacb6a8fd1140dd9cd45e81d69d2c877b56aaf991c34d0ea84eaf3716',
|
||||
'f7cb1c942d657c41d436c7a1b6e29f65f3e900dbb9aff4064dc4ab2f843acda8',
|
||||
'3046022100efd48b2aacb6a8fd1140dd9cd45e81d69d2c877b56aaf991c34d0ea84eaf3716022100f7cb1c942d657c41d436c7'
|
||||
. 'a1b6e29f65f3e900dbb9aff4064dc4ab2f843acda8',
|
||||
],
|
||||
[
|
||||
'94edbb92a5ecb8aad4736e56c691916b3f88140666ce9fa73d64c4ea95ad133c81a648152e44acf96e36dd1e80fabe46',
|
||||
'99ef4aeb15f178cea1fe40db2603138f130e740a19624526203b6351d0a3a94fa329c145786e679e7b82c71a38628ac8',
|
||||
'306602310094edbb92a5ecb8aad4736e56c691916b3f88140666ce9fa73d64c4ea95ad133c81a648152e44acf96e36dd1e80fa'
|
||||
. 'be4602310099ef4aeb15f178cea1fe40db2603138f130e740a19624526203b6351d0a3a94fa329c145786e679e7b82c71a38'
|
||||
. '628ac8',
|
||||
],
|
||||
[
|
||||
'00c328fafcbd79dd77850370c46325d987cb525569fb63c5d3bc53950e6d4c5f174e25a1ee9017b5d450606add152b534931d7'
|
||||
. 'd4e8455cc91f9b15bf05ec36e377fa',
|
||||
'00617cce7cf5064806c467f678d3b4080d6f1cc50af26ca209417308281b68af282623eaa63e5b5c0723d8b8c37ff0777b1a20'
|
||||
. 'f8ccb1dccc43997f1ee0e44da4a67a',
|
||||
'308187024200c328fafcbd79dd77850370c46325d987cb525569fb63c5d3bc53950e6d4c5f174e25a1ee9017b5d450606add15'
|
||||
. '2b534931d7d4e8455cc91f9b15bf05ec36e377fa0241617cce7cf5064806c467f678d3b4080d6f1cc50af26ca20941730828'
|
||||
. '1b68af282623eaa63e5b5c0723d8b8c37ff0777b1a20f8ccb1dccc43997f1ee0e44da4a67a',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider invalidAsn1Structures
|
||||
*
|
||||
* @covers ::fromAsn1
|
||||
* @covers ::readAsn1Content
|
||||
* @covers ::readAsn1Integer
|
||||
* @covers ::retrievePositiveInteger
|
||||
*/
|
||||
public function fromAsn1ShouldRaiseExceptionOnInvalidMessage($message)
|
||||
{
|
||||
$converter = new MultibyteStringConverter();
|
||||
$message = hex2bin($message);
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$converter->fromAsn1($message, 64);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function invalidAsn1Structures()
|
||||
{
|
||||
return [
|
||||
'Not a sequence' => [''],
|
||||
'Sequence without length' => ['30'],
|
||||
'Only one string element' => ['3006030204f0'],
|
||||
'Only one integer element' => ['3004020101'],
|
||||
'Integer+string elements' => ['300a020101030204f0'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -11,13 +11,13 @@ namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha256Test extends \PHPUnit_Framework_TestCase
|
||||
class Sha256Test extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @uses Lcobucci\JWT\Signer\OpenSSL
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getAlgorithmId
|
||||
*/
|
||||
@@ -32,7 +32,7 @@ class Sha256Test extends \PHPUnit_Framework_TestCase
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @uses Lcobucci\JWT\Signer\OpenSSL
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getAlgorithm
|
||||
*/
|
||||
@@ -47,14 +47,14 @@ class Sha256Test extends \PHPUnit_Framework_TestCase
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @uses Lcobucci\JWT\Signer\OpenSSL
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getSignatureLength
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getKeyLength
|
||||
*/
|
||||
public function getSignatureLengthMustBeCorrect()
|
||||
public function getKeyLengthMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha256();
|
||||
|
||||
$this->assertEquals(64, $signer->getSignatureLength());
|
||||
$this->assertEquals(64, $signer->getKeyLength());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,13 +11,13 @@ namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha384Test extends \PHPUnit_Framework_TestCase
|
||||
class Sha384Test extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @uses Lcobucci\JWT\Signer\OpenSSL
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getAlgorithmId
|
||||
*/
|
||||
@@ -32,7 +32,7 @@ class Sha384Test extends \PHPUnit_Framework_TestCase
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @uses Lcobucci\JWT\Signer\OpenSSL
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getAlgorithm
|
||||
*/
|
||||
@@ -47,14 +47,14 @@ class Sha384Test extends \PHPUnit_Framework_TestCase
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @uses Lcobucci\JWT\Signer\OpenSSL
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getSignatureLength
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getKeyLength
|
||||
*/
|
||||
public function getSignatureLengthMustBeCorrect()
|
||||
public function getKeyLengthMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha384();
|
||||
|
||||
$this->assertEquals(96, $signer->getSignatureLength());
|
||||
$this->assertEquals(96, $signer->getKeyLength());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,13 +11,13 @@ namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha512Test extends \PHPUnit_Framework_TestCase
|
||||
class Sha512Test extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @uses Lcobucci\JWT\Signer\OpenSSL
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getAlgorithmId
|
||||
*/
|
||||
@@ -32,7 +32,7 @@ class Sha512Test extends \PHPUnit_Framework_TestCase
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @uses Lcobucci\JWT\Signer\OpenSSL
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getAlgorithm
|
||||
*/
|
||||
@@ -47,14 +47,14 @@ class Sha512Test extends \PHPUnit_Framework_TestCase
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @uses Lcobucci\JWT\Signer\OpenSSL
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getSignatureLength
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getKeyLength
|
||||
*/
|
||||
public function getSignatureLengthMustBeCorrect()
|
||||
public function getKeyLengthMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha512();
|
||||
|
||||
$this->assertEquals(132, $signer->getSignatureLength());
|
||||
$this->assertEquals(132, $signer->getKeyLength());
|
||||
}
|
||||
}
|
||||
|
||||
188
vendor/lcobucci/jwt/test/unit/Signer/EcdsaTest.php
vendored
188
vendor/lcobucci/jwt/test/unit/Signer/EcdsaTest.php
vendored
@@ -7,70 +7,43 @@
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
use Lcobucci\JWT\Signer\Ecdsa\KeyParser;
|
||||
use Mdanter\Ecc\Crypto\Signature\Signature;
|
||||
use Mdanter\Ecc\Crypto\Signature\Signer;
|
||||
use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface;
|
||||
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
|
||||
use Mdanter\Ecc\Math\MathAdapterInterface as Adapter;
|
||||
use Mdanter\Ecc\Primitives\PointInterface;
|
||||
use Mdanter\Ecc\Random\RandomNumberGeneratorInterface;
|
||||
use Lcobucci\JWT\Keys;
|
||||
use Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter;
|
||||
use const OPENSSL_ALGO_SHA256;
|
||||
use function openssl_pkey_get_private;
|
||||
use function openssl_pkey_get_public;
|
||||
use function openssl_sign;
|
||||
use function openssl_verify;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class EcdsaTest extends \PHPUnit_Framework_TestCase
|
||||
class EcdsaTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var Adapter|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $adapter;
|
||||
use Keys;
|
||||
|
||||
/**
|
||||
* @var Signer|\PHPUnit_Framework_MockObject_MockObject
|
||||
* @var MultibyteStringConverter
|
||||
*/
|
||||
private $signer;
|
||||
|
||||
/**
|
||||
* @var RandomNumberGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $randomGenerator;
|
||||
|
||||
/**
|
||||
* @var KeyParser|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $parser;
|
||||
private $pointsManipulator;
|
||||
|
||||
/**
|
||||
* @before
|
||||
*/
|
||||
public function createDependencies()
|
||||
{
|
||||
$this->adapter = $this->getMock(Adapter::class);
|
||||
$this->signer = $this->getMock(Signer::class, [], [$this->adapter]);
|
||||
$this->randomGenerator = $this->getMock(RandomNumberGeneratorInterface::class);
|
||||
$this->parser = $this->getMock(KeyParser::class, [], [], '', false);
|
||||
$this->pointsManipulator = new MultibyteStringConverter();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Ecdsa
|
||||
*/
|
||||
private function getSigner()
|
||||
{
|
||||
$signer = $this->getMockForAbstractClass(
|
||||
Ecdsa::class,
|
||||
[$this->adapter, $this->signer, $this->parser]
|
||||
);
|
||||
|
||||
$signer->method('getSignatureLength')
|
||||
->willReturn(64);
|
||||
$signer = $this->getMockForAbstractClass(Ecdsa::class, [$this->pointsManipulator]);
|
||||
|
||||
$signer->method('getAlgorithm')
|
||||
->willReturn('sha256');
|
||||
->willReturn(OPENSSL_ALGO_SHA256);
|
||||
|
||||
$signer->method('getAlgorithmId')
|
||||
->willReturn('ES256');
|
||||
->willReturn('ES256');
|
||||
|
||||
$signer->method('getKeyLength')
|
||||
->willReturn(64);
|
||||
|
||||
return $signer;
|
||||
}
|
||||
@@ -78,96 +51,67 @@ class EcdsaTest extends \PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::__construct
|
||||
* @covers \Lcobucci\JWT\Signer\Ecdsa::createHash
|
||||
* @covers \Lcobucci\JWT\Signer\Ecdsa::getKeyType
|
||||
* @covers \Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter
|
||||
* @covers \Lcobucci\JWT\Signer\OpenSSL
|
||||
* @covers \Lcobucci\JWT\Signer\BaseSigner
|
||||
*
|
||||
* @uses \Lcobucci\JWT\Signer\Ecdsa::__construct
|
||||
* @uses \Lcobucci\JWT\Signer\Key
|
||||
* @uses \Lcobucci\JWT\Signature
|
||||
*/
|
||||
public function constructShouldConfigureDependencies()
|
||||
public function createHashShouldReturnTheAHashBasedOnTheOpenSslSignature()
|
||||
{
|
||||
$signer = $this->getSigner();
|
||||
$payload = 'testing';
|
||||
|
||||
$this->assertAttributeSame($this->adapter, 'adapter', $signer);
|
||||
$this->assertAttributeSame($this->signer, 'signer', $signer);
|
||||
$this->assertAttributeSame($this->parser, 'parser', $signer);
|
||||
$signer = $this->getSigner();
|
||||
$signature = $signer->sign($payload, self::$ecdsaKeys['private']);
|
||||
|
||||
$publicKey = openssl_pkey_get_public(self::$ecdsaKeys['public1']->getContent());
|
||||
|
||||
self::assertInternalType('resource', $publicKey);
|
||||
self::assertSame(
|
||||
1,
|
||||
openssl_verify(
|
||||
$payload,
|
||||
$this->pointsManipulator->toAsn1($signature, $signer->getKeyLength()),
|
||||
$publicKey,
|
||||
OPENSSL_ALGO_SHA256
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
* @covers \Lcobucci\JWT\Signer\Ecdsa::doVerify
|
||||
* @covers \Lcobucci\JWT\Signer\Ecdsa::getKeyType
|
||||
* @covers \Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter
|
||||
* @covers \Lcobucci\JWT\Signer\OpenSSL
|
||||
* @covers \Lcobucci\JWT\Signer\BaseSigner
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::createHash
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::createSigningHash
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::createSignatureHash
|
||||
*/
|
||||
public function createHashShouldReturnAHashUsingPrivateKey()
|
||||
{
|
||||
$signer = $this->getSigner();
|
||||
$key = new Key('testing');
|
||||
$privateKey = $this->getMock(PrivateKeyInterface::class);
|
||||
$point = $this->getMock(PointInterface::class);
|
||||
|
||||
$privateKey->method('getPoint')
|
||||
->willReturn($point);
|
||||
|
||||
$point->method('getOrder')
|
||||
->willReturn('1');
|
||||
|
||||
$this->parser->expects($this->once())
|
||||
->method('getPrivateKey')
|
||||
->with($key)
|
||||
->willReturn($privateKey);
|
||||
|
||||
$this->randomGenerator->expects($this->once())
|
||||
->method('generate')
|
||||
->with('1')
|
||||
->willReturn('123');
|
||||
|
||||
$this->adapter->expects($this->once())
|
||||
->method('hexDec')
|
||||
->willReturn('123');
|
||||
|
||||
$this->adapter->expects($this->exactly(2))
|
||||
->method('decHex')
|
||||
->willReturn('123');
|
||||
|
||||
$this->signer->expects($this->once())
|
||||
->method('sign')
|
||||
->with($privateKey, $this->isType('string'), $this->isType('string'))
|
||||
->willReturn(new Signature('1234', '456'));
|
||||
|
||||
$this->assertInternalType('string', $signer->createHash('testing', $key, $this->randomGenerator));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::doVerify
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::createSigningHash
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::extractSignature
|
||||
* @uses \Lcobucci\JWT\Signer\Ecdsa::__construct
|
||||
* @uses \Lcobucci\JWT\Signer\Key
|
||||
*/
|
||||
public function doVerifyShouldDelegateToEcdsaSignerUsingPublicKey()
|
||||
{
|
||||
$payload = 'testing';
|
||||
$privateKey = openssl_pkey_get_private(self::$ecdsaKeys['private']->getContent());
|
||||
|
||||
self::assertInternalType('resource', $privateKey);
|
||||
|
||||
$signature = '';
|
||||
openssl_sign($payload, $signature, $privateKey, OPENSSL_ALGO_SHA256);
|
||||
|
||||
$signer = $this->getSigner();
|
||||
$key = new Key('testing');
|
||||
$publicKey = $this->getMock(PublicKeyInterface::class);
|
||||
|
||||
$this->parser->expects($this->once())
|
||||
->method('getPublicKey')
|
||||
->with($key)
|
||||
->willReturn($publicKey);
|
||||
|
||||
$this->adapter->expects($this->exactly(3))
|
||||
->method('hexDec')
|
||||
->willReturn('123');
|
||||
|
||||
$this->signer->expects($this->once())
|
||||
->method('verify')
|
||||
->with($publicKey, $this->isInstanceOf(Signature::class), $this->isType('string'))
|
||||
->willReturn(true);
|
||||
|
||||
$this->assertTrue($signer->doVerify('testing', 'testing2', $key));
|
||||
self::assertTrue(
|
||||
$signer->verify(
|
||||
$this->pointsManipulator->fromAsn1($signature, $signer->getKeyLength()),
|
||||
$payload,
|
||||
self::$ecdsaKeys['public1']
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Lcobucci\JWT\Signer\Hmac;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Sha256Test extends \PHPUnit_Framework_TestCase
|
||||
class Sha256Test extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Lcobucci\JWT\Signer\Hmac;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Sha384Test extends \PHPUnit_Framework_TestCase
|
||||
class Sha384Test extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Lcobucci\JWT\Signer\Hmac;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Sha512Test extends \PHPUnit_Framework_TestCase
|
||||
class Sha512Test extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Lcobucci\JWT\Signer;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class HmacTest extends \PHPUnit_Framework_TestCase
|
||||
class HmacTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var Hmac|\PHPUnit_Framework_MockObject_MockObject
|
||||
|
||||
@@ -13,7 +13,7 @@ use org\bovigo\vfs\vfsStream;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 3.0.4
|
||||
*/
|
||||
class KeyTest extends \PHPUnit_Framework_TestCase
|
||||
class KeyTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @before
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Lcobucci\JWT\Signer;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class KeychainTest extends \PHPUnit_Framework_TestCase
|
||||
class KeychainTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Lcobucci\JWT\Signer\Rsa;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha256Test extends \PHPUnit_Framework_TestCase
|
||||
class Sha256Test extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Lcobucci\JWT\Signer\Rsa;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha384Test extends \PHPUnit_Framework_TestCase
|
||||
class Sha384Test extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Lcobucci\JWT\Signer\Rsa;
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha512Test extends \PHPUnit_Framework_TestCase
|
||||
class Sha512Test extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
|
||||
188
vendor/lcobucci/jwt/test/unit/Signer/RsaTest.php
vendored
Normal file
188
vendor/lcobucci/jwt/test/unit/Signer/RsaTest.php
vendored
Normal file
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Lcobucci\JWT\Keys;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use const OPENSSL_ALGO_SHA256;
|
||||
use function openssl_pkey_get_private;
|
||||
use function openssl_pkey_get_public;
|
||||
use function openssl_sign;
|
||||
use function openssl_verify;
|
||||
|
||||
final class RsaTest extends TestCase
|
||||
{
|
||||
use Keys;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::createHash
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::validateKey
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::getKeyType
|
||||
* @covers \Lcobucci\JWT\Signer\OpenSSL
|
||||
* @covers \Lcobucci\JWT\Signer\BaseSigner
|
||||
*
|
||||
* @uses \Lcobucci\JWT\Signer\Key
|
||||
* @uses \Lcobucci\JWT\Signature
|
||||
*/
|
||||
public function createHashShouldReturnAValidOpensslSignature()
|
||||
{
|
||||
$payload = 'testing';
|
||||
|
||||
$signer = $this->getSigner();
|
||||
$signature = $signer->sign($payload, self::$rsaKeys['private']);
|
||||
|
||||
$publicKey = openssl_pkey_get_public(self::$rsaKeys['public']->getContent());
|
||||
self::assertInternalType('resource', $publicKey);
|
||||
self::assertSame(1, openssl_verify($payload, $signature, $publicKey, OPENSSL_ALGO_SHA256));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::createHash
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::validateKey
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::getKeyType
|
||||
* @covers \Lcobucci\JWT\Signer\OpenSSL
|
||||
* @covers \Lcobucci\JWT\Signer\BaseSigner
|
||||
*
|
||||
* @uses \Lcobucci\JWT\Signer\Key
|
||||
*/
|
||||
public function createHashShouldRaiseAnExceptionWhenKeyIsInvalid()
|
||||
{
|
||||
$key = <<<KEY
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MGECAQACEQC4MRKSVsq5XnRBrJoX6+rnAgMBAAECECO8SZkgw6Yg66A6SUly/3kC
|
||||
CQDtPXZtCQWJuwIJAMbBu17GDOrFAggopfhNlFcjkwIIVjb7G+U0/TECCEERyvxP
|
||||
TWdN
|
||||
-----END RSA PRIVATE KEY-----
|
||||
KEY;
|
||||
|
||||
$signer = $this->getSigner();
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('There was an error while creating the signature');
|
||||
|
||||
$signer->sign('testing', new Key($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::createHash
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::validateKey
|
||||
* @covers \Lcobucci\JWT\Signer\OpenSSL
|
||||
* @covers \Lcobucci\JWT\Signer\BaseSigner
|
||||
*
|
||||
* @uses \Lcobucci\JWT\Signer\Key
|
||||
*/
|
||||
public function createHashShouldRaiseAnExceptionWhenKeyIsNotParseable()
|
||||
{
|
||||
$signer = $this->getSigner();
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('It was not possible to parse your key');
|
||||
|
||||
$signer->sign('testing', new Key('blablabla'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::createHash
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::validateKey
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::getKeyType
|
||||
* @covers \Lcobucci\JWT\Signer\OpenSSL
|
||||
* @covers \Lcobucci\JWT\Signer\BaseSigner
|
||||
*
|
||||
* @uses \Lcobucci\JWT\Signer\Key
|
||||
*/
|
||||
public function createHashShouldRaiseAnExceptionWhenKeyTypeIsNotRsa()
|
||||
{
|
||||
$signer = $this->getSigner();
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('This key is not compatible with this signer');
|
||||
|
||||
$signer->sign('testing', self::$ecdsaKeys['private']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::doVerify
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::validateKey
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::getKeyType
|
||||
* @covers \Lcobucci\JWT\Signer\OpenSSL
|
||||
* @covers \Lcobucci\JWT\Signer\BaseSigner
|
||||
*
|
||||
* @uses \Lcobucci\JWT\Signer\Key
|
||||
*/
|
||||
public function doVerifyShouldReturnTrueWhenSignatureIsValid()
|
||||
{
|
||||
$payload = 'testing';
|
||||
$privateKey = openssl_pkey_get_private(self::$rsaKeys['private']->getContent());
|
||||
self::assertInternalType('resource', $privateKey);
|
||||
|
||||
$signature = '';
|
||||
openssl_sign($payload, $signature, $privateKey, OPENSSL_ALGO_SHA256);
|
||||
|
||||
$signer = $this->getSigner();
|
||||
|
||||
self::assertTrue($signer->verify($signature, $payload, self::$rsaKeys['public']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::doVerify
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::validateKey
|
||||
* @covers \Lcobucci\JWT\Signer\OpenSSL
|
||||
* @covers \Lcobucci\JWT\Signer\BaseSigner
|
||||
*
|
||||
* @uses \Lcobucci\JWT\Signer\Key
|
||||
*/
|
||||
public function doVerifyShouldRaiseAnExceptionWhenKeyIsNotParseable()
|
||||
{
|
||||
$signer = $this->getSigner();
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('It was not possible to parse your key');
|
||||
|
||||
$signer->verify('testing', 'testing', new Key('blablabla'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::doVerify
|
||||
* @covers \Lcobucci\JWT\Signer\Rsa::validateKey
|
||||
* @covers \Lcobucci\JWT\Signer\OpenSSL
|
||||
* @covers \Lcobucci\JWT\Signer\BaseSigner
|
||||
*
|
||||
* @uses \Lcobucci\JWT\Signer\Key
|
||||
*/
|
||||
public function doVerifyShouldRaiseAnExceptionWhenKeyTypeIsNotRsa()
|
||||
{
|
||||
$signer = $this->getSigner();
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('It was not possible to parse your key');
|
||||
|
||||
$signer->verify('testing', 'testing', self::$ecdsaKeys['private']);
|
||||
}
|
||||
|
||||
private function getSigner()
|
||||
{
|
||||
$signer = $this->getMockForAbstractClass(Rsa::class);
|
||||
|
||||
$signer->method('getAlgorithm')
|
||||
->willReturn(OPENSSL_ALGO_SHA256);
|
||||
|
||||
$signer->method('getAlgorithmId')
|
||||
->willReturn('RS256');
|
||||
|
||||
return $signer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user