Skip to content

Instantly share code, notes, and snippets.

@argordmel
Last active June 8, 2017 23:32
Show Gist options
  • Save argordmel/2c9cb9d161f70e8fe0b73584e04790cc to your computer and use it in GitHub Desktop.
Save argordmel/2c9cb9d161f70e8fe0b73584e04790cc to your computer and use it in GitHub Desktop.
Json Web Token JWT with KumbiaPHP
<?php
/**
* JSON Web Token implementation, based on this spec:
* http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
*
* PHP version 5
*
* @category Authentication
* @package Authentication_JWT
* @author Neuman Vong <neuman@twilio.com>
* @author Anant Narayanan <anant@php.net>
* @author Iván Meléndez <argordmel@gmail.com>
* @license http://opensource.org/licenses/BSD-3-Clause 3-clause BSD
* @link https://github.com/firebase/php-jwt
* @link https://coderwall.com/p/8wrxfw/goodbye-php-sessions-hello-json-web-tokens
* @link https://github.com/rmcdaniel/angular-codeigniter-seed
*
* Usage:
*
* 1) Instalation
* * composer require firebase/php-jwt
* * Edit the file public/index.php and uncomment/enable vendors:
* require_once("../../vendor/autoload.php");
*
* 2) Define your KEY
* define('JWT_KEY', 'aklasdfñ!sdljsdk45654@!');
*
* 3) Generate Token:
*
* // Private Info
* $token = [];
* $token['id'] = 1;
* $token['var'] = 'value';
*
* // Public Info
* $output['name'] = 'Iván Meléndez';
* $output['email'] = 'argordmel@gmail.com';
* $output['token'] = DwJwt::encode($token);
*
* echo json_encode($output);
*
* 4) Validate Token in your controller (api_controller.php, rest_controller.php, app_controller.php, etc)
*
* Example: rest_controller.php
*
* public function before_initialize() {
*
* // You can get the token via URL ?token=abc or via header: x-token-auth: 'abc'
* $token = Input::get('token') ? Input::get('token') : Input::server('HTTP_X_TOKEN_AUTH');
* $auth = DwJwt::decode(empty($token) ? '' : $token);
* if(empty($auth->id)) {
* $this->data = $this->error('Token inválido', 401);
* return false; // STOP EXCECUTION
* }
* }
*
*
*/
use Firebase\JWT\JWT;
class DwJwt {
public static $_data = [];
/**
* Get decode variable
* @param type $var
* @return type
*/
public static function get($var) {
return isset(self::$_data[$var]) ? self::$_data[$var] : NULL;
}
/**
*
* @param type $token
* @return type
*/
public static function decode($token) {
try {
$payload = JWT::decode($token, JWT_KEY, array('HS256'));
self::$_data = (array) $payload;
return $payload;
} catch (Exception $e) {
self::$_data = [];
return [];
}
}
/**
*
* @param array $token
* @return type
*/
public static function encode($token) {
return JWT::encode($token, JWT_KEY, 'HS256');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment