sergeymitr / simple-php-jwt
Simple JWT Authentication using PHP
Requires
- ext-json: *
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-26 15:37:23 UTC
README
The library serves a single purpose: making JWT easier to work with.
Requirements
- PHP 7.4 or higher.
Installation
Run this command to install the library via Composer:
composer require sergeymitr/simple-php-jwt
Usage
Creating and Encoding the Token
Use Token::create()
factory method to create a new token:
use Sergeymitr\SimpleJWT\Token; $token = Token::create();
The token doesn't hold any data, so we'll need to fill that out:
$token->setIssuer('Sergeymitr\SimpleJWT') ->setSubject('Test Sample') ->setAudience('PHPUnit') ->setExpiration(new \DateTime('2020-02-02')) ->setNotBefore(new \DateTime('2020-01-01')) ->setIssuedAt(new DateTime('2020-01-15')) ->setID('sample-id');
All the data is optional, you only fill out what you need. These methods define the claim names described by RFC 7519.
You can also add custom values to your token:
$token->setCustomPayload('key', 'value');
To encode the token you will need use the Token
object, and a secret string to encode it with.
use Sergeymitr\SimpleJWT\Encoder; $encoded_token = Encoder::do($token, 'secret');
The $encoded_token
will contain a string representation of the encoded and encrypted token.
Decoding the Token
To decode the token you'll need the string representation of the token, and the secret string you encoded it with:
use Sergeymitr\SimpleJWT\Decoder; $token = Decoder::do($encodedToken, 'secret');
The Token
object provides a number of "getter" methods you can use to retrieve the values,
which you can review in the interface TokenInterface
.