TokenTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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;
  8. use DateInterval;
  9. use DateTime;
  10. use Lcobucci\JWT\Claim\Basic;
  11. use Lcobucci\JWT\Claim\EqualsTo;
  12. use Lcobucci\JWT\Claim\GreaterOrEqualsTo;
  13. use Lcobucci\JWT\Claim\LesserOrEqualsTo;
  14. /**
  15. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  16. * @since 0.1.0
  17. */
  18. class TokenTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @test
  22. *
  23. * @covers Lcobucci\JWT\Token::__construct
  24. */
  25. public function constructMustInitializeAnEmptyPlainTextTokenWhenNoArgumentsArePassed()
  26. {
  27. $token = new Token();
  28. $this->assertAttributeEquals(['alg' => 'none'], 'headers', $token);
  29. $this->assertAttributeEquals([], 'claims', $token);
  30. $this->assertAttributeEquals(null, 'signature', $token);
  31. $this->assertAttributeEquals(['', ''], 'payload', $token);
  32. }
  33. /**
  34. * @test
  35. *
  36. * @uses Lcobucci\JWT\Token::__construct
  37. *
  38. * @covers Lcobucci\JWT\Token::hasHeader
  39. */
  40. public function hasHeaderMustReturnTrueWhenItIsConfigured()
  41. {
  42. $token = new Token(['test' => 'testing']);
  43. $this->assertTrue($token->hasHeader('test'));
  44. }
  45. /**
  46. * @test
  47. *
  48. * @uses Lcobucci\JWT\Token::__construct
  49. *
  50. * @covers Lcobucci\JWT\Token::hasHeader
  51. */
  52. public function hasHeaderMustReturnFalseWhenItIsNotConfigured()
  53. {
  54. $token = new Token(['test' => 'testing']);
  55. $this->assertFalse($token->hasHeader('testing'));
  56. }
  57. /**
  58. * @test
  59. *
  60. * @uses Lcobucci\JWT\Token::__construct
  61. * @uses Lcobucci\JWT\Token::hasHeader
  62. *
  63. * @covers Lcobucci\JWT\Token::getHeader
  64. *
  65. * @expectedException \OutOfBoundsException
  66. */
  67. public function getHeaderMustRaiseExceptionWhenHeaderIsNotConfigured()
  68. {
  69. $token = new Token(['test' => 'testing']);
  70. $token->getHeader('testing');
  71. }
  72. /**
  73. * @test
  74. *
  75. * @uses Lcobucci\JWT\Token::__construct
  76. * @uses Lcobucci\JWT\Token::hasHeader
  77. *
  78. * @covers Lcobucci\JWT\Token::getHeader
  79. */
  80. public function getHeaderMustReturnTheDefaultValueWhenIsNotConfigured()
  81. {
  82. $token = new Token(['test' => 'testing']);
  83. $this->assertEquals('blah', $token->getHeader('testing', 'blah'));
  84. }
  85. /**
  86. * @test
  87. *
  88. * @uses Lcobucci\JWT\Token::__construct
  89. * @uses Lcobucci\JWT\Token::hasHeader
  90. *
  91. * @covers Lcobucci\JWT\Token::getHeader
  92. * @covers Lcobucci\JWT\Token::getHeaderValue
  93. */
  94. public function getHeaderMustReturnTheRequestedHeader()
  95. {
  96. $token = new Token(['test' => 'testing']);
  97. $this->assertEquals('testing', $token->getHeader('test'));
  98. }
  99. /**
  100. * @test
  101. *
  102. * @uses Lcobucci\JWT\Token::__construct
  103. * @uses Lcobucci\JWT\Token::hasHeader
  104. * @uses Lcobucci\JWT\Claim\Basic
  105. *
  106. * @covers Lcobucci\JWT\Token::getHeader
  107. * @covers Lcobucci\JWT\Token::getHeaderValue
  108. */
  109. public function getHeaderMustReturnValueWhenItIsAReplicatedClaim()
  110. {
  111. $token = new Token(['jti' => new EqualsTo('jti', 1)]);
  112. $this->assertEquals(1, $token->getHeader('jti'));
  113. }
  114. /**
  115. * @test
  116. *
  117. * @uses Lcobucci\JWT\Token::__construct
  118. *
  119. * @covers Lcobucci\JWT\Token::getHeaders
  120. */
  121. public function getHeadersMustReturnTheConfiguredHeader()
  122. {
  123. $token = new Token(['test' => 'testing']);
  124. $this->assertEquals(['test' => 'testing'], $token->getHeaders());
  125. }
  126. /**
  127. * @test
  128. *
  129. * @uses Lcobucci\JWT\Token::__construct
  130. *
  131. * @covers Lcobucci\JWT\Token::getClaims
  132. */
  133. public function getClaimsMustReturnTheConfiguredClaims()
  134. {
  135. $token = new Token([], ['test' => 'testing']);
  136. $this->assertEquals(['test' => 'testing'], $token->getClaims());
  137. }
  138. /**
  139. * @test
  140. *
  141. * @uses Lcobucci\JWT\Token::__construct
  142. * @uses Lcobucci\JWT\Claim\Basic
  143. *
  144. * @covers Lcobucci\JWT\Token::hasClaim
  145. */
  146. public function hasClaimMustReturnTrueWhenItIsConfigured()
  147. {
  148. $token = new Token([], ['test' => new Basic('test', 'testing')]);
  149. $this->assertTrue($token->hasClaim('test'));
  150. }
  151. /**
  152. * @test
  153. *
  154. * @uses Lcobucci\JWT\Token::__construct
  155. * @uses Lcobucci\JWT\Claim\Basic
  156. *
  157. * @covers Lcobucci\JWT\Token::hasClaim
  158. */
  159. public function hasClaimMustReturnFalseWhenItIsNotConfigured()
  160. {
  161. $token = new Token([], ['test' => new Basic('test', 'testing')]);
  162. $this->assertFalse($token->hasClaim('testing'));
  163. }
  164. /**
  165. * @test
  166. *
  167. * @uses Lcobucci\JWT\Token::__construct
  168. * @uses Lcobucci\JWT\Token::hasClaim
  169. * @uses Lcobucci\JWT\Claim\Basic
  170. *
  171. * @covers Lcobucci\JWT\Token::getClaim
  172. */
  173. public function getClaimMustReturnTheDefaultValueWhenIsNotConfigured()
  174. {
  175. $token = new Token([], ['test' => new Basic('test', 'testing')]);
  176. $this->assertEquals('blah', $token->getClaim('testing', 'blah'));
  177. }
  178. /**
  179. * @test
  180. *
  181. * @uses Lcobucci\JWT\Token::__construct
  182. * @uses Lcobucci\JWT\Token::hasClaim
  183. * @uses Lcobucci\JWT\Claim\Basic
  184. *
  185. * @covers Lcobucci\JWT\Token::getClaim
  186. *
  187. * @expectedException \OutOfBoundsException
  188. */
  189. public function getClaimShouldRaiseExceptionWhenClaimIsNotConfigured()
  190. {
  191. $token = new Token();
  192. $token->getClaim('testing');
  193. }
  194. /**
  195. * @test
  196. *
  197. * @uses Lcobucci\JWT\Token::__construct
  198. * @uses Lcobucci\JWT\Token::hasClaim
  199. * @uses Lcobucci\JWT\Claim\Basic
  200. *
  201. * @covers Lcobucci\JWT\Token::getClaim
  202. */
  203. public function getClaimShouldReturnTheClaimValueWhenItExists()
  204. {
  205. $token = new Token([], ['testing' => new Basic('testing', 'test')]);
  206. $this->assertEquals('test', $token->getClaim('testing'));
  207. }
  208. /**
  209. * @test
  210. *
  211. * @uses Lcobucci\JWT\Token::__construct
  212. *
  213. * @covers Lcobucci\JWT\Token::verify
  214. *
  215. * @expectedException BadMethodCallException
  216. */
  217. public function verifyMustRaiseExceptionWhenTokenIsUnsigned()
  218. {
  219. $signer = $this->getMock(Signer::class);
  220. $token = new Token();
  221. $token->verify($signer, 'test');
  222. }
  223. /**
  224. * @test
  225. *
  226. * @uses Lcobucci\JWT\Token::__construct
  227. *
  228. * @covers Lcobucci\JWT\Token::verify
  229. * @covers Lcobucci\JWT\Token::getPayload
  230. */
  231. public function verifyShouldReturnFalseWhenTokenAlgorithmIsDifferent()
  232. {
  233. $signer = $this->getMock(Signer::class);
  234. $signature = $this->getMock(Signature::class, [], [], '', false);
  235. $signer->expects($this->any())
  236. ->method('getAlgorithmId')
  237. ->willReturn('HS256');
  238. $signature->expects($this->never())
  239. ->method('verify');
  240. $token = new Token(['alg' => 'RS256'], [], $signature);
  241. $this->assertFalse($token->verify($signer, 'test'));
  242. }
  243. /**
  244. * @test
  245. *
  246. * @uses Lcobucci\JWT\Token::__construct
  247. *
  248. * @covers Lcobucci\JWT\Token::verify
  249. * @covers Lcobucci\JWT\Token::getPayload
  250. */
  251. public function verifyMustDelegateTheValidationToSignature()
  252. {
  253. $signer = $this->getMock(Signer::class);
  254. $signature = $this->getMock(Signature::class, [], [], '', false);
  255. $signer->expects($this->any())
  256. ->method('getAlgorithmId')
  257. ->willReturn('HS256');
  258. $signature->expects($this->once())
  259. ->method('verify')
  260. ->with($signer, $this->isType('string'), 'test')
  261. ->willReturn(true);
  262. $token = new Token(['alg' => 'HS256'], [], $signature);
  263. $this->assertTrue($token->verify($signer, 'test'));
  264. }
  265. /**
  266. * @test
  267. *
  268. * @uses Lcobucci\JWT\Token::__construct
  269. * @uses Lcobucci\JWT\ValidationData::__construct
  270. *
  271. * @covers Lcobucci\JWT\Token::validate
  272. * @covers Lcobucci\JWT\Token::getValidatableClaims
  273. */
  274. public function validateShouldReturnTrueWhenClaimsAreEmpty()
  275. {
  276. $token = new Token();
  277. $this->assertTrue($token->validate(new ValidationData()));
  278. }
  279. /**
  280. * @test
  281. *
  282. * @uses Lcobucci\JWT\Token::__construct
  283. * @uses Lcobucci\JWT\ValidationData::__construct
  284. * @uses Lcobucci\JWT\Claim\Basic::__construct
  285. *
  286. * @covers Lcobucci\JWT\Token::validate
  287. * @covers Lcobucci\JWT\Token::getValidatableClaims
  288. */
  289. public function validateShouldReturnTrueWhenThereAreNoValidatableClaims()
  290. {
  291. $token = new Token([], ['testing' => new Basic('testing', 'test')]);
  292. $this->assertTrue($token->validate(new ValidationData()));
  293. }
  294. /**
  295. * @test
  296. *
  297. * @uses Lcobucci\JWT\Token::__construct
  298. * @uses Lcobucci\JWT\ValidationData
  299. * @uses Lcobucci\JWT\Claim\Basic
  300. * @uses Lcobucci\JWT\Claim\EqualsTo
  301. *
  302. * @covers Lcobucci\JWT\Token::validate
  303. * @covers Lcobucci\JWT\Token::getValidatableClaims
  304. */
  305. public function validateShouldReturnFalseWhenThereIsAtLeastOneFailedValidatableClaim()
  306. {
  307. $token = new Token(
  308. [],
  309. [
  310. 'iss' => new EqualsTo('iss', 'test'),
  311. 'testing' => new Basic('testing', 'test')
  312. ]
  313. );
  314. $data = new ValidationData();
  315. $data->setIssuer('test1');
  316. $this->assertFalse($token->validate($data));
  317. }
  318. /**
  319. * @test
  320. *
  321. * @uses Lcobucci\JWT\Token::__construct
  322. * @uses Lcobucci\JWT\ValidationData
  323. * @uses Lcobucci\JWT\Claim\Basic
  324. * @uses Lcobucci\JWT\Claim\EqualsTo
  325. * @uses Lcobucci\JWT\Claim\LesserOrEqualsTo
  326. * @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
  327. *
  328. * @covers Lcobucci\JWT\Token::validate
  329. * @covers Lcobucci\JWT\Token::getValidatableClaims
  330. */
  331. public function validateShouldReturnTrueWhenThereAreNoFailedValidatableClaims()
  332. {
  333. $now = time();
  334. $token = new Token(
  335. [],
  336. [
  337. 'iss' => new EqualsTo('iss', 'test'),
  338. 'iat' => new LesserOrEqualsTo('iat', $now),
  339. 'exp' => new GreaterOrEqualsTo('exp', $now + 500),
  340. 'testing' => new Basic('testing', 'test')
  341. ]
  342. );
  343. $data = new ValidationData($now + 10);
  344. $data->setIssuer('test');
  345. $this->assertTrue($token->validate($data));
  346. }
  347. /**
  348. * @test
  349. *
  350. * @covers Lcobucci\JWT\Token::isExpired
  351. *
  352. * @uses Lcobucci\JWT\Token::__construct
  353. * @uses Lcobucci\JWT\Token::getClaim
  354. * @uses Lcobucci\JWT\Token::hasClaim
  355. */
  356. public function isExpiredShouldReturnFalseWhenTokenDoesNotExpires()
  357. {
  358. $token = new Token(['alg' => 'none']);
  359. $this->assertFalse($token->isExpired());
  360. }
  361. /**
  362. * @test
  363. *
  364. * @covers Lcobucci\JWT\Token::isExpired
  365. *
  366. * @uses Lcobucci\JWT\Token::__construct
  367. * @uses Lcobucci\JWT\Token::getClaim
  368. * @uses Lcobucci\JWT\Token::hasClaim
  369. * @uses Lcobucci\JWT\Claim\Basic
  370. * @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
  371. */
  372. public function isExpiredShouldReturnFalseWhenTokenIsNotExpired()
  373. {
  374. $token = new Token(
  375. ['alg' => 'none'],
  376. ['exp' => new GreaterOrEqualsTo('exp', time() + 500)]
  377. );
  378. $this->assertFalse($token->isExpired());
  379. }
  380. /**
  381. * @test
  382. *
  383. * @covers Lcobucci\JWT\Token::isExpired
  384. *
  385. * @uses Lcobucci\JWT\Token::__construct
  386. * @uses Lcobucci\JWT\Token::getClaim
  387. * @uses Lcobucci\JWT\Token::hasClaim
  388. * @uses Lcobucci\JWT\Claim\Basic
  389. * @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
  390. */
  391. public function isExpiredShouldReturnTrueAfterTokenExpires()
  392. {
  393. $token = new Token(
  394. ['alg' => 'none'],
  395. ['exp' => new GreaterOrEqualsTo('exp', time())]
  396. );
  397. $this->assertTrue($token->isExpired(new DateTime('+10 days')));
  398. }
  399. /**
  400. * @test
  401. *
  402. * @uses Lcobucci\JWT\Token::__construct
  403. *
  404. * @covers Lcobucci\JWT\Token::getPayload
  405. */
  406. public function getPayloadShouldReturnAStringWithTheTwoEncodePartsThatGeneratedTheToken()
  407. {
  408. $token = new Token(['alg' => 'none'], [], null, ['test1', 'test2', 'test3']);
  409. $this->assertEquals('test1.test2', $token->getPayload());
  410. }
  411. /**
  412. * @test
  413. *
  414. * @uses Lcobucci\JWT\Token::__construct
  415. * @uses Lcobucci\JWT\Token::getPayload
  416. *
  417. * @covers Lcobucci\JWT\Token::__toString
  418. */
  419. public function toStringMustReturnEncodedDataWithEmptySignature()
  420. {
  421. $token = new Token(['alg' => 'none'], [], null, ['test', 'test']);
  422. $this->assertEquals('test.test.', (string) $token);
  423. }
  424. /**
  425. * @test
  426. *
  427. * @uses Lcobucci\JWT\Token::__construct
  428. * @uses Lcobucci\JWT\Token::getPayload
  429. *
  430. * @covers Lcobucci\JWT\Token::__toString
  431. */
  432. public function toStringMustReturnEncodedData()
  433. {
  434. $signature = $this->getMock(Signature::class, [], [], '', false);
  435. $token = new Token(['alg' => 'none'], [], $signature, ['test', 'test', 'test']);
  436. $this->assertEquals('test.test.test', (string) $token);
  437. }
  438. }