1. Go to this page and download the library: Download itaiarbel/jwt library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
itaiarbel / jwt example snippets
$private_rsa_key='
-----BEGIN PRIVATE KEY-----
... generate your own 2048bit/4096bit RSA keys ...
-----END PRIVATE KEY-----';
$public_rsa_key='
-----BEGIN PUBLIC KEY-----
... generate your own 2048bit/4096bit RSA keys ...
-----END PUBLIC KEY-----';
$jws2= Jwt::Builder()
->header('kid','38890') //optional key id - for key menegment
->jti()
->claim('iss','me')
->claim('aud','you')
->exp(3600)
->nbf(600)
->claim('sub','123123')
->claim('user_verified','1')
->sign($private_rsa_key,'RS256');
echo $jws2->jwt; //your jws signed string using the private key
$private_rsa_key='
-----BEGIN PRIVATE KEY-----
... generate your own 2048bit/4096bit RSA keys ...
-----END PRIVATE KEY-----';
$jwe= Jwt::Builder()
->header('kid','1')
->jti()
->claim('iss','me')
->claim('aud','you')
->exp(3600)
->nbf(600)
->claim('sub','123123')
->claim('user_verified','1')
->encrypt($private_rsa_key,'RSA1_5','A128CBC-HS256');
echo $jwe->jwt; //your jwe string encrypted using the private key
->nbf(600) //time()+600
->nbf(600,$ts) //$ts+600
$public_rsa_key='
-----BEGIN PUBLIC KEY-----
... generate your own 2048bit/4096bit RSA keys ...
-----END PUBLIC KEY-----';
try{
//start checker with jwt string input
$jwe= Jwt::Checker($user_input_jwe);
//you can get header claims before decrypting like: alg,enc,kid ect...
$header= $jwe->getHeaderClaims();
//$jwe->decrypt($public_key,$header['alg'],$header['enc']); //extract alg &enc from header
$jwe->decrypt($public_rsa_key,'RSA1_5','A128CBC-HS256');// or you can decrypt using known preset alg & enc
}catch(Exception $e){ //error decrypting/parsing
$jwe=false;
}
if ($jwe && $jwe->validate()){ //validates exp & nbf & decryption here
echo "VALID TOKEN!!<br>";
print_r($jwe->getClaims()); //print all claims
echo '<br><br>';
}else{
echo "TOKEN NOT VALID!!<br>";
}
//same as above + checking extra fields like issuer
if (
$jwe && //ckeck input of token
$jwe->decrypted && // Signature verification
$jwe->checkExp() && //If exp timstamp passed
$jwe->checkNbf() && //checkNbf
//up to here - same as calling: $jws->validate()
$jwe->iss('me') && //If issuer is"me"
$jwe->aud('you') //If audience is "you"
){
echo "VALID TOKEN FROM ME TO YOU!!<br>";
print_r($jwe->getClaims()); //print all claims
echo '<br><br>';
}else{
echo "TOKEN NOT VALID!!<br>";
}