Skip to content

Instantly share code, notes, and snippets.

@ahmedsayedabdelsalam
Last active April 14, 2021 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedsayedabdelsalam/f98feadb8cae66b17966b5d45fa0c678 to your computer and use it in GitHub Desktop.
Save ahmedsayedabdelsalam/f98feadb8cae66b17966b5d45fa0c678 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
function base64UrlEncode($text)
{
return str_replace(
['+', '/', '='],
['-', '_', ''],
base64_encode($text)
);
}
// Create the token header
$header = json_encode([
'alg' => 'HS256',
'typ' => 'JWT',
]);
// Create the token payload
$payload = json_encode([
'device_id' => $argv[1],
]);
// get the local secret key
$secret = $argv[2];
// Encode Header
$base64UrlHeader = base64UrlEncode($header);
// Encode Payload
$base64UrlPayload = base64UrlEncode($payload);
// Create Signature Hash
$signature = hash_hmac('sha256', $base64UrlHeader.'.'.$base64UrlPayload, $secret, true);
// Encode Signature to Base64Url String
$base64UrlSignature = base64UrlEncode($signature);
// Create JWT
$jwt = $base64UrlHeader.'.'.$base64UrlPayload.'.'.$base64UrlSignature;
echo "Your token:\n".$jwt."\n";
@ahmedsayedabdelsalam
Copy link
Author

1- put inside root project directory
2- chmod +x jwt-gen
3- ./jwt-gen <device_id> <secret>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment