123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <?php
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 9/27/15
- * Time: 1:51 AM
- */
-
- namespace Luticate\Auth\Business;
-
- class JwtHelper
- {
- const EXPIRATION_KEY = "expiration_date";
-
- public static function decode($token)
- {
- $jwt = mcrypt_decrypt(MCRYPT_TRIPLEDES, env("MCRYPT_KEY"), base64_decode($token), "ecb");
- try
- {
- $data = (array)\JWT::decode($jwt, env("JWT_KEY"), ['HS256']);
- }
- catch (\Exception $e)
- {
- return null;
- }
-
- $expiration_date = array_key_exists(self::EXPIRATION_KEY, $data) ? $data[self::EXPIRATION_KEY] : null;
- if (!is_numeric($expiration_date) || $expiration_date < time())
- return null;
-
- return $data;
- }
-
- public static function encode($data, $session_time)
- {
- $date = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
- $date->modify("+${session_time} day");
- $data[self::EXPIRATION_KEY] = $date->getTimestamp();
-
- return base64_encode(mcrypt_encrypt(MCRYPT_TRIPLEDES, env("MCRYPT_KEY"), \JWT::encode($data, env("JWT_KEY")), "ecb"));
- }
- }
|