1. Go to this page and download the library: Download tmilos/jose-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/ */
tmilos / jose-jwt example snippets
$factory = new \Tmilos\JoseJwt\Context\DefaultContextFactory();
$context = $factory->get();
$payload = ['msg' => 'Hello!'];
$extraHeader = ['iam'=>'my-id'];
// plain (no signature) token
$token = \Tmilos\JoseJwt\Jwt::encode($context, $payload, null, \Tmilos\JoseJwt\Jws\JwsAlgorithm::NONE, $extraHeader);
// HS256 signature
$secret = '...'; // 256 bits secret
$token = \Tmilos\JoseJwt\Jwt::encode($context, $payload, $secret, \Tmilos\JoseJwt\Jws\JwsAlgorithm::HS256, $extraHeader);
// HS384 signature
$secret = '...'; // 256 bits secret
$token = \Tmilos\JoseJwt\Jwt::encode($context, $payload, $secret, \Tmilos\JoseJwt\Jws\JwsAlgorithm::HS384, $extraHeader);
// HS512 signature
$secret = '...'; // 256 bits secret
$token = \Tmilos\JoseJwt\Jwt::encode($context, $payload, $secret, \Tmilos\JoseJwt\Jws\JwsAlgorithm::HS512, $extraHeader);
// RS256
$privateKey = openssl_get_privatekey($filename);
$token = \Tmilos\JoseJwt\Jwt::encode($context, $payload, $secret, \Tmilos\JoseJwt\Jws\JwsAlgorithm::RS256, $extraHeader);
// RS384
$privateKey = openssl_get_privatekey($filename);
$token = \Tmilos\JoseJwt\Jwt::encode($context, $payload, $secret, \Tmilos\JoseJwt\Jws\JwsAlgorithm::RS384, $extraHeader);
// RS512
$privateKey = openssl_get_privatekey($filename);
$token = \Tmilos\JoseJwt\Jwt::encode($context, $payload, $secret, \Tmilos\JoseJwt\Jws\JwsAlgorithm::RS512, $extraHeader);
// decode
$header = \Tmilos\JoseJwt\Jwt::header($token);
// eventually also use other header data to indicate which key should be used
switch($header['alg']) {
case \Tmilos\JoseJwt\Jws\JwsAlgorithm::NONE:
$key = null;
break;
case \Tmilos\JoseJwt\Jws\JwsAlgorithm::HS256:
case \Tmilos\JoseJwt\Jws\JwsAlgorithm::HS384:
case \Tmilos\JoseJwt\Jws\JwsAlgorithm::HS512:
$key = $secret;
break;
case \Tmilos\JoseJwt\Jws\JwsAlgorithm::RS256:
case \Tmilos\JoseJwt\Jws\JwsAlgorithm::RS384:
case \Tmilos\JoseJwt\Jws\JwsAlgorithm::RS512:
$key = $publicKey;
break;
}
$payload = \Tmilos\JoseJwt\JWT::decode($context, $token, $key);