The JWT Object - fachsimpeln/EasyJWT GitHub Wiki

Description

This is the object which verifies, reads the values and generates the actual JWT.

Usage

Create a JWT

To create a JWT you must provide a JWTData object, which contains a header, some dataand the signature.

Note: This can also be a JWTDataEncrypted object

$jwt = new EasyJWT\JWT($jwt_data);

When you create the JWT object, the JWTData object's signature is verified, and, if the signature is valid, you can interact with the object.

Validate a JWT

To validate a JWT, the signature must be verified. But EasyJWT does this process automatically just as you create the object. Only if the signature is valid, the array of the JWT can be accessed.

To manually verify the signature, you can use the function IsValid()

if ($jwt->IsValid()) {
    echo 'Valid!';
} else {
    echo 'Invalid!';
}

Read Data from the JWT

Reading data from a JWT that is validated, is pretty easy. Just call the function GetData(), which gives you an array which contains the data.

$sample_array = $jwt->GetData(); // $jwt->GetJWT(); $jwt->d();

If the JWT is not validated, the function just returns null

Set the JWT as a cookie

For web authentication, you can send the JWT as a cookie to the user. The cookie's lifetime is by default 14 days, other settings can be changed in the configuration.

To set the JWT as a cookie, just call SetJWT()

$jwt->SetJWT();

Get the JWT as a String

To do your own cookie handling, EasyJWT also allows you to get the value of the cookie as a string - without setting the cookie.

To get the JWT value, just call toString()

$jwt_value_string = $jwt->toString();

Removing the cookie

When you want to remove the JWT cookie from the user's browser (e.g. when the user logs out), you can just call RemoveJWT()

$jwt_value_string = $jwt->RemoveJWT();