Last active
December 1, 2018 12:06
-
-
Save kikegarcia/5960f11a41a3a75490a743ea858ee506 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'classes/sdk/firebase/php-jwt/src/BeforeValidException.php'; | |
require_once 'classes/sdk/firebase/php-jwt/src/ExpiredException.php'; | |
require_once 'classes/sdk/firebase/php-jwt/src/SignatureInvalidException.php'; | |
require_once 'classes/sdk/firebase/php-jwt/src/JWT.php'; | |
use \Firebase\JWT\JWT; | |
/** | |
* Clase para generar JWT | |
*/ | |
class jwt_generator | |
{ | |
private $key; | |
private $iss; | |
private $aud; | |
/** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
$this->key = get_config_bbdd('jwt_authentication_key'); | |
$this->iss = "auth"; // Creador del JWT | |
$this->aud = "kikegarcia"; // Audiencia | |
} | |
/** | |
* Genera JWT con los datos de usuario | |
* @param [type] $userData [description] | |
* @return [type] [description] | |
*/ | |
public function generate_jwt($userData) | |
{ | |
// Hora de creación | |
$dateObject = new DateTime(); | |
$creationDate = $dateObject->getTimestamp(); | |
// Hora de caducidad (+8H) | |
$expirationDateObject = $dateObject->add(new DateInterval('PT8H')); | |
$expirationDate = $expirationDateObject->getTimestamp(); | |
$tokenPayloads = array( | |
"iss" => $this->iss, | |
"aud" => $this->aud, | |
"iat" => $creationDate, | |
"exp" => $expirationDate | |
); | |
$finalPayloads = $tokenPayloads + $userData; | |
$jwt = JWT::encode($finalPayloads, $this->key, 'HS256'); | |
return $jwt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment