PHP code example of lgrevelink / php-simple-jwt

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

    

lgrevelink / php-simple-jwt example snippets


use LGrevelink\SimpleJWT\Token;

$token = new Token([
    'custom' => 'payload',
]);

$token->toString();
// > eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJjdXN0b20iOiJwYXlsb2FkIn0.

use LGrevelink\SimpleJWT\TokenBlueprint;

class MyToken extends TokenBlueprint
{
    protected static $audience = 'GitHub users';

    protected static $expirationTime = 3600;

    protected static $issuedAt = 0;

    protected static $issuer = 'Developer';

    protected static $subject = 'Blueprint example';
}

$token = MyToken::generate([ /* Custom claims */ ]);

$token->toString();
// > eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJhdWQiOiJHaXRIdWIgdXNlcnMiLCJleHAiOjE1NTk1NzkwNzgsImlhdCI6MTU1OTU3NTQ3OCwiaXNzIjoiRGV2ZWxvcGVyIiwic3ViIjoiQmx1ZXByaW50IGV4YW1wbGUifQ.

MyToken::validate($token);
// > true

use LGrevelink\SimpleJWT\Token;
use LGrevelink\SimpleJWT\Signing\Hmac\HmacSha256;

$token = new Token();
$token->sign(new HmacSha256(), 'signing secret');

$token->toString();
// > eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.W10.o5-rpJi4_bEYcIWisa6qD7rFX6fk4Jh0FfNyOTmDbsI

$token->verify(new HmacSha256(), 'signing secret');
// true

use LGrevelink\SimpleJWT\Signing\Rsa\Keys\PrivateKey;
use LGrevelink\SimpleJWT\Signing\Rsa\Keys\PublicKey;
use LGrevelink\SimpleJWT\Signing\Rsa\RsaSha256;
use LGrevelink\SimpleJWT\Token;

$privateKey = new PrivateKey('private_rsa.pem');

$token = new Token();
$token->sign(new RsaSha256($privateKey), 'possible passphrase');

$token->toString();
// > eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.W10.Tsm8x3WxQUa12n2FedJIHlObnLZBbPF_IczvcTAt25ZIaJlYOuka8S5GzdmJ6ZfD5UiHLTgbJG0pdpSwdnsKg44TyWj5Yl19qx6ddDKcfQxk7zaPTy6kDaCi5Hl6yC0WiXjgnvolD9Hp8fBYoYmcer-V0cFr50Ee9SfBuIejQPddlGvx7EfjZ0yIVNuxBD7Uzimio3VacomolpFAmJHPqLLSfrHKI9ITncyg9U_IpnwHcBUe3BBeHEUzeL8k9nvUKZof5vIAGsZ7o3xi0NbOMfYw5DhP8jCTgjKlqMfxlQVRI8cNPj852qfrf8CzYHvYuR_7uN1s8a_ooBfHjOxeYg

$publicKey = new PublicKey('public_rsa.pem');

$token->verify(new RsaSha256(null, $publicKey));
// true

use LGrevelink\SimpleJWT\TokenBlueprint;
use LGrevelink\SimpleJWT\TokenSignature;
use LGrevelink\SimpleJWT\Signing\Hmac\HmacSha256;

class MyToken extends TokenBlueprint
{
    // ...

    public function signature($key) {
        return new TokenSignature(new HmacSha256(), $key);
    }
}

$token = MyToken::generate([
    'custom-claim' => 'data'
])->signature(MyToken::signature('some-key'));

// or

$token = MyToken::generateAndSign([
    'custom-claim' => 'data'
], 'some-key');

// or

$token = MyToken::sign(
    SignatureBlueprintMock::generate([
        'custom-claim' => 'data'
    ]),
    'some-key'
);

use LGrevelink\SimpleJWT\Token;

$token = Token::parse('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.W10.o5-rpJi4_bEYcIWisa6qD7rFX6fk4Jh0FfNyOTmDbsI');
bash
composer