PHP code example of kelvinmo / simplejwt

1. Go to this page and download the library: Download kelvinmo/simplejwt 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/ */

    

kelvinmo / simplejwt example snippets


  $set = new SimpleJWT\Keys\KeySet();
  $set->load(file_get_contents('private.json'));
  

  $set = new SimpleJWT\Keys\KeySet();

  // JWK format
  $key = new SimpleJWT\Keys\RSAKey(file_get_contents('jwk.json'), 'json');

  // PEM format - note raw key only, no X.509 certificates
  $key = new SimpleJWT\Keys\RSAKey(file_get_contents('rsa.pem'), 'pem');

  $set->add($key);
  

  $set = SimpleJWT\Keys\KeySet::createFromSecret('secret123');

  // The above is a shortcut for the following:
  $set = new SimpleJWT\Keys\KeySet();
  $key = new SimpleJWT\Keys\SymmetricKey('secret123', 'bin');
  $set->add($key);
  

// Note $headers['alg'] is 6', 'typ' => 'JWT'];
$claims = ['iss' => 'me', 'exp' => 1234567];
$jwt = new SimpleJWT\JWT($headers, $claims);

try {
    print $jwt->encode($set);
} catch (\RuntimeException $e) {

}

try {
    $jwt = SimpleJWT\JWT::decode('abc.def.ghigjghr', $set, 'HS256');
} catch (SimpleJWT\InvalidTokenException $e) {

}

print $jwt->getHeader('alg');
print $jwt->getClaim('sub');

try {
    $result = SimpleJWT\JWT::deserialise('abc.def.ghigjghr');
} catch (SimpleJWT\InvalidTokenException $e) {

}

print $result['claims']['sub'];
print $result['signatures'][0]['headers']['alg'];
print $result['signatures'][0]['signing_input'];  // abc.def
print $result['signatures'][0]['signature'];      // ghigjghr
// Additional indices under $result['signatures'] if the JWT has more than
// one signature

// Note $headers['alg'] and $headers['enc'] are ' => 'A128CBC-HS256'];
$plaintext = 'This is the plaintext I want to encrypt.';
$jwt = new SimpleJWT\JWE($headers, $plaintext);

try {
    print $jwt->encrypt($set);
} catch (\RuntimeException $e) {

}

try {
    $jwt = SimpleJWT\JWE::decrypt('abc.def.ghi.klm.nop', $set, 'PBES2-HS256+A128KW');
} catch (SimpleJWT\InvalidTokenException $e) {

}

print $jwt->getHeader('alg');
print $jwt->getPlaintext();