PHP code example of phithi92 / json-web-token

1. Go to this page and download the library: Download phithi92/json-web-token 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/ */

    

phithi92 / json-web-token example snippets


use Phithi92\JsonWebToken\JwtAlgorithmManager;
use Phithi92\JsonWebToken\JwtPayload;
use Phithi92\JsonWebToken\JwtTokenFactory;

$manager = new JwtAlgorithmManager(
    'RS256',        // Specify the algorithm
    null,           // Passphrase for symmetric algorithms (optional for asymmetric)
    'public-key',  // Private key for asymmetric algorithms
    'private-key'    // Public key for asymmetric algorithms
);

$payload = (new JwtPayload())
    ->setIssuer('https://myapp.com')
    ->setAudience('https://myapi.com')
    ->setNotBefore('+3 minutes')
    ->setExpiration('+15 minutes');

$token = JwtTokenFactory::createToken($manager, $payload);

use Phithi92\JsonWebToken\JwtAlgorithmManager;
use Phithi92\JsonWebToken\JwtTokenFactory;

$manager = new JwtAlgorithmManager(
    'RS256',        // Specify the algorithm
    null,           // Passphrase for symmetric algorithms (optional for asymmetric)
    'public-key',  // Private key for asymmetric algorithms
    'private-key'    // Public key for asymmetric algorithms
);

$token = JwtTokenFactory::refreshToken($manager,$encodedToken,'+15 minutes');

use Phithi92\JsonWebToken\Exceptions\Payload\PayloadException;
use Phithi92\JsonWebToken\Exceptions\Token\TokenException;
use Phithi92\JsonWebToken\JwtAlgorithmManager;
use Phithi92\JsonWebToken\JwtTokenFactory;


$manager = new JwtAlgorithmManager(
    'RS256',        // Specify the algorithm
    null,           // Passphrase for symmetric algorithms (optional for asymmetric)
    'public-key',   // Private key for asymmetric algorithms
    'private-key'   // Public key for asymmetric algorithms
);

try {
    $token = JwtTokenFactory::decryptToken($manager, $encodedToken);
    $payload = $token->getPayload();
} catch(TokenException){
    ...
} catch(PayloadException){
    ...
}

use Phithi92\JsonWebToken\Exceptions\Payload\InvalidIssuerException;

try {
    $token->getPayload()->validateIssuer($issuer);
} catch(InvalidIssuerException){
    ...
}

use Phithi92\JsonWebToken\Exceptions\Payload\InvalidAudienceException;

try {
    $token->getPayload()->validateAudience($audience);
} catch(InvalidAudienceException){
    ...
}

$audience = ["example.com", "anotherdomain.com"];
$token->getPayload()->validateAudience($audience);

use Phithi92\JsonWebToken\Exception\Json\JsonException;
use Phithi92\JsonWebToken\Exception\Token\TokenException;
use Phithi92\JsonWebToken\Exception\Payload\PayloadException;
use Phithi92\JsonWebToken\Exception\Cryptography\CryptographyException;

try {
    // ...   
} catch (JsonException $e) {
    // JSON errors during JWT processing
} catch (PayloadException $e) {
    // Errors with the JWT payload
} catch (CryptographyException $e) {
    // Issues with the signing algorithm
} catch (TokenException $e) {
    // General token error
}