Add Hashids to Token.php; catch bad token exceptions
This commit is contained in:
parent
3b4acdf1f3
commit
e5ced20d27
|
@ -1,11 +1,21 @@
|
||||||
<?php
|
<?php
|
||||||
require_once('./vendor/firebase/php-jwt/src/JWT.php');
|
require_once('./vendor/firebase/php-jwt/src/JWT.php');
|
||||||
|
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');
|
||||||
|
|
||||||
use \Firebase\JWT\JWT;
|
use \Firebase\JWT\JWT;
|
||||||
|
use \Hashids\Hashids;
|
||||||
|
|
||||||
|
|
||||||
class Token {
|
class Token {
|
||||||
private $key;
|
private $key;
|
||||||
|
private $hashids;
|
||||||
function __construct() {
|
function __construct() {
|
||||||
$this->key = 'ˈkɑːn.læŋ.ɪŋ_4eva';
|
$this->key = 'ˈkɑːn.læŋ.ɪŋ_4eva';
|
||||||
|
$this->hashids = new Hashids($this->key, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function encode ($data) {
|
public function encode ($data) {
|
||||||
|
@ -13,6 +23,18 @@ class Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function decode ($token) {
|
public function decode ($token) {
|
||||||
|
try {
|
||||||
return JWT::decode($token, $this->key, array('HS256'));
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,7 +3,8 @@
|
||||||
"description": "The PHP backend API for Lexiconga (lexicon.ga)",
|
"description": "The PHP backend API for Lexiconga (lexicon.ga)",
|
||||||
"type": "project",
|
"type": "project",
|
||||||
"require": {
|
"require": {
|
||||||
"firebase/php-jwt": "^5.0"
|
"firebase/php-jwt": "^5.0",
|
||||||
|
"hashids/hashids": "^1.0"
|
||||||
},
|
},
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "0032d2a2e253b895d7f3dbb3856674da",
|
"content-hash": "5d014834681eb6b0d1582cd6e6c0a6c9",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "firebase/php-jwt",
|
"name": "firebase/php-jwt",
|
||||||
|
@ -51,6 +51,63 @@
|
||||||
"description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.",
|
"description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.",
|
||||||
"homepage": "https://github.com/firebase/php-jwt",
|
"homepage": "https://github.com/firebase/php-jwt",
|
||||||
"time": "2017-06-27T22:17:23+00:00"
|
"time": "2017-06-27T22:17:23+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "hashids/hashids",
|
||||||
|
"version": "1.0.6",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/ivanakimov/hashids.php.git",
|
||||||
|
"reference": "6bd7ceffb2ccddef6cd64e88f1717f97d131cc3c"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/ivanakimov/hashids.php/zipball/6bd7ceffb2ccddef6cd64e88f1717f97d131cc3c",
|
||||||
|
"reference": "6bd7ceffb2ccddef6cd64e88f1717f97d131cc3c",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": "^5.3.3 || ^7.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^4.8 || ^5.0"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-master": "1.0-dev"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Hashids\\": "lib/Hashids/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Ivan Akimov",
|
||||||
|
"email": "ivan@barreleye.com",
|
||||||
|
"homepage": "https://twitter.com/IvanAkimov"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Generate short, unique, non-sequential ids (like YouTube and Bitly) from numbers.",
|
||||||
|
"homepage": "http://hashids.org/php",
|
||||||
|
"keywords": [
|
||||||
|
"bitly",
|
||||||
|
"decode",
|
||||||
|
"encode",
|
||||||
|
"hash",
|
||||||
|
"hashid",
|
||||||
|
"hashids",
|
||||||
|
"ids",
|
||||||
|
"obfuscate",
|
||||||
|
"youtube"
|
||||||
|
],
|
||||||
|
"time": "2016-01-21T04:26:38+00:00"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"packages-dev": [],
|
"packages-dev": [],
|
||||||
|
|
Loading…
Reference in New Issue