An instance of the LittleApps\LittleJWT\JWT\JWT
class provides immutable access to the header claims, payload claims, and signature.
A token can be created by casting the JWT to a string:
$token = (string) $jwt;
// $token = 'ey...';
The header and payload claims can be accessed using the appropriate method:
$headers = $jwt->getHeaders();
$payload = $jwt->getPayload();
Both the getHeaders()
and getPayload()
methods return an instance of the LittleApps\LittleJWT\JWT\ClaimManager
class, which is an immutable collection of the claims.
$claims = $jwt->getPayload(); // or $jwt->getHeaders();
// Check if claim key 'abc' exists:
$exists = $claims->has('abc');
$exists = isset($claims->abc);
$exists = isset($claims['abc']);
// Get the claim value for key 'abc'
$value = $claims->get('abc'); // Returns null if key 'abc' doesn't exist.
$value = $claims->get('abc', false); // You can pass the default value that will be returned if the key doesn't exist.
$value = $claims->abc;
$value = $claims['abc'];
// Get all of the claim keys and values as a Collection.
$all = $claims->get();
// Get the number of claims in the header or payload.
$count = $claims->count();
All claim values are deserialized using the appropriate mutator before being added to the collection. See Mutating Claims for more information.
The signature can be retrieved from the JWT instance as raw bytes (decoded from base64):
$signature = $jwt->getSignature();
The signature can be encoded back to base64 using the provided LittleApps\LittleJWT\Utils\Base64Encoder
method:
use LittleApps\LittleJWT\Utils\Base64Encoder;
$signature = $jwt->getSignature();
$encoded = Base64Encoder::encode($signature);