HmacTokenTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\Token;
  11. use Lcobucci\JWT\Signature;
  12. use Lcobucci\JWT\Signer\Hmac\Sha256;
  13. use Lcobucci\JWT\Signer\Hmac\Sha512;
  14. /**
  15. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  16. * @since 2.1.0
  17. */
  18. class HmacTokenTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var Sha256
  22. */
  23. private $signer;
  24. /**
  25. * @before
  26. */
  27. public function createSigner()
  28. {
  29. $this->signer = new Sha256();
  30. }
  31. /**
  32. * @test
  33. *
  34. * @covers Lcobucci\JWT\Builder
  35. * @covers Lcobucci\JWT\Token
  36. * @covers Lcobucci\JWT\Signature
  37. * @covers Lcobucci\JWT\Claim\Factory
  38. * @covers Lcobucci\JWT\Claim\Basic
  39. * @covers Lcobucci\JWT\Parsing\Encoder
  40. * @covers Lcobucci\JWT\Signer\Key
  41. * @covers Lcobucci\JWT\Signer\BaseSigner
  42. * @covers Lcobucci\JWT\Signer\Hmac
  43. * @covers Lcobucci\JWT\Signer\Hmac\Sha256
  44. */
  45. public function builderCanGenerateAToken()
  46. {
  47. $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
  48. $token = (new Builder())->setId(1)
  49. ->setAudience('http://client.abc.com')
  50. ->setIssuer('http://api.abc.com')
  51. ->set('user', $user)
  52. ->setHeader('jki', '1234')
  53. ->sign($this->signer, 'testing')
  54. ->getToken();
  55. $this->assertAttributeInstanceOf(Signature::class, 'signature', $token);
  56. $this->assertEquals('1234', $token->getHeader('jki'));
  57. $this->assertEquals('http://client.abc.com', $token->getClaim('aud'));
  58. $this->assertEquals('http://api.abc.com', $token->getClaim('iss'));
  59. $this->assertEquals($user, $token->getClaim('user'));
  60. return $token;
  61. }
  62. /**
  63. * @test
  64. *
  65. * @depends builderCanGenerateAToken
  66. *
  67. * @covers Lcobucci\JWT\Builder
  68. * @covers Lcobucci\JWT\Parser
  69. * @covers Lcobucci\JWT\Token
  70. * @covers Lcobucci\JWT\Signature
  71. * @covers Lcobucci\JWT\Claim\Factory
  72. * @covers Lcobucci\JWT\Claim\Basic
  73. * @covers Lcobucci\JWT\Parsing\Encoder
  74. * @covers Lcobucci\JWT\Parsing\Decoder
  75. */
  76. public function parserCanReadAToken(Token $generated)
  77. {
  78. $read = (new Parser())->parse((string) $generated);
  79. $this->assertEquals($generated, $read);
  80. $this->assertEquals('testing', $read->getClaim('user')->name);
  81. }
  82. /**
  83. * @test
  84. *
  85. * @depends builderCanGenerateAToken
  86. *
  87. * @covers Lcobucci\JWT\Builder
  88. * @covers Lcobucci\JWT\Parser
  89. * @covers Lcobucci\JWT\Token
  90. * @covers Lcobucci\JWT\Signature
  91. * @covers Lcobucci\JWT\Parsing\Encoder
  92. * @covers Lcobucci\JWT\Claim\Factory
  93. * @covers Lcobucci\JWT\Claim\Basic
  94. * @covers Lcobucci\JWT\Signer\Key
  95. * @covers Lcobucci\JWT\Signer\BaseSigner
  96. * @covers Lcobucci\JWT\Signer\Hmac
  97. * @covers Lcobucci\JWT\Signer\Hmac\Sha256
  98. */
  99. public function verifyShouldReturnFalseWhenKeyIsNotRight(Token $token)
  100. {
  101. $this->assertFalse($token->verify($this->signer, 'testing1'));
  102. }
  103. /**
  104. * @test
  105. *
  106. * @depends builderCanGenerateAToken
  107. *
  108. * @covers Lcobucci\JWT\Builder
  109. * @covers Lcobucci\JWT\Parser
  110. * @covers Lcobucci\JWT\Token
  111. * @covers Lcobucci\JWT\Signature
  112. * @covers Lcobucci\JWT\Parsing\Encoder
  113. * @covers Lcobucci\JWT\Claim\Factory
  114. * @covers Lcobucci\JWT\Claim\Basic
  115. * @covers Lcobucci\JWT\Signer\Key
  116. * @covers Lcobucci\JWT\Signer\BaseSigner
  117. * @covers Lcobucci\JWT\Signer\Hmac
  118. * @covers Lcobucci\JWT\Signer\Hmac\Sha256
  119. * @covers Lcobucci\JWT\Signer\Hmac\Sha512
  120. */
  121. public function verifyShouldReturnFalseWhenAlgorithmIsDifferent(Token $token)
  122. {
  123. $this->assertFalse($token->verify(new Sha512(), 'testing'));
  124. }
  125. /**
  126. * @test
  127. *
  128. * @depends builderCanGenerateAToken
  129. *
  130. * @covers Lcobucci\JWT\Builder
  131. * @covers Lcobucci\JWT\Parser
  132. * @covers Lcobucci\JWT\Token
  133. * @covers Lcobucci\JWT\Signature
  134. * @covers Lcobucci\JWT\Parsing\Encoder
  135. * @covers Lcobucci\JWT\Claim\Factory
  136. * @covers Lcobucci\JWT\Claim\Basic
  137. * @covers Lcobucci\JWT\Signer\Key
  138. * @covers Lcobucci\JWT\Signer\BaseSigner
  139. * @covers Lcobucci\JWT\Signer\Hmac
  140. * @covers Lcobucci\JWT\Signer\Hmac\Sha256
  141. */
  142. public function verifyShouldReturnTrueWhenKeyIsRight(Token $token)
  143. {
  144. $this->assertTrue($token->verify($this->signer, 'testing'));
  145. }
  146. /**
  147. * @test
  148. *
  149. * @covers Lcobucci\JWT\Builder
  150. * @covers Lcobucci\JWT\Parser
  151. * @covers Lcobucci\JWT\Token
  152. * @covers Lcobucci\JWT\Signature
  153. * @covers Lcobucci\JWT\Signer\Key
  154. * @covers Lcobucci\JWT\Signer\BaseSigner
  155. * @covers Lcobucci\JWT\Signer\Hmac
  156. * @covers Lcobucci\JWT\Signer\Hmac\Sha256
  157. * @covers Lcobucci\JWT\Claim\Factory
  158. * @covers Lcobucci\JWT\Claim\Basic
  159. * @covers Lcobucci\JWT\Parsing\Encoder
  160. * @covers Lcobucci\JWT\Parsing\Decoder
  161. */
  162. public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs()
  163. {
  164. $data = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJoZWxsbyI6IndvcmxkIn0.Rh'
  165. . '7AEgqCB7zae1PkgIlvOpeyw9Ab8NGTbeOH7heHO0o';
  166. $token = (new Parser())->parse((string) $data);
  167. $this->assertEquals('world', $token->getClaim('hello'));
  168. $this->assertTrue($token->verify($this->signer, 'testing'));
  169. }
  170. }