PHP code example of spoova / jwstoken

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

    

spoova / jwstoken example snippets

 
use Spoova\JwsToken\JwsToken;

$jws = new JwsToken;
 
  $jws->set($type, $algo);
  
 
   $jws->set('JWS', 'HS256');
   

  $jws = new JwsToken;

  $jws->algo('HS384'); //modify algorithm only
  

  $jws = new JwsToken;

  $payload = [

      'sub' => 'Token for accessing a class',
      'iss' => 'Issuer name or id'
      'aud' => 'Owner name or id'
      'iat' => time(),
      'nbf' => time() + 120, //token becomes active after 2 minutes of generation
      'exp' => time() + 240, //token becomes expired after 4 minutes of generation
      'data' => [
          'age' => 'user age',
          'gender' => 'male',
      ]
  ]

  $jws->payload($payload);
  

    $jws->payload($payload)->expires(time() + 240); //expires after  4 minutes of generation
    
 
   $jws->sign('secret_key', 'sha256');
   
 
   $token = $jws->token(); //return the generated token 
   
 
   $jws->token($token); //set a token for validation
   
 
   $jws->token($token);  
   
   if($jws->isValid('secret_key', 'sha256')){

      echo "token is valid";

   } else { 

     echo $jws->error();

   }
   
 
   $jws->token($token); 

   if($jws->expired('secret_key', 'sha256')){

       echo "token has expired";

   } else if(!$jws->error()) { 

       echo 'token is has not expired';

   }
   
 
   $jws->token($token); 

   if($jws->pending('secret_key', 'sha256')){

       echo "token has expired";

   } else if(!$jws->error()) { 

       echo 'token is has not expired';

   }
   

   $payload = $jws->decrypt($token, 'secret_key' 'sha256');
   
   var_dump($payload);
   
 
   if($jws->token($token)->isValid('secret_key' 'sha256')) {

     $payload = $jws->decrypt();
   
     var_dump($payload);
    
   }else {

     echo $jws->error();

   }