TokenTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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->createMock(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->createMock(Signer::class);
  234. $signature = $this->createMock(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->createMock(Signer::class);
  254. $signature = $this->createMock(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. * @uses Lcobucci\JWT\ValidationData::setCurrentTime
  271. *
  272. * @covers Lcobucci\JWT\Token::validate
  273. * @covers Lcobucci\JWT\Token::getValidatableClaims
  274. */
  275. public function validateShouldReturnTrueWhenClaimsAreEmpty()
  276. {
  277. $token = new Token();
  278. $this->assertTrue($token->validate(new ValidationData()));
  279. }
  280. /**
  281. * @test
  282. *
  283. * @uses Lcobucci\JWT\Token::__construct
  284. * @uses Lcobucci\JWT\ValidationData::__construct
  285. * @uses Lcobucci\JWT\ValidationData::setCurrentTime
  286. * @uses Lcobucci\JWT\Claim\Basic::__construct
  287. *
  288. * @covers Lcobucci\JWT\Token::validate
  289. * @covers Lcobucci\JWT\Token::getValidatableClaims
  290. */
  291. public function validateShouldReturnTrueWhenThereAreNoValidatableClaims()
  292. {
  293. $token = new Token([], ['testing' => new Basic('testing', 'test')]);
  294. $this->assertTrue($token->validate(new ValidationData()));
  295. }
  296. /**
  297. * @test
  298. *
  299. * @uses Lcobucci\JWT\Token::__construct
  300. * @uses Lcobucci\JWT\ValidationData
  301. * @uses Lcobucci\JWT\Claim\Basic
  302. * @uses Lcobucci\JWT\Claim\EqualsTo
  303. *
  304. * @covers Lcobucci\JWT\Token::validate
  305. * @covers Lcobucci\JWT\Token::getValidatableClaims
  306. */
  307. public function validateShouldReturnFalseWhenThereIsAtLeastOneFailedValidatableClaim()
  308. {
  309. $token = new Token(
  310. [],
  311. [
  312. 'iss' => new EqualsTo('iss', 'test'),
  313. 'testing' => new Basic('testing', 'test')
  314. ]
  315. );
  316. $data = new ValidationData();
  317. $data->setIssuer('test1');
  318. $this->assertFalse($token->validate($data));
  319. }
  320. /**
  321. * @test
  322. *
  323. * @uses Lcobucci\JWT\Token::__construct
  324. * @uses Lcobucci\JWT\ValidationData
  325. * @uses Lcobucci\JWT\Claim\Basic
  326. * @uses Lcobucci\JWT\Claim\EqualsTo
  327. * @uses Lcobucci\JWT\Claim\LesserOrEqualsTo
  328. * @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
  329. *
  330. * @covers Lcobucci\JWT\Token::validate
  331. * @covers Lcobucci\JWT\Token::getValidatableClaims
  332. */
  333. public function validateShouldReturnFalseWhenATimeBasedClaimFails()
  334. {
  335. $now = time();
  336. $token = new Token(
  337. [],
  338. [
  339. 'iss' => new EqualsTo('iss', 'test'),
  340. 'iat' => new LesserOrEqualsTo('iat', $now),
  341. 'nbf' => new LesserOrEqualsTo('nbf', $now + 20),
  342. 'exp' => new GreaterOrEqualsTo('exp', $now + 500),
  343. 'testing' => new Basic('testing', 'test')
  344. ]
  345. );
  346. $data = new ValidationData($now + 10);
  347. $data->setIssuer('test');
  348. $this->assertFalse($token->validate($data));
  349. }
  350. /**
  351. * @test
  352. *
  353. * @uses Lcobucci\JWT\Token::__construct
  354. * @uses Lcobucci\JWT\ValidationData
  355. * @uses Lcobucci\JWT\Claim\Basic
  356. * @uses Lcobucci\JWT\Claim\EqualsTo
  357. * @uses Lcobucci\JWT\Claim\LesserOrEqualsTo
  358. * @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
  359. *
  360. * @covers Lcobucci\JWT\Token::validate
  361. * @covers Lcobucci\JWT\Token::getValidatableClaims
  362. */
  363. public function validateShouldReturnTrueWhenThereAreNoFailedValidatableClaims()
  364. {
  365. $now = time();
  366. $token = new Token(
  367. [],
  368. [
  369. 'iss' => new EqualsTo('iss', 'test'),
  370. 'iat' => new LesserOrEqualsTo('iat', $now),
  371. 'exp' => new GreaterOrEqualsTo('exp', $now + 500),
  372. 'testing' => new Basic('testing', 'test')
  373. ]
  374. );
  375. $data = new ValidationData($now + 10);
  376. $data->setIssuer('test');
  377. $this->assertTrue($token->validate($data));
  378. }
  379. /**
  380. * @test
  381. *
  382. * @uses Lcobucci\JWT\Token::__construct
  383. * @uses Lcobucci\JWT\ValidationData
  384. * @uses Lcobucci\JWT\Claim\Basic
  385. * @uses Lcobucci\JWT\Claim\EqualsTo
  386. * @uses Lcobucci\JWT\Claim\LesserOrEqualsTo
  387. * @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
  388. *
  389. * @covers Lcobucci\JWT\Token::validate
  390. * @covers Lcobucci\JWT\Token::getValidatableClaims
  391. */
  392. public function validateShouldReturnTrueWhenLeewayMakesAllTimeBasedClaimsTrueAndOtherClaimsAreTrue()
  393. {
  394. $now = time();
  395. $token = new Token(
  396. [],
  397. [
  398. 'iss' => new EqualsTo('iss', 'test'),
  399. 'iat' => new LesserOrEqualsTo('iat', $now),
  400. 'nbf' => new LesserOrEqualsTo('nbf', $now + 20),
  401. 'exp' => new GreaterOrEqualsTo('exp', $now + 500),
  402. 'testing' => new Basic('testing', 'test')
  403. ]
  404. );
  405. $data = new ValidationData($now + 10, 20);
  406. $data->setIssuer('test');
  407. $this->assertTrue($token->validate($data));
  408. }
  409. /**
  410. * @test
  411. *
  412. * @covers Lcobucci\JWT\Token::isExpired
  413. *
  414. * @uses Lcobucci\JWT\Token::__construct
  415. * @uses Lcobucci\JWT\Token::getClaim
  416. * @uses Lcobucci\JWT\Token::hasClaim
  417. */
  418. public function isExpiredShouldReturnFalseWhenTokenDoesNotExpires()
  419. {
  420. $token = new Token(['alg' => 'none']);
  421. $this->assertFalse($token->isExpired());
  422. }
  423. /**
  424. * @test
  425. *
  426. * @covers Lcobucci\JWT\Token::isExpired
  427. *
  428. * @uses Lcobucci\JWT\Token::__construct
  429. * @uses Lcobucci\JWT\Token::getClaim
  430. * @uses Lcobucci\JWT\Token::hasClaim
  431. * @uses Lcobucci\JWT\Claim\Basic
  432. * @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
  433. */
  434. public function isExpiredShouldReturnFalseWhenTokenIsNotExpired()
  435. {
  436. $token = new Token(
  437. ['alg' => 'none'],
  438. ['exp' => new GreaterOrEqualsTo('exp', time() + 500)]
  439. );
  440. $this->assertFalse($token->isExpired());
  441. }
  442. /**
  443. * @test
  444. *
  445. * @covers Lcobucci\JWT\Token::isExpired
  446. *
  447. * @uses Lcobucci\JWT\Token::__construct
  448. * @uses Lcobucci\JWT\Token::getClaim
  449. * @uses Lcobucci\JWT\Token::hasClaim
  450. * @uses Lcobucci\JWT\Claim\Basic
  451. * @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
  452. */
  453. public function isExpiredShouldReturnTrueAfterTokenExpires()
  454. {
  455. $token = new Token(
  456. ['alg' => 'none'],
  457. ['exp' => new GreaterOrEqualsTo('exp', time())]
  458. );
  459. $this->assertTrue($token->isExpired(new DateTime('+10 days')));
  460. }
  461. /**
  462. * @test
  463. *
  464. * @uses Lcobucci\JWT\Token::__construct
  465. *
  466. * @covers Lcobucci\JWT\Token::getPayload
  467. */
  468. public function getPayloadShouldReturnAStringWithTheTwoEncodePartsThatGeneratedTheToken()
  469. {
  470. $token = new Token(['alg' => 'none'], [], null, ['test1', 'test2', 'test3']);
  471. $this->assertEquals('test1.test2', $token->getPayload());
  472. }
  473. /**
  474. * @test
  475. *
  476. * @uses Lcobucci\JWT\Token::__construct
  477. * @uses Lcobucci\JWT\Token::getPayload
  478. *
  479. * @covers Lcobucci\JWT\Token::__toString
  480. */
  481. public function toStringMustReturnEncodedDataWithEmptySignature()
  482. {
  483. $token = new Token(['alg' => 'none'], [], null, ['test', 'test']);
  484. $this->assertEquals('test.test.', (string) $token);
  485. }
  486. /**
  487. * @test
  488. *
  489. * @uses Lcobucci\JWT\Token::__construct
  490. * @uses Lcobucci\JWT\Token::getPayload
  491. *
  492. * @covers Lcobucci\JWT\Token::__toString
  493. */
  494. public function toStringMustReturnEncodedData()
  495. {
  496. $signature = $this->createMock(Signature::class, [], [], '', false);
  497. $token = new Token(['alg' => 'none'], [], $signature, ['test', 'test', 'test']);
  498. $this->assertEquals('test.test.test', (string) $token);
  499. }
  500. }