ParserTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 Lcobucci\JWT\Claim\Factory as ClaimFactory;
  9. use Lcobucci\JWT\Parsing\Decoder;
  10. use RuntimeException;
  11. /**
  12. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  13. * @since 0.1.0
  14. */
  15. class ParserTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var Decoder|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $decoder;
  21. /**
  22. * @var ClaimFactory|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $claimFactory;
  25. /**
  26. * @var Claim|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $defaultClaim;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function setUp()
  33. {
  34. $this->decoder = $this->getMock(Decoder::class);
  35. $this->claimFactory = $this->getMock(ClaimFactory::class, [], [], '', false);
  36. $this->defaultClaim = $this->getMock(Claim::class);
  37. $this->claimFactory->expects($this->any())
  38. ->method('create')
  39. ->willReturn($this->defaultClaim);
  40. }
  41. /**
  42. * @return Parser
  43. */
  44. private function createParser()
  45. {
  46. return new Parser($this->decoder, $this->claimFactory);
  47. }
  48. /**
  49. * @test
  50. *
  51. * @covers Lcobucci\JWT\Parser::__construct
  52. */
  53. public function constructMustConfigureTheAttributes()
  54. {
  55. $parser = $this->createParser();
  56. $this->assertAttributeSame($this->decoder, 'decoder', $parser);
  57. $this->assertAttributeSame($this->claimFactory, 'claimFactory', $parser);
  58. }
  59. /**
  60. * @test
  61. *
  62. * @uses Lcobucci\JWT\Parser::__construct
  63. *
  64. * @covers Lcobucci\JWT\Parser::parse
  65. * @covers Lcobucci\JWT\Parser::splitJwt
  66. *
  67. * @expectedException InvalidArgumentException
  68. */
  69. public function parseMustRaiseExceptionWhenJWSIsNotAString()
  70. {
  71. $parser = $this->createParser();
  72. $parser->parse(['asdasd']);
  73. }
  74. /**
  75. * @test
  76. *
  77. * @uses Lcobucci\JWT\Parser::__construct
  78. *
  79. * @covers Lcobucci\JWT\Parser::parse
  80. * @covers Lcobucci\JWT\Parser::splitJwt
  81. *
  82. * @expectedException InvalidArgumentException
  83. */
  84. public function parseMustRaiseExceptionWhenJWSDontHaveThreeParts()
  85. {
  86. $parser = $this->createParser();
  87. $parser->parse('');
  88. }
  89. /**
  90. * @test
  91. *
  92. * @uses Lcobucci\JWT\Parser::__construct
  93. *
  94. * @covers Lcobucci\JWT\Parser::parse
  95. * @covers Lcobucci\JWT\Parser::splitJwt
  96. * @covers Lcobucci\JWT\Parser::parseHeader
  97. *
  98. * @expectedException RuntimeException
  99. */
  100. public function parseMustRaiseExceptionWhenHeaderCannotBeDecoded()
  101. {
  102. $this->decoder->expects($this->any())
  103. ->method('jsonDecode')
  104. ->willThrowException(new RuntimeException());
  105. $parser = $this->createParser();
  106. $parser->parse('asdfad.asdfasdf.');
  107. }
  108. /**
  109. * @test
  110. *
  111. * @uses Lcobucci\JWT\Parser::__construct
  112. *
  113. * @covers Lcobucci\JWT\Parser::parse
  114. * @covers Lcobucci\JWT\Parser::splitJwt
  115. * @covers Lcobucci\JWT\Parser::parseHeader
  116. *
  117. * @expectedException InvalidArgumentException
  118. */
  119. public function parseMustRaiseExceptionWhenHeaderIsFromAnEncryptedToken()
  120. {
  121. $this->decoder->expects($this->any())
  122. ->method('jsonDecode')
  123. ->willReturn(['enc' => 'AAA']);
  124. $parser = $this->createParser();
  125. $parser->parse('a.a.');
  126. }
  127. /**
  128. * @test
  129. *
  130. * @uses Lcobucci\JWT\Parser::__construct
  131. * @uses Lcobucci\JWT\Token::__construct
  132. *
  133. * @covers Lcobucci\JWT\Parser::parse
  134. * @covers Lcobucci\JWT\Parser::splitJwt
  135. * @covers Lcobucci\JWT\Parser::parseHeader
  136. * @covers Lcobucci\JWT\Parser::parseClaims
  137. * @covers Lcobucci\JWT\Parser::parseSignature
  138. *
  139. */
  140. public function parseMustReturnANonSignedTokenWhenSignatureIsNotInformed()
  141. {
  142. $this->decoder->expects($this->at(1))
  143. ->method('jsonDecode')
  144. ->willReturn(['typ' => 'JWT', 'alg' => 'none']);
  145. $this->decoder->expects($this->at(3))
  146. ->method('jsonDecode')
  147. ->willReturn(['aud' => 'test']);
  148. $parser = $this->createParser();
  149. $token = $parser->parse('a.a.');
  150. $this->assertAttributeEquals(['typ' => 'JWT', 'alg' => 'none'], 'headers', $token);
  151. $this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
  152. $this->assertAttributeEquals(null, 'signature', $token);
  153. }
  154. /**
  155. * @test
  156. *
  157. * @uses Lcobucci\JWT\Parser::__construct
  158. * @uses Lcobucci\JWT\Token::__construct
  159. *
  160. * @covers Lcobucci\JWT\Parser::parse
  161. * @covers Lcobucci\JWT\Parser::splitJwt
  162. * @covers Lcobucci\JWT\Parser::parseHeader
  163. * @covers Lcobucci\JWT\Parser::parseClaims
  164. * @covers Lcobucci\JWT\Parser::parseSignature
  165. */
  166. public function parseShouldReplicateClaimValueOnHeaderWhenNeeded()
  167. {
  168. $this->decoder->expects($this->at(1))
  169. ->method('jsonDecode')
  170. ->willReturn(['typ' => 'JWT', 'alg' => 'none', 'aud' => 'test']);
  171. $this->decoder->expects($this->at(3))
  172. ->method('jsonDecode')
  173. ->willReturn(['aud' => 'test']);
  174. $parser = $this->createParser();
  175. $token = $parser->parse('a.a.');
  176. $this->assertAttributeEquals(
  177. ['typ' => 'JWT', 'alg' => 'none', 'aud' => $this->defaultClaim],
  178. 'headers',
  179. $token
  180. );
  181. $this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
  182. $this->assertAttributeEquals(null, 'signature', $token);
  183. }
  184. /**
  185. * @test
  186. *
  187. * @uses Lcobucci\JWT\Parser::__construct
  188. * @uses Lcobucci\JWT\Token::__construct
  189. * @uses Lcobucci\JWT\Signature::__construct
  190. *
  191. * @covers Lcobucci\JWT\Parser::parse
  192. * @covers Lcobucci\JWT\Parser::splitJwt
  193. * @covers Lcobucci\JWT\Parser::parseHeader
  194. * @covers Lcobucci\JWT\Parser::parseClaims
  195. * @covers Lcobucci\JWT\Parser::parseSignature
  196. */
  197. public function parseMustReturnASignedTokenWhenSignatureIsInformed()
  198. {
  199. $this->decoder->expects($this->at(1))
  200. ->method('jsonDecode')
  201. ->willReturn(['typ' => 'JWT', 'alg' => 'HS256']);
  202. $this->decoder->expects($this->at(3))
  203. ->method('jsonDecode')
  204. ->willReturn(['aud' => 'test']);
  205. $this->decoder->expects($this->at(4))
  206. ->method('base64UrlDecode')
  207. ->willReturn('aaa');
  208. $parser = $this->createParser();
  209. $token = $parser->parse('a.a.a');
  210. $this->assertAttributeEquals(['typ' => 'JWT', 'alg' => 'HS256'], 'headers', $token);
  211. $this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
  212. $this->assertAttributeEquals(new Signature('aaa'), 'signature', $token);
  213. }
  214. }