2017 © Pedro Peláez
 

library jwt

Ultra lightweight JSON web token (JWT) library for PHP5.5+.

image

adhocore/jwt

Ultra lightweight JSON web token (JWT) library for PHP5.5+.

  • Sunday, July 22, 2018
  • by adhocore
  • Repository
  • 1 Watchers
  • 53 Stars
  • 297 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 8 Versions
  • 1385 % Grown

The README.md

adhocore/jwt

If you are new to JWT or want to refresh your familiarity with it, please check jwt.io, (*1)

Latest Version Build Scrutinizer CI Codecov branch StyleCI Software License Tweet Support , (*2)

  • Lightweight JSON Web Token (JWT) library for PHP7, PHP8 and beyond.
  • Zero dependency (no vendor bloat).
  • If you still use PHP5.6, use version 0.1.2

Installation

# PHP7.x, PHP8.x
composer require adhocore/jwt

# PHP5.6 (deprecated)
composer require adhocore/jwt:0.1.2

# For PHP5.4-5.5 (deprecated), use version 0.1.2 with a polyfill for https://php.net/hash_equals

Features

  • Six algorithms supported:
'HS256', 'HS384', 'HS512', 'RS256', 'RS384', 'RS512'
  • kid support.
  • Leeway support 0-120 seconds.
  • Timestamp spoofing for tests.
  • Passphrase support for RS* algos.

Usage

use Ahc\Jwt\JWT;

// Instantiate with key, algo, maxAge and leeway.
$jwt = new JWT('secret', 'HS256', 3600, 10);

Only the key is required. Defaults will be used for the rest:, (*3)

$jwt = new JWT('secret');
// algo = HS256, maxAge = 3600, leeway = 0

For RS* algo, the key should be either a resource like below:, (*4)

$key = openssl_pkey_new([
    'digest_alg' => 'sha256',
    'private_key_bits' => 1024,
    'private_key_type' => OPENSSL_KEYTYPE_RSA,
]);

OR, a string with full path to the RSA private key like below:, (*5)

$key = '/path/to/rsa.key';

// Then, instantiate JWT with this key and RS* as algo:
$jwt = new JWT($key, 'RS384');

Pro You dont need to specify pub key path, that is deduced from priv key., (*6)

Generate JWT token from payload array:, (*7)

$token = $jwt->encode([
    'uid'    => 1,
    'aud'    => 'http://site.com',
    'scopes' => ['user'],
    'iss'    => 'http://api.mysite.com',
]);

Retrieve the payload array:, (*8)

$payload = $jwt->decode($token);

Oneliner:, (*9)

$token   = (new JWT('topSecret', 'HS512', 1800))->encode(['uid' => 1, 'scopes' => ['user']]);
$payload = (new JWT('topSecret', 'HS512', 1800))->decode($token);

Pro, (*10)

Can pass extra headers into encode() with second parameter:, (*11)

$token = $jwt->encode($payload, ['hdr' => 'hdr_value']);

Test mocking

Spoof time() for testing token expiry:, (*12)

$jwt->setTestTimestamp(time() + 10000);

// Throws Exception.
$jwt->parse($token);

Call again without parameter to stop spoofing time():, (*13)

$jwt->setTestTimestamp();

Examples with kid

$jwt = new JWT(['key1' => 'secret1', 'key2' => 'secret2']);

// Use key2
$token = $jwt->encode(['a' => 1, 'exp' => time() + 1000], ['kid' => 'key2']);

$payload = $jwt->decode($token);

$token = $jwt->encode(['a' => 1, 'exp' => time() + 1000], ['kid' => 'key3']);
// -> Exception with message Unknown key ID key3

Stabillity

The library is now marked at version 1.*.* as being stable in functionality and API., (*14)

Integration

Phalcon

Check adhocore/phalcon-ext. , (*15)

Consideration

Be aware of some security related considerations as outlined here which can be valid for any JWT implementations., (*16)

The Versions

22/07 2018

dev-master

9999999-dev

Ultra lightweight JSON web token (JWT) library for PHP5.5+.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Jitendra Adhikari

auth jwt token jwt-php json-web-token

14/07 2018

v0.0.6

0.0.6.0

Ultra lightweight JSON web token (JWT) library for PHP5.5+.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Jitendra Adhikari

auth jwt token jwt-php json-web-token

14/07 2018

v0.0.5

0.0.5.0

Ultra lightweight JSON web token (JWT) library for PHP7.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Jitendra Adhikari

auth jwt token jwt-php json-web-token

24/06 2018

v0.0.4

0.0.4.0

Ultra lightweight JSON web token (JWT) library for PHP7.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Jitendra Adhikari

auth jwt token jwt-php json-web-token

16/06 2018

v0.0.3

0.0.3.0

Ultra lightweight JSON web token (JWT) library for PHP7.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Jitendra Adhikari

auth jwt token jwt-php json-web-token

24/10 2017

dev-develop

dev-develop

Ultra lightweight JSON web token (JWT) library for PHP7.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Jitendra Adhikari

auth jwt token jwt-php json-web-token

14/04 2017

v0.0.2

0.0.2.0

Ultra lightweight JSON web token (JWT) library for PHP7.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Jitendra Adhikari

auth jwt token jwt-php json-web-token

13/04 2017

v0.0.1

0.0.1.0

Ultra lightweight JSON web token (JWT) library for PHP7.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Jitendra Adhikari

auth jwt token jwt-php json-web-token