KeyParserTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Ecdsa;
  8. use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface;
  9. use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
  10. use Mdanter\Ecc\Math\MathAdapterInterface;
  11. use Mdanter\Ecc\Serializer\PrivateKey\PrivateKeySerializerInterface;
  12. use Mdanter\Ecc\Serializer\PublicKey\PublicKeySerializerInterface;
  13. use Lcobucci\JWT\Signer\Key;
  14. /**
  15. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  16. * @since 3.0.4
  17. */
  18. class KeyParserTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var MathAdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $adapter;
  24. /**
  25. * @var PrivateKeySerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $privateKeySerializer;
  28. /**
  29. * @var PublicKeySerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $publicKeySerializer;
  32. /**
  33. * @before
  34. */
  35. public function createDependencies()
  36. {
  37. $this->adapter = $this->getMock(MathAdapterInterface::class);
  38. $this->privateKeySerializer = $this->getMock(PrivateKeySerializerInterface::class);
  39. $this->publicKeySerializer = $this->getMock(PublicKeySerializerInterface::class);
  40. }
  41. /**
  42. * @test
  43. *
  44. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
  45. */
  46. public function constructShouldConfigureDependencies()
  47. {
  48. $parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
  49. $this->assertAttributeSame($this->privateKeySerializer, 'privateKeySerializer', $parser);
  50. $this->assertAttributeSame($this->publicKeySerializer, 'publicKeySerializer', $parser);
  51. }
  52. /**
  53. * @test
  54. *
  55. * @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
  56. * @uses Lcobucci\JWT\Signer\Key
  57. *
  58. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPrivateKey
  59. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
  60. */
  61. public function getPrivateKeyShouldAskSerializerToParseTheKey()
  62. {
  63. $privateKey = $this->getMock(PrivateKeyInterface::class);
  64. $keyContent = 'MHcCAQEEIBGpMoZJ64MMSzuo5JbmXpf9V4qSWdLIl/8RmJLcfn/qoAoGC'
  65. . 'CqGSM49AwEHoUQDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpnd0wxa2iF'
  66. . 'ruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==';
  67. $this->privateKeySerializer->expects($this->once())
  68. ->method('parse')
  69. ->with($keyContent)
  70. ->willReturn($privateKey);
  71. $parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
  72. $this->assertSame($privateKey, $parser->getPrivateKey($this->getPrivateKey()));
  73. }
  74. /**
  75. * @test
  76. *
  77. * @expectedException \InvalidArgumentException
  78. *
  79. * @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
  80. * @uses Lcobucci\JWT\Signer\Key
  81. *
  82. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPrivateKey
  83. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
  84. */
  85. public function getPrivateKeyShouldRaiseExceptionWhenAWrongKeyWasGiven()
  86. {
  87. $this->privateKeySerializer->expects($this->never())
  88. ->method('parse');
  89. $parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
  90. $parser->getPrivateKey($this->getPublicKey());
  91. }
  92. /**
  93. * @test
  94. *
  95. * @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
  96. * @uses Lcobucci\JWT\Signer\Key
  97. *
  98. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPublicKey
  99. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
  100. */
  101. public function getPublicKeyShouldAskSerializerToParseTheKey()
  102. {
  103. $publicKey = $this->getMock(PublicKeyInterface::class);
  104. $keyContent = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpn'
  105. . 'd0wxa2iFruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==';
  106. $this->publicKeySerializer->expects($this->once())
  107. ->method('parse')
  108. ->with($keyContent)
  109. ->willReturn($publicKey);
  110. $parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
  111. $this->assertSame($publicKey, $parser->getPublicKey($this->getPublicKey()));
  112. }
  113. /**
  114. * @test
  115. *
  116. * @expectedException \InvalidArgumentException
  117. *
  118. * @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
  119. * @uses Lcobucci\JWT\Signer\Key
  120. *
  121. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPublicKey
  122. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
  123. */
  124. public function getPublicKeyShouldRaiseExceptionWhenAWrongKeyWasGiven()
  125. {
  126. $this->publicKeySerializer->expects($this->never())
  127. ->method('parse');
  128. $parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
  129. $parser->getPublicKey($this->getPrivateKey());
  130. }
  131. /**
  132. * @return Key
  133. */
  134. private function getPrivateKey()
  135. {
  136. return new Key(
  137. "-----BEGIN EC PRIVATE KEY-----\n"
  138. . "MHcCAQEEIBGpMoZJ64MMSzuo5JbmXpf9V4qSWdLIl/8RmJLcfn/qoAoGCCqGSM49\n"
  139. . "AwEHoUQDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpnd0wxa2iFruiI2tsEdGFTLTsy\n"
  140. . "U+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==\n"
  141. . "-----END EC PRIVATE KEY-----"
  142. );
  143. }
  144. /**
  145. * @return Key
  146. */
  147. private function getPublicKey()
  148. {
  149. return new Key(
  150. "-----BEGIN PUBLIC KEY-----\n"
  151. . "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpn\n"
  152. . "d0wxa2iFruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==\n"
  153. . "-----END PUBLIC KEY-----"
  154. );
  155. }
  156. }