Parsing tokens


The examples here fetch the configuration object from a hypothetical dependency injection container. You can create it in the same script or require it from a different file. It basically depends on how your system is bootstrapped.

To parse a token you must create a new parser (easier when using the configuration object) and ask it to parse a string:

use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\UnencryptedToken;

$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$token = $config->parser()->parse(
    . 'eyJzdWIiOiIxMjM0NTY3ODkwIn0.'
    . '2gSBz9EOsQRN9I-3iSxJoFt7NtgV6Rm0IL6a8CAwl3Q'

assert($token instanceof UnencryptedToken);

$token->headers(); // Retrieves the token headers
$token->claims(); // Retrieves the token claims


In case of parsing errors the Parser will throw an exception of type InvalidArgumentException.