2017-12-24 20:00:45 +01:00
|
|
|
|
<?php
|
|
|
|
|
require_once('./vendor/firebase/php-jwt/src/JWT.php');
|
2017-12-24 23:13:42 +01:00
|
|
|
|
require_once('./vendor/firebase/php-jwt/src/BeforeValidException.php');
|
|
|
|
|
require_once('./vendor/firebase/php-jwt/src/ExpiredException.php');
|
|
|
|
|
require_once('./vendor/firebase/php-jwt/src/SignatureInvalidException.php');
|
|
|
|
|
require_once('./vendor/hashids/hashids/lib/Hashids/HashGenerator.php');
|
|
|
|
|
require_once('./vendor/hashids/hashids/lib/Hashids/Hashids.php');
|
|
|
|
|
|
2017-12-24 20:00:45 +01:00
|
|
|
|
use \Firebase\JWT\JWT;
|
2017-12-24 23:13:42 +01:00
|
|
|
|
use \Hashids\Hashids;
|
|
|
|
|
|
2017-12-24 20:00:45 +01:00
|
|
|
|
|
|
|
|
|
class Token {
|
|
|
|
|
private $key;
|
2017-12-24 23:13:42 +01:00
|
|
|
|
private $hashids;
|
2017-12-24 22:19:42 +01:00
|
|
|
|
function __construct() {
|
2017-12-24 20:00:45 +01:00
|
|
|
|
$this->key = 'ˈkɑːn.læŋ.ɪŋ_4eva';
|
2017-12-24 23:13:42 +01:00
|
|
|
|
$this->hashids = new Hashids($this->key, 10);
|
2017-12-24 20:00:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function encode ($data) {
|
|
|
|
|
return JWT::encode($data, $this->key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function decode ($token) {
|
2017-12-24 23:13:42 +01:00
|
|
|
|
try {
|
|
|
|
|
return JWT::decode($token, $this->key, array('HS256'));
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function hash ($id) {
|
|
|
|
|
return $this->hashids->encode($id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function unhash ($hash) {
|
|
|
|
|
return $this->hashids->decode($hash);
|
2017-12-24 20:00:45 +01:00
|
|
|
|
}
|
|
|
|
|
}
|