JwtTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of qbhy/simple-jwt.
  5. *
  6. * @link https://github.com/qbhy/simple-jwt
  7. * @document https://github.com/qbhy/simple-jwt/blob/master/README.md
  8. * @contact qbhy0715@qq.com
  9. * @license https://github.com/qbhy/simple-jwt/blob/master/LICENSE
  10. */
  11. namespace Qbhy\SimpleJwt\Tests\Unit;
  12. use Qbhy\SimpleJwt\Encoders\Base64UrlSafeEncoder;
  13. use Qbhy\SimpleJwt\EncryptAdapters\CryptEncrypter;
  14. use Qbhy\SimpleJwt\EncryptAdapters\HS256Encrypter;
  15. use Qbhy\SimpleJwt\EncryptAdapters\Md5Encrypter;
  16. use Qbhy\SimpleJwt\EncryptAdapters\PasswordHashEncrypter;
  17. use Qbhy\SimpleJwt\EncryptAdapters\SHA1Encrypter;
  18. use Qbhy\SimpleJwt\Exceptions\TokenBlacklistException;
  19. use Qbhy\SimpleJwt\JWT;
  20. use Qbhy\SimpleJwt\JWTManager;
  21. use Qbhy\SimpleJwt\Tests\TestCase;
  22. /**
  23. * @internal
  24. * @coversNothing
  25. */
  26. class JwtTest extends TestCase
  27. {
  28. /**
  29. * 测试默认 加密器.
  30. */
  31. public function testJwtManager()
  32. {
  33. $manager = $this->manager();
  34. $this->assertTrue($this->check($manager));
  35. $this->assertTrue($this->check($manager->useEncrypter(Md5Encrypter::alg())));
  36. $this->assertTrue($this->check($manager->useEncrypter(Md5Encrypter::class)));
  37. $this->assertTrue($this->check($this->manager(null, new Base64UrlSafeEncoder())));
  38. }
  39. /**
  40. * 测试默认 md5 加密器.
  41. */
  42. public function testMd5JwtManager()
  43. {
  44. $secret = 'qbhy/simple-jwt';
  45. $this->assertTrue($this->check($this->manager(Md5Encrypter::class)));
  46. $this->assertTrue($this->check($this->manager(Md5Encrypter::class, new Base64UrlSafeEncoder())));
  47. }
  48. /**
  49. * 测试默认 md5 加密器.
  50. */
  51. public function testHS256JwtManager()
  52. {
  53. $jwtManager = new JWTManager([
  54. 'secret' => 'secret',
  55. 'default' => new HS256Encrypter('qwewe1232323dfsdfhadlibfjfh90'),
  56. 'encode' => new Base64UrlSafeEncoder(),
  57. 'cache' => function (JWTManager $JWTManager) {
  58. return new \Doctrine\Common\Cache\FilesystemCache(sys_get_temp_dir());
  59. },
  60. ]);
  61. // 群友提供
  62. $token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBpZCI6MSwiY29tcGFueUlkIjp7IkludDY0IjoxLCJWYWxpZCI6dHJ1ZX0sImV4cCI6MTYyOTg4NDg5NywiaWF0IjoxNjI3MjkyODk3LCJzdGFmZk5hbWUiOnsiU3RyaW5nIjoi5bCP5piOIiwiVmFsaWQiOnRydWV9LCJzdGFmZmlkIjp7IkludDY0IjoxLCJWYWxpZCI6dHJ1ZX0sInVzZXJJZCI6MSwidXNlck5hbWUiOiJ1c2VyMDEifQ.Pz8kWBdtDMOcJs9HXmzvlPgY53aZVyg50vBRELG7G9M';
  63. $jwt = $jwtManager->justParse($token);
  64. $this->assertTrue($jwt instanceof JWT);
  65. $this->assertTrue($this->check($this->manager(HS256Encrypter::class)));
  66. $this->assertTrue($this->check($this->manager(HS256Encrypter::class, new Base64UrlSafeEncoder())));
  67. }
  68. /**
  69. * 测试默认 crypt 加密器.
  70. */
  71. public function testCryptJwtManager()
  72. {
  73. $this->assertTrue($this->check($this->manager(CryptEncrypter::class)));
  74. $this->assertTrue($this->check($this->manager(CryptEncrypter::class, new Base64UrlSafeEncoder())));
  75. }
  76. /**
  77. * 测试默认 crypt 加密器.
  78. */
  79. public function testPasswordJwtManager()
  80. {
  81. $this->assertTrue($this->check($this->manager(PasswordHashEncrypter::class)));
  82. $this->assertTrue($this->check($this->manager(PasswordHashEncrypter::class, new Base64UrlSafeEncoder())));
  83. }
  84. /**
  85. * 测试默认 crypt 加密器.
  86. */
  87. public function testSHA1JwtManager()
  88. {
  89. $this->assertTrue($this->check($this->manager(SHA1Encrypter::class)));
  90. $this->assertTrue($this->check($this->manager(SHA1Encrypter::class, new Base64UrlSafeEncoder())));
  91. }
  92. /**
  93. * 测试默认黑名单功能.
  94. */
  95. public function testJwtManagerBlacklist()
  96. {
  97. $secret = 'qbhy/simple-jwt';
  98. $jwtManager = new JWTManager(compact('secret'));
  99. $jwt = $jwtManager->make(['test' => 'test']);
  100. $jwtManager->addBlacklist($jwt);
  101. try {
  102. $jwtManager->parse($jwt->token());
  103. $this->assertTrue(false, 'jwt 黑名单测试出错');
  104. } catch (\Throwable $exception) {
  105. $this->assertTrue($exception instanceof TokenBlacklistException, $exception->getMessage());
  106. }
  107. }
  108. protected function check(JWTManager $manager)
  109. {
  110. $jwt = $manager->make(['user_id' => 1], ['header' => 'test']);
  111. $token = $jwt->token();
  112. $manager->addBlacklist($jwt);
  113. $manager->removeBlacklist($jwt);
  114. return $manager->parse($token) instanceof JWT && $manager->justParse($token) instanceof JWT;
  115. }
  116. protected function manager($driver = null, $encoder = null)
  117. {
  118. return new JWTManager([
  119. 'secret' => 'secret',
  120. 'default' => $driver,
  121. 'encode' => $encoder,
  122. 'cache' => function (JWTManager $JWTManager) {
  123. return new \Doctrine\Common\Cache\FilesystemCache(sys_get_temp_dir());
  124. },
  125. ]);
  126. }
  127. }