1. Go to this page and download the library: Download tc/jose 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/ */
tc / jose example snippets
use Tc\JOSE\JWS;
// ...
// Create a new JWS
$jws = new JWS();
// Add some data to the payload
$jws->setPayload(array(
'user' => 'SomeUser'
));
// Set Issued At Claim
$jws->setIssuedAt();
// Set Expires for 1 hour
$jws->setExpires(3600);
// Sign the JWS (Can use any of the supported algorithms)
$jws->sign('HS256', 'SecretKeyHere');
// Serialize the JWS to be transported
$jwsSerialized = $jws->serialize();
// Should look like:
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpPU0UifQ.e30.ssb8sFTv7UK37oW395EUkSL9g8uNPDhMHFvRwcUenXo
// You could then return this token to the client normally or as a header/cookie
// ...
use Tc\JOSE\JWT;
use Tc\JOSE\JWS;
// ...
// The Serialized JWT (could be from a header/request parameter)
$jwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpPU0UifQ.e30.ssb8sFTv7UK37oW395EUkSL9g8uNPDhMHFvRwcUenXo';
// Try to decode the Serialized JWT
try {
$decodedJWT = JWT::decode($jwt);
// Check if the decoded JWT is a JWS (could potentially be a JWS or JWE)
if ( $decodedJWT instanceof JWS ) {
// is a JWS, we now check it is valid
$isValid = $decodedJWT->validate('SecretKeyHere');
if ($isValid) {
// JWS is valid
}
}
} catch(InvalidArgumentException $e) {
// Invalid JWT, handle here
}
// ...