EcdsaTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
  4. *
  5. * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
  6. */
  7. namespace Lcobucci\JWT\Signer;
  8. use Lcobucci\JWT\Signer\Ecdsa\KeyParser;
  9. use Mdanter\Ecc\Crypto\Signature\Signature;
  10. use Mdanter\Ecc\Crypto\Signature\Signer;
  11. use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface;
  12. use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
  13. use Mdanter\Ecc\Math\MathAdapterInterface as Adapter;
  14. use Mdanter\Ecc\Primitives\PointInterface;
  15. use Mdanter\Ecc\Random\RandomNumberGeneratorInterface;
  16. /**
  17. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  18. * @since 2.1.0
  19. */
  20. class EcdsaTest extends \PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * @var Adapter|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $adapter;
  26. /**
  27. * @var Signer|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $signer;
  30. /**
  31. * @var RandomNumberGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $randomGenerator;
  34. /**
  35. * @var KeyParser|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $parser;
  38. /**
  39. * @before
  40. */
  41. public function createDependencies()
  42. {
  43. $this->adapter = $this->getMock(Adapter::class);
  44. $this->signer = $this->getMock(Signer::class, [], [$this->adapter]);
  45. $this->randomGenerator = $this->getMock(RandomNumberGeneratorInterface::class);
  46. $this->parser = $this->getMock(KeyParser::class, [], [], '', false);
  47. }
  48. /**
  49. * @return Ecdsa
  50. */
  51. private function getSigner()
  52. {
  53. $signer = $this->getMockForAbstractClass(
  54. Ecdsa::class,
  55. [$this->adapter, $this->signer, $this->parser]
  56. );
  57. $signer->method('getSignatureLength')
  58. ->willReturn(64);
  59. $signer->method('getAlgorithm')
  60. ->willReturn('sha256');
  61. $signer->method('getAlgorithmId')
  62. ->willReturn('ES256');
  63. return $signer;
  64. }
  65. /**
  66. * @test
  67. *
  68. * @covers Lcobucci\JWT\Signer\Ecdsa::__construct
  69. */
  70. public function constructShouldConfigureDependencies()
  71. {
  72. $signer = $this->getSigner();
  73. $this->assertAttributeSame($this->adapter, 'adapter', $signer);
  74. $this->assertAttributeSame($this->signer, 'signer', $signer);
  75. $this->assertAttributeSame($this->parser, 'parser', $signer);
  76. }
  77. /**
  78. * @test
  79. *
  80. * @uses Lcobucci\JWT\Signer\Ecdsa::__construct
  81. * @uses Lcobucci\JWT\Signer\Key
  82. *
  83. * @covers Lcobucci\JWT\Signer\Ecdsa::createHash
  84. * @covers Lcobucci\JWT\Signer\Ecdsa::createSigningHash
  85. * @covers Lcobucci\JWT\Signer\Ecdsa::createSignatureHash
  86. */
  87. public function createHashShouldReturnAHashUsingPrivateKey()
  88. {
  89. $signer = $this->getSigner();
  90. $key = new Key('testing');
  91. $privateKey = $this->getMock(PrivateKeyInterface::class);
  92. $point = $this->getMock(PointInterface::class);
  93. $privateKey->method('getPoint')
  94. ->willReturn($point);
  95. $point->method('getOrder')
  96. ->willReturn('1');
  97. $this->parser->expects($this->once())
  98. ->method('getPrivateKey')
  99. ->with($key)
  100. ->willReturn($privateKey);
  101. $this->randomGenerator->expects($this->once())
  102. ->method('generate')
  103. ->with('1')
  104. ->willReturn('123');
  105. $this->adapter->expects($this->once())
  106. ->method('hexDec')
  107. ->willReturn('123');
  108. $this->adapter->expects($this->exactly(2))
  109. ->method('decHex')
  110. ->willReturn('123');
  111. $this->signer->expects($this->once())
  112. ->method('sign')
  113. ->with($privateKey, $this->isType('string'), $this->isType('string'))
  114. ->willReturn(new Signature('1234', '456'));
  115. $this->assertInternalType('string', $signer->createHash('testing', $key, $this->randomGenerator));
  116. }
  117. /**
  118. * @test
  119. *
  120. * @uses Lcobucci\JWT\Signer\Ecdsa::__construct
  121. * @uses Lcobucci\JWT\Signer\Key
  122. *
  123. * @covers Lcobucci\JWT\Signer\Ecdsa::doVerify
  124. * @covers Lcobucci\JWT\Signer\Ecdsa::createSigningHash
  125. * @covers Lcobucci\JWT\Signer\Ecdsa::extractSignature
  126. */
  127. public function doVerifyShouldDelegateToEcdsaSignerUsingPublicKey()
  128. {
  129. $signer = $this->getSigner();
  130. $key = new Key('testing');
  131. $publicKey = $this->getMock(PublicKeyInterface::class);
  132. $this->parser->expects($this->once())
  133. ->method('getPublicKey')
  134. ->with($key)
  135. ->willReturn($publicKey);
  136. $this->adapter->expects($this->exactly(3))
  137. ->method('hexDec')
  138. ->willReturn('123');
  139. $this->signer->expects($this->once())
  140. ->method('verify')
  141. ->with($publicKey, $this->isInstanceOf(Signature::class), $this->isType('string'))
  142. ->willReturn(true);
  143. $this->assertTrue($signer->doVerify('testing', 'testing2', $key));
  144. }
  145. }