Skip to content

Instantly share code, notes, and snippets.

@madeinnordeste
Created April 12, 2017 17:52
Show Gist options
  • Save madeinnordeste/c1e651b094161d4e9ab7034d4a7fede6 to your computer and use it in GitHub Desktop.
Save madeinnordeste/c1e651b094161d4e9ab7034d4a7fede6 to your computer and use it in GitHub Desktop.
JWT example
<?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