Skip to content

Instantly share code, notes, and snippets.

@kinncj
Last active June 15, 2021 20:03
Show Gist options
  • Save kinncj/b245cbbf17cce28e14a2c899116acb05 to your computer and use it in GitHub Desktop.
Save kinncj/b245cbbf17cce28e14a2c899116acb05 to your computer and use it in GitHub Desktop.
PHP JWT Encoder/Decoder

Download

$ cd ~/ && mkdir .jwt && cd .jwt && curl https://gist.githubusercontent.com/kinncj/b245cbbf17cce28e14a2c899116acb05/raw/03b714540d36cc65b0d6c356cebc009293b70919/jwt.php > jwt.php

php jwt.php encode HS256 foo "{\"name\":\"Kinn\"}" 

Usage

php jwt.php encode RSA256 my_private_key_file my_json_content
php jwt.php encode HS256 my_key my_json_content
php jwt.php encode RSA256 my_private_key_file my_json_content.json
php jwt.php encode HS256 my_key my_json_content.json
php jwt.php decode RSA256 my_public_key_file my_key_content
php jwt.php decode HS256 my_key my_key_content

php jwt.php encode HS256 foo "{\"name\":\"Kinn\"}"
php jwt.php decode HS256 foo eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiS2lubiJ9.l4JOS3Xisor8lgjs8_BvnR-o-KfUd1EZauh5vrf05CU
<?php
`curl https://getcomposer.org/download/2.1.3/composer.phar > composer`;
`chmod +x composer`;
`./composer require firebase/php-jwt`;
require_once './vendor/autoload.php';
use Firebase\JWT\JWT;
echo "Usage: php jwt.php encode RSA256 my_private_key_file my_json_content".PHP_EOL;
echo "Usage: php jwt.php encode HS256 my_key my_json_content".PHP_EOL;
echo "Usage: php jwt.php encode RSA256 my_private_key_file my_json_content.json".PHP_EOL;
echo "Usage: php jwt.php encode HS256 my_key my_json_content.json".PHP_EOL;
echo "Usage: php jwt.php decode RSA256 my_public_key_file my_key_content".PHP_EOL;
echo "Usage: php jwt.php decode HS256 my_key my_key_content".PHP_EOL;
define("SCRIPT_NAME_VAR", 0);
define("OPERATION_VAR", 1);
define("KEY_TYPE_VAR", 2);
define("KEY_VAR", 3);
define("CONTENT_VAR", 4);
define("VARS", [
"SCRIPT_NAME_VAR" => SCRIPT_NAME_VAR,
"OPERATION_VAR" => OPERATION_VAR,
"KEY_TYPE_VAR" => KEY_TYPE_VAR,
"KEY_VAR" => KEY_VAR,
"CONTENT_VAR" => CONTENT_VAR,
]);
$keyTypes = ['HS256' ,'RS256'];
validate($argv ?: [], $keyTypes);
define("SCRIPT_NAME", $argv[SCRIPT_NAME_VAR]);
define("OPERATION", $argv[OPERATION_VAR]);
define("KEY_TYPE", $argv[KEY_TYPE_VAR]);
$__CONTENT = $argv[CONTENT_VAR];
switch (KEY_TYPE) {
case "RSA256":
define("KEY", file_get_contents($argv[KEY_VAR]));
break;
default:
define("KEY", $argv[KEY_VAR]);
break;
}
if (strpos($__CONTENT, ".json") === (strlen($__CONTENT) - strlen(".json"))) {
$__CONTENT = file_get_contents($__CONTENT);
}
define("CONTENT", $__CONTENT);
function validate($args, $keyTypes)
{
foreach (VARS as $var => $value) {
if (empty($args[$value])) {
throw new \Exception(sprintf('Undefined value for "%s"', $var));
}
}
if ( ! in_array($args[KEY_TYPE_VAR], $keyTypes)) {
throw new \Exception(sprintf('Invalid key type "%s"', $args[KEY_TYPE_VAR]));
}
}
switch (OPERATION) {
case "encode":
encode();
break;
default:
decode();
}
function encode() {
echo JWT::encode(json_decode(CONTENT), KEY, KEY_TYPE).PHP_EOL;
}
function decode() {
var_dump((array) JWT::decode(CONTENT, KEY, [KEY_TYPE])).PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment