EcdsaTokenTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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\Parser;
  10. use Lcobucci\JWT\Signer\Key;
  11. use Lcobucci\JWT\Token;
  12. use Lcobucci\JWT\Signature;
  13. use Lcobucci\JWT\Signer\Ecdsa\Sha256;
  14. use Lcobucci\JWT\Signer\Ecdsa\Sha512;
  15. use Lcobucci\JWT\Signer\Keychain;
  16. use Lcobucci\JWT\Keys;
  17. /**
  18. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  19. * @since 2.1.0
  20. */
  21. class EcdsaTokenTest extends \PHPUnit_Framework_TestCase
  22. {
  23. use Keys;
  24. /**
  25. * @var Sha256
  26. */
  27. private $signer;
  28. /**
  29. * @before
  30. */
  31. public function createSigner()
  32. {
  33. $this->signer = new Sha256();
  34. }
  35. /**
  36. * @test
  37. *
  38. * @expectedException \InvalidArgumentException
  39. *
  40. * @covers Lcobucci\JWT\Builder
  41. * @covers Lcobucci\JWT\Token
  42. * @covers Lcobucci\JWT\Signature
  43. * @covers Lcobucci\JWT\Claim\Factory
  44. * @covers Lcobucci\JWT\Claim\Basic
  45. * @covers Lcobucci\JWT\Parsing\Encoder
  46. * @covers Lcobucci\JWT\Signer\Key
  47. * @covers Lcobucci\JWT\Signer\BaseSigner
  48. * @covers Lcobucci\JWT\Signer\Ecdsa
  49. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
  50. * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
  51. */
  52. public function builderShouldRaiseExceptionWhenKeyIsInvalid()
  53. {
  54. $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
  55. (new Builder())->setId(1)
  56. ->setAudience('http://client.abc.com')
  57. ->setIssuer('http://api.abc.com')
  58. ->set('user', $user)
  59. ->sign($this->signer, new Key('testing'));
  60. }
  61. /**
  62. * @test
  63. *
  64. * @expectedException \InvalidArgumentException
  65. *
  66. * @covers Lcobucci\JWT\Builder
  67. * @covers Lcobucci\JWT\Token
  68. * @covers Lcobucci\JWT\Signature
  69. * @covers Lcobucci\JWT\Claim\Factory
  70. * @covers Lcobucci\JWT\Claim\Basic
  71. * @covers Lcobucci\JWT\Parsing\Encoder
  72. * @covers Lcobucci\JWT\Signer\Key
  73. * @covers Lcobucci\JWT\Signer\BaseSigner
  74. * @covers Lcobucci\JWT\Signer\Ecdsa
  75. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
  76. * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
  77. */
  78. public function builderShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible()
  79. {
  80. $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
  81. (new Builder())->setId(1)
  82. ->setAudience('http://client.abc.com')
  83. ->setIssuer('http://api.abc.com')
  84. ->set('user', $user)
  85. ->sign($this->signer, static::$rsaKeys['private']);
  86. }
  87. /**
  88. * @test
  89. *
  90. * @covers Lcobucci\JWT\Builder
  91. * @covers Lcobucci\JWT\Token
  92. * @covers Lcobucci\JWT\Signature
  93. * @covers Lcobucci\JWT\Claim\Factory
  94. * @covers Lcobucci\JWT\Claim\Basic
  95. * @covers Lcobucci\JWT\Parsing\Encoder
  96. * @covers Lcobucci\JWT\Signer\Key
  97. * @covers Lcobucci\JWT\Signer\BaseSigner
  98. * @covers Lcobucci\JWT\Signer\Ecdsa
  99. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
  100. * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
  101. */
  102. public function builderCanGenerateAToken()
  103. {
  104. $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
  105. $token = (new Builder())->setId(1)
  106. ->setAudience('http://client.abc.com')
  107. ->setIssuer('http://api.abc.com')
  108. ->set('user', $user)
  109. ->setHeader('jki', '1234')
  110. ->sign($this->signer, static::$ecdsaKeys['private'])
  111. ->getToken();
  112. $this->assertAttributeInstanceOf(Signature::class, 'signature', $token);
  113. $this->assertEquals('1234', $token->getHeader('jki'));
  114. $this->assertEquals('http://client.abc.com', $token->getClaim('aud'));
  115. $this->assertEquals('http://api.abc.com', $token->getClaim('iss'));
  116. $this->assertEquals($user, $token->getClaim('user'));
  117. return $token;
  118. }
  119. /**
  120. * @test
  121. *
  122. * @depends builderCanGenerateAToken
  123. *
  124. * @covers Lcobucci\JWT\Builder
  125. * @covers Lcobucci\JWT\Parser
  126. * @covers Lcobucci\JWT\Token
  127. * @covers Lcobucci\JWT\Signature
  128. * @covers Lcobucci\JWT\Claim\Factory
  129. * @covers Lcobucci\JWT\Claim\Basic
  130. * @covers Lcobucci\JWT\Parsing\Encoder
  131. * @covers Lcobucci\JWT\Parsing\Decoder
  132. * @covers Lcobucci\JWT\Signer\Ecdsa
  133. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
  134. */
  135. public function parserCanReadAToken(Token $generated)
  136. {
  137. $read = (new Parser())->parse((string) $generated);
  138. $this->assertEquals($generated, $read);
  139. $this->assertEquals('testing', $read->getClaim('user')->name);
  140. }
  141. /**
  142. * @test
  143. *
  144. * @depends builderCanGenerateAToken
  145. *
  146. * @covers Lcobucci\JWT\Builder
  147. * @covers Lcobucci\JWT\Parser
  148. * @covers Lcobucci\JWT\Token
  149. * @covers Lcobucci\JWT\Signature
  150. * @covers Lcobucci\JWT\Parsing\Encoder
  151. * @covers Lcobucci\JWT\Claim\Factory
  152. * @covers Lcobucci\JWT\Claim\Basic
  153. * @covers Lcobucci\JWT\Signer\Key
  154. * @covers Lcobucci\JWT\Signer\BaseSigner
  155. * @covers Lcobucci\JWT\Signer\Ecdsa
  156. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
  157. * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
  158. */
  159. public function verifyShouldReturnFalseWhenKeyIsNotRight(Token $token)
  160. {
  161. $this->assertFalse($token->verify($this->signer, static::$ecdsaKeys['public2']));
  162. }
  163. /**
  164. * @test
  165. *
  166. * @depends builderCanGenerateAToken
  167. *
  168. * @covers Lcobucci\JWT\Builder
  169. * @covers Lcobucci\JWT\Parser
  170. * @covers Lcobucci\JWT\Token
  171. * @covers Lcobucci\JWT\Signature
  172. * @covers Lcobucci\JWT\Parsing\Encoder
  173. * @covers Lcobucci\JWT\Claim\Factory
  174. * @covers Lcobucci\JWT\Claim\Basic
  175. * @covers Lcobucci\JWT\Signer\Key
  176. * @covers Lcobucci\JWT\Signer\BaseSigner
  177. * @covers Lcobucci\JWT\Signer\Ecdsa
  178. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
  179. * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
  180. * @covers Lcobucci\JWT\Signer\Ecdsa\Sha512
  181. */
  182. public function verifyShouldReturnFalseWhenAlgorithmIsDifferent(Token $token)
  183. {
  184. $this->assertFalse($token->verify(new Sha512(), static::$ecdsaKeys['public1']));
  185. }
  186. /**
  187. * @test
  188. *
  189. * @expectedException \RuntimeException
  190. *
  191. * @depends builderCanGenerateAToken
  192. *
  193. * @covers Lcobucci\JWT\Builder
  194. * @covers Lcobucci\JWT\Parser
  195. * @covers Lcobucci\JWT\Token
  196. * @covers Lcobucci\JWT\Signature
  197. * @covers Lcobucci\JWT\Parsing\Encoder
  198. * @covers Lcobucci\JWT\Claim\Factory
  199. * @covers Lcobucci\JWT\Claim\Basic
  200. * @covers Lcobucci\JWT\Signer\Key
  201. * @covers Lcobucci\JWT\Signer\BaseSigner
  202. * @covers Lcobucci\JWT\Signer\Ecdsa
  203. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
  204. * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
  205. */
  206. public function verifyShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible(Token $token)
  207. {
  208. $this->assertFalse($token->verify($this->signer, static::$rsaKeys['public']));
  209. }
  210. /**
  211. * @test
  212. *
  213. * @depends builderCanGenerateAToken
  214. *
  215. * @covers Lcobucci\JWT\Builder
  216. * @covers Lcobucci\JWT\Parser
  217. * @covers Lcobucci\JWT\Token
  218. * @covers Lcobucci\JWT\Signature
  219. * @covers Lcobucci\JWT\Parsing\Encoder
  220. * @covers Lcobucci\JWT\Claim\Factory
  221. * @covers Lcobucci\JWT\Claim\Basic
  222. * @covers Lcobucci\JWT\Signer\Key
  223. * @covers Lcobucci\JWT\Signer\BaseSigner
  224. * @covers Lcobucci\JWT\Signer\Ecdsa
  225. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
  226. * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
  227. */
  228. public function verifyShouldReturnTrueWhenKeyIsRight(Token $token)
  229. {
  230. $this->assertTrue($token->verify($this->signer, static::$ecdsaKeys['public1']));
  231. }
  232. /**
  233. * @test
  234. *
  235. * @covers Lcobucci\JWT\Builder
  236. * @covers Lcobucci\JWT\Token
  237. * @covers Lcobucci\JWT\Signature
  238. * @covers Lcobucci\JWT\Claim\Factory
  239. * @covers Lcobucci\JWT\Claim\Basic
  240. * @covers Lcobucci\JWT\Parsing\Encoder
  241. * @covers Lcobucci\JWT\Signer\Key
  242. * @covers Lcobucci\JWT\Signer\BaseSigner
  243. * @covers Lcobucci\JWT\Signer\Ecdsa
  244. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
  245. * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
  246. */
  247. public function everythingShouldWorkWithAKeyWithParams()
  248. {
  249. $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
  250. $token = (new Builder())->setId(1)
  251. ->setAudience('http://client.abc.com')
  252. ->setIssuer('http://api.abc.com')
  253. ->set('user', $user)
  254. ->setHeader('jki', '1234')
  255. ->sign($this->signer, static::$ecdsaKeys['private-params'])
  256. ->getToken();
  257. $this->assertTrue($token->verify($this->signer, static::$ecdsaKeys['public-params']));
  258. }
  259. /**
  260. * @test
  261. *
  262. * @covers Lcobucci\JWT\Builder
  263. * @covers Lcobucci\JWT\Parser
  264. * @covers Lcobucci\JWT\Token
  265. * @covers Lcobucci\JWT\Signature
  266. * @covers Lcobucci\JWT\Signer\Key
  267. * @covers Lcobucci\JWT\Signer\BaseSigner
  268. * @covers Lcobucci\JWT\Signer\Ecdsa
  269. * @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
  270. * @covers Lcobucci\JWT\Signer\Ecdsa\Sha512
  271. * @covers Lcobucci\JWT\Signer\Keychain
  272. * @covers Lcobucci\JWT\Claim\Factory
  273. * @covers Lcobucci\JWT\Claim\Basic
  274. * @covers Lcobucci\JWT\Parsing\Encoder
  275. * @covers Lcobucci\JWT\Parsing\Decoder
  276. */
  277. public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs()
  278. {
  279. $data = 'eyJhbGciOiJFUzUxMiIsInR5cCI6IkpXVCJ9.eyJoZWxsbyI6IndvcmxkIn0.'
  280. . 'AQx1MqdTni6KuzfOoedg2-7NUiwe-b88SWbdmviz40GTwrM0Mybp1i1tVtm'
  281. . 'TSQ91oEXGXBdtwsN6yalzP9J-sp2YATX_Tv4h-BednbdSvYxZsYnUoZ--ZU'
  282. . 'dL10t7g8Yt3y9hdY_diOjIptcha6ajX8yzkDGYG42iSe3f5LywSuD6FO5c';
  283. $key = '-----BEGIN PUBLIC KEY-----' . PHP_EOL
  284. . 'MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQAcpkss6wI7PPlxj3t7A1RqMH3nvL4' . PHP_EOL
  285. . 'L5Tzxze/XeeYZnHqxiX+gle70DlGRMqqOq+PJ6RYX7vK0PJFdiAIXlyPQq0B3KaU' . PHP_EOL
  286. . 'e86IvFeQSFrJdCc0K8NfiH2G1loIk3fiR+YLqlXk6FAeKtpXJKxR1pCQCAM+vBCs' . PHP_EOL
  287. . 'mZudf1zCUZ8/4eodlHU=' . PHP_EOL
  288. . '-----END PUBLIC KEY-----';
  289. $keychain = new Keychain();
  290. $token = (new Parser())->parse((string) $data);
  291. $this->assertEquals('world', $token->getClaim('hello'));
  292. $this->assertTrue($token->verify(new Sha512(), $keychain->getPublicKey($key)));
  293. }
  294. }