Last active
April 14, 2021 19:51
-
-
Save ahmedsayedabdelsalam/f98feadb8cae66b17966b5d45fa0c678 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/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"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1- put inside root project directory
2-
chmod +x jwt-gen
3-
./jwt-gen <device_id> <secret>