PHP Classes

PHP JWT Token Generator and Manager: Generate and manage JWT authentication tokens

Recommend this page to a friend!
  Info   View files Example   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-11-30 (6 months ago) RSS 2.0 feedNot enough user ratingsTotal: 54 All time: 10,588 This week: 488Up
Version License PHP version Categories
jwt-manager-php-lib 1.0.0Custom (specified...7Web services, Security, PHP 7
Description 

Author

This package can generate and manage JWT authentication tokens.

It can generate a JWT token string from parameters that define the secret string, the context string, the expiry time, and the remaining time to require the token's renewal.

The package can also:

- Decode a token

- Check if a token is valid

- Check if a token did not expire

- Get the token expiry date

- Check if a token needs to be refreshed

Picture of Leonardo Di Sarli
  Performance   Level  
Name: Leonardo Di Sarli <contact>
Classes: 11 packages by
Country: Brazil Brazil
Innovation award
Innovation award
Nominee: 5x

Winner: 2x

Example

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use
JwtManager\JwtManager;

$secret = '77682b9441bb7daa7a1fa6eb7522b689';
$context = 'test';
$expire = 30; //expire token time
$renew = 10; //time left to expire token

$jwtManager = new JwtManager(
   
$secret,
   
$context,
   
$expire,
   
$renew
);

//Generate token
$tokenGenerated = $jwtManager->generate('test');
print(
'token: ' . $tokenGenerated);
echo
PHP_EOL;

//decode the token and return the data in array
$result = $jwtManager->decodePayload($tokenGenerated);
echo
'Decoded Token payload: ';
echo
PHP_EOL;

print_r($result);
echo
PHP_EOL;

//Verify if token is valid
$result = $jwtManager->isValid($tokenGenerated);
echo
'Is valid: '.$result;
echo
PHP_EOL;

//Check if the token is still valid
$result = $jwtManager->isOnTime($tokenGenerated);
echo
'Is on time: '.$result;
echo
PHP_EOL;

//Return the expire time that was set
$result = $jwtManager->getexpire();
echo
'Token expiration time: '.$result;
echo
PHP_EOL;

//Check if is needed generate new token
$result = $jwtManager->tokenNeedToRefresh($tokenGenerated);
echo
'Need to refresh token: '.$result;
echo
PHP_EOL;


Details

JWT Manager PHP

Latest Version codecov CI Build Downloads Old Downloads PRs Welcome Packagist License (custom server)

PHP library to manage JWT authentication

Installation

Release 7.0.0 Requires PHP 8.3

Release 6.0.0 Requires PHP 8.2

Release 5.0.0 Requires PHP 8.1

Release 4.0.0 Requires PHP 7.4

Release 3.0.0 Requires PHP 7.3

Release 2.0.0 Requires PHP 7.2

Release 1.0.0 Requires PHP 7.1

The recommended way to install is through Composer.

composer require not-empty/jwt-manager-php-lib

Usage

Generating a token

use JwtManager\JwtManager;
$secret = '77682b9441bb7daa7a1fa6eb7522b689';
$context = 'test';
$expire = 30;
$renew = 10;
$jwtManager = new JwtManager(
    $secret,
    $context,
    $expire,
    $renew
);
$tokenGenerated = $jwtManager->generate('test');
var_dump($tokenGenerated);

Dedoce the token and return the data

$result = $jwtManager->decodePayload($tokenGenerated);
var_dump($result);

Verify if token is valid

$result = $jwtManager->isValid($tokenGenerated);
var_dump($result);

Check if a token is on time

$result = $jwtManager->isOnTime($tokenGenerated);
var_dump($result);

Get the expiration time of a token

$result = $jwtManager->getexpire($tokenGenerated);
var_dump($result);

Check if need to refresh a token

$result = $jwtManager->tokenNeedToRefresh($tokenGenerated);
var_dump($result);

if you want an environment to run or test it, you can build and install dependences like this

docker build --build-arg PHP_VERSION=8.3-rc-cli -t not-empty/jwt-manager-php-lib:php83 -f contrib/Dockerfile .

Access the container

docker run -v ${PWD}/:/var/www/html -it not-empty/jwt-manager-php-lib:php83 bash

Verify if all dependencies is installed

composer install --no-dev --prefer-dist

and run

php sample/jwt-manager-sample.php

Development

Want to contribute? Great!

The project using a simple code. Make a change in your file and be careful with your updates! Any new code will only be accepted with all validations.

To ensure that the entire project is fine:

First you need to building a correct environment to install all dependences

docker build --build-arg PHP_VERSION=8.3-rc-cli -t not-empty/jwt-manager-php-lib:php83 -f contrib/Dockerfile .

Access the container

docker run -v ${PWD}/:/var/www/html -it not-empty/jwt-manager-php-lib:php83 bash

Install all dependences

composer install --dev --prefer-dist

Run all validations

composer check

Not Empty Foundation - Free codes, full minds


  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imagecontrib (4 files)
Files folder imagesample (1 file)
Files folder imagesrc (1 file)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpmd.xml Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file php.yml Data Auxiliary data

  Files folder image Files  /  contrib  
File Role Description
  Accessible without login Plain text file coverage-checker.php Example Example script
  Accessible without login Plain text file Dockerfile Data Auxiliary data
  Accessible without login Plain text file pre-commit Data Auxiliary data
  Accessible without login Plain text file setup.sh Data Auxiliary data

  Files folder image Files  /  sample  
File Role Description
  Accessible without login Plain text file jwt-manager-sample.php Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file JwtManager.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file JwtManagerTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:54
This week:0
All time:10,588
This week:488Up