您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JwtHelper.php 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 9/27/15
  6. * Time: 1:51 AM
  7. */
  8. namespace Luticate\Auth\Business;
  9. class JwtHelper
  10. {
  11. const EXPIRATION_KEY = "expiration_date";
  12. public static function decode($token)
  13. {
  14. $jwt = mcrypt_decrypt(MCRYPT_TRIPLEDES, env("MCRYPT_KEY"), base64_decode($token), "ecb");
  15. try
  16. {
  17. $data = (array)\JWT::decode($jwt, env("JWT_KEY"), ['HS256']);
  18. }
  19. catch (\Exception $e)
  20. {
  21. return null;
  22. }
  23. $expiration_date = array_key_exists(self::EXPIRATION_KEY, $data) ? $data[self::EXPIRATION_KEY] : null;
  24. if (!is_numeric($expiration_date) || $expiration_date < time())
  25. return null;
  26. return $data;
  27. }
  28. public static function encode($data, $session_time)
  29. {
  30. $date = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
  31. $date->modify("+${session_time} day");
  32. $data[self::EXPIRATION_KEY] = $date->getTimestamp();
  33. return base64_encode(mcrypt_encrypt(MCRYPT_TRIPLEDES, env("MCRYPT_KEY"), \JWT::encode($data, env("JWT_KEY")), "ecb"));
  34. }
  35. }