Created
April 12, 2017 17:52
-
-
Save madeinnordeste/c1e651b094161d4e9ab7034d4a7fede6 to your computer and use it in GitHub Desktop.
JWT example
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
<?php | |
// tutorial: https://www.youtube.com/watch?v=k3KfK0ZS_FY | |
// debuger: https://jwt.io/ | |
// lcobucci/jwt | |
// Generate | |
// - - - - - | |
function token(){ | |
//key | |
$key = 'macaco'; | |
//header | |
$header = [ | |
'typ' => 'JWT', | |
'alg' => 'HS256' | |
]; | |
$header = json_encode($header); | |
$header = base64_encode($header); | |
//payload | |
$payload = [ | |
'iss' => 'madeinnordeste.com.br', //reservado | |
'username' => 'madeinnordeste', | |
'email' => '[email protected]' | |
]; | |
$payload = json_encode($payload); | |
$payload = base64_encode($payload); | |
//signature | |
$signature = hash_hmac('sha256', "$header.$payload", $key, true); | |
$signature = base64_encode($signature); | |
$token = "$header.$payload.$signature"; | |
return $token; | |
} | |
$received_token = token(); | |
echo "\n\n"; | |
echo $received_token; | |
echo "\n\n"; | |
if( $received_token === token() ){ | |
echo "token valid"; | |
}else{ | |
echo "token invalid"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment