RsaTokenTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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\FunctionalTests;
  8. use Lcobucci\JWT\Builder;
  9. use Lcobucci\JWT\Keys;
  10. use Lcobucci\JWT\Parser;
  11. use Lcobucci\JWT\Signer\Key;
  12. use Lcobucci\JWT\Token;
  13. use Lcobucci\JWT\Signature;
  14. use Lcobucci\JWT\Signer\Rsa\Sha256;
  15. use Lcobucci\JWT\Signer\Rsa\Sha512;
  16. /**
  17. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  18. * @since 2.1.0
  19. */
  20. class RsaTokenTest extends \PHPUnit_Framework_TestCase
  21. {
  22. use Keys;
  23. /**
  24. * @var Sha256
  25. */
  26. private $signer;
  27. /**
  28. * @before
  29. */
  30. public function createSigner()
  31. {
  32. $this->signer = new Sha256();
  33. }
  34. /**
  35. * @test
  36. *
  37. * @expectedException \InvalidArgumentException
  38. *
  39. * @covers Lcobucci\JWT\Builder
  40. * @covers Lcobucci\JWT\Token
  41. * @covers Lcobucci\JWT\Signature
  42. * @covers Lcobucci\JWT\Claim\Factory
  43. * @covers Lcobucci\JWT\Claim\Basic
  44. * @covers Lcobucci\JWT\Parsing\Encoder
  45. * @covers Lcobucci\JWT\Signer\Key
  46. * @covers Lcobucci\JWT\Signer\BaseSigner
  47. * @covers Lcobucci\JWT\Signer\Rsa
  48. * @covers Lcobucci\JWT\Signer\Rsa\Sha256
  49. */
  50. public function builderShouldRaiseExceptionWhenKeyIsInvalid()
  51. {
  52. $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
  53. (new Builder())->setId(1)
  54. ->setAudience('http://client.abc.com')
  55. ->setIssuer('http://api.abc.com')
  56. ->set('user', $user)
  57. ->sign($this->signer, new Key('testing'));
  58. }
  59. /**
  60. * @test
  61. *
  62. * @expectedException \InvalidArgumentException
  63. *
  64. * @covers Lcobucci\JWT\Builder
  65. * @covers Lcobucci\JWT\Token
  66. * @covers Lcobucci\JWT\Signature
  67. * @covers Lcobucci\JWT\Claim\Factory
  68. * @covers Lcobucci\JWT\Claim\Basic
  69. * @covers Lcobucci\JWT\Parsing\Encoder
  70. * @covers Lcobucci\JWT\Signer\Key
  71. * @covers Lcobucci\JWT\Signer\BaseSigner
  72. * @covers Lcobucci\JWT\Signer\Rsa
  73. * @covers Lcobucci\JWT\Signer\Rsa\Sha256
  74. */
  75. public function builderShouldRaiseExceptionWhenKeyIsNotRsaCompatible()
  76. {
  77. $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
  78. (new Builder())->setId(1)
  79. ->setAudience('http://client.abc.com')
  80. ->setIssuer('http://api.abc.com')
  81. ->set('user', $user)
  82. ->sign($this->signer, static::$ecdsaKeys['private']);
  83. }
  84. /**
  85. * @test
  86. *
  87. * @covers Lcobucci\JWT\Builder
  88. * @covers Lcobucci\JWT\Token
  89. * @covers Lcobucci\JWT\Signature
  90. * @covers Lcobucci\JWT\Claim\Factory
  91. * @covers Lcobucci\JWT\Claim\Basic
  92. * @covers Lcobucci\JWT\Parsing\Encoder
  93. * @covers Lcobucci\JWT\Signer\Key
  94. * @covers Lcobucci\JWT\Signer\BaseSigner
  95. * @covers Lcobucci\JWT\Signer\Rsa
  96. * @covers Lcobucci\JWT\Signer\Rsa\Sha256
  97. */
  98. public function builderCanGenerateAToken()
  99. {
  100. $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
  101. $token = (new Builder())->setId(1)
  102. ->setAudience('http://client.abc.com')
  103. ->setIssuer('http://api.abc.com')
  104. ->set('user', $user)
  105. ->setHeader('jki', '1234')
  106. ->sign($this->signer, static::$rsaKeys['private'])
  107. ->getToken();
  108. $this->assertAttributeInstanceOf(Signature::class, 'signature', $token);
  109. $this->assertEquals('1234', $token->getHeader('jki'));
  110. $this->assertEquals('http://client.abc.com', $token->getClaim('aud'));
  111. $this->assertEquals('http://api.abc.com', $token->getClaim('iss'));
  112. $this->assertEquals($user, $token->getClaim('user'));
  113. return $token;
  114. }
  115. /**
  116. * @test
  117. *
  118. * @depends builderCanGenerateAToken
  119. *
  120. * @covers Lcobucci\JWT\Builder
  121. * @covers Lcobucci\JWT\Parser
  122. * @covers Lcobucci\JWT\Token
  123. * @covers Lcobucci\JWT\Signature
  124. * @covers Lcobucci\JWT\Claim\Factory
  125. * @covers Lcobucci\JWT\Claim\Basic
  126. * @covers Lcobucci\JWT\Parsing\Encoder
  127. * @covers Lcobucci\JWT\Parsing\Decoder
  128. */
  129. public function parserCanReadAToken(Token $generated)
  130. {
  131. $read = (new Parser())->parse((string) $generated);
  132. $this->assertEquals($generated, $read);
  133. $this->assertEquals('testing', $read->getClaim('user')->name);
  134. }
  135. /**
  136. * @test
  137. *
  138. * @depends builderCanGenerateAToken
  139. *
  140. * @covers Lcobucci\JWT\Builder
  141. * @covers Lcobucci\JWT\Parser
  142. * @covers Lcobucci\JWT\Token
  143. * @covers Lcobucci\JWT\Signature
  144. * @covers Lcobucci\JWT\Parsing\Encoder
  145. * @covers Lcobucci\JWT\Claim\Factory
  146. * @covers Lcobucci\JWT\Claim\Basic
  147. * @covers Lcobucci\JWT\Signer\Key
  148. * @covers Lcobucci\JWT\Signer\BaseSigner
  149. * @covers Lcobucci\JWT\Signer\Rsa
  150. * @covers Lcobucci\JWT\Signer\Rsa\Sha256
  151. */
  152. public function verifyShouldReturnFalseWhenKeyIsNotRight(Token $token)
  153. {
  154. $this->assertFalse($token->verify($this->signer, self::$rsaKeys['encrypted-public']));
  155. }
  156. /**
  157. * @test
  158. *
  159. * @depends builderCanGenerateAToken
  160. *
  161. * @covers Lcobucci\JWT\Builder
  162. * @covers Lcobucci\JWT\Parser
  163. * @covers Lcobucci\JWT\Token
  164. * @covers Lcobucci\JWT\Signature
  165. * @covers Lcobucci\JWT\Parsing\Encoder
  166. * @covers Lcobucci\JWT\Claim\Factory
  167. * @covers Lcobucci\JWT\Claim\Basic
  168. * @covers Lcobucci\JWT\Signer\Key
  169. * @covers Lcobucci\JWT\Signer\BaseSigner
  170. * @covers Lcobucci\JWT\Signer\Rsa
  171. * @covers Lcobucci\JWT\Signer\Rsa\Sha256
  172. * @covers Lcobucci\JWT\Signer\Rsa\Sha512
  173. */
  174. public function verifyShouldReturnFalseWhenAlgorithmIsDifferent(Token $token)
  175. {
  176. $this->assertFalse($token->verify(new Sha512(), self::$rsaKeys['public']));
  177. }
  178. /**
  179. * @test
  180. *
  181. * @expectedException \InvalidArgumentException
  182. *
  183. * @depends builderCanGenerateAToken
  184. *
  185. * @covers Lcobucci\JWT\Builder
  186. * @covers Lcobucci\JWT\Parser
  187. * @covers Lcobucci\JWT\Token
  188. * @covers Lcobucci\JWT\Signature
  189. * @covers Lcobucci\JWT\Parsing\Encoder
  190. * @covers Lcobucci\JWT\Claim\Factory
  191. * @covers Lcobucci\JWT\Claim\Basic
  192. * @covers Lcobucci\JWT\Signer\Key
  193. * @covers Lcobucci\JWT\Signer\BaseSigner
  194. * @covers Lcobucci\JWT\Signer\Rsa
  195. * @covers Lcobucci\JWT\Signer\Rsa\Sha256
  196. */
  197. public function verifyShouldRaiseExceptionWhenKeyIsNotRsaCompatible(Token $token)
  198. {
  199. $this->assertFalse($token->verify($this->signer, self::$ecdsaKeys['public1']));
  200. }
  201. /**
  202. * @test
  203. *
  204. * @depends builderCanGenerateAToken
  205. *
  206. * @covers Lcobucci\JWT\Builder
  207. * @covers Lcobucci\JWT\Parser
  208. * @covers Lcobucci\JWT\Token
  209. * @covers Lcobucci\JWT\Signature
  210. * @covers Lcobucci\JWT\Parsing\Encoder
  211. * @covers Lcobucci\JWT\Claim\Factory
  212. * @covers Lcobucci\JWT\Claim\Basic
  213. * @covers Lcobucci\JWT\Signer\Key
  214. * @covers Lcobucci\JWT\Signer\BaseSigner
  215. * @covers Lcobucci\JWT\Signer\Rsa
  216. * @covers Lcobucci\JWT\Signer\Rsa\Sha256
  217. */
  218. public function verifyShouldReturnTrueWhenKeyIsRight(Token $token)
  219. {
  220. $this->assertTrue($token->verify($this->signer, self::$rsaKeys['public']));
  221. }
  222. /**
  223. * @test
  224. *
  225. * @covers Lcobucci\JWT\Builder
  226. * @covers Lcobucci\JWT\Parser
  227. * @covers Lcobucci\JWT\Token
  228. * @covers Lcobucci\JWT\Signature
  229. * @covers Lcobucci\JWT\Signer\Key
  230. * @covers Lcobucci\JWT\Signer\BaseSigner
  231. * @covers Lcobucci\JWT\Signer\Rsa
  232. * @covers Lcobucci\JWT\Signer\Rsa\Sha256
  233. * @covers Lcobucci\JWT\Claim\Factory
  234. * @covers Lcobucci\JWT\Claim\Basic
  235. * @covers Lcobucci\JWT\Parsing\Encoder
  236. * @covers Lcobucci\JWT\Parsing\Decoder
  237. */
  238. public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs()
  239. {
  240. $data = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJoZWxsbyI6IndvcmxkIn0.s'
  241. . 'GYbB1KrmnESNfJ4D9hOe1Zad_BMyxdb8G4p4LNP7StYlOyBWck6q7XPpPj_6gB'
  242. . 'Bo1ohD3MA2o0HY42lNIrAStaVhfsFKGdIou8TarwMGZBPcif_3ThUV1pGS3fZc'
  243. . 'lFwF2SP7rqCngQis_xcUVCyqa8E1Wa_v28grnl1QZrnmQFO8B5JGGLqcrfUHJO'
  244. . 'nJCupP-Lqh4TmIhftIimSCgLNmJg80wyrpUEfZYReE7hPuEmY0ClTqAGIMQoNS'
  245. . '98ljwDxwhfbSuL2tAdbV4DekbTpWzspe3dOJ7RSzmPKVZ6NoezaIazKqyqkmHZfcMaHI1lQeGia6LTbHU1bp0gINi74Vw';
  246. $token = (new Parser())->parse((string) $data);
  247. $this->assertEquals('world', $token->getClaim('hello'));
  248. $this->assertTrue($token->verify($this->signer, self::$rsaKeys['public']));
  249. }
  250. }