2017 © Pedro Peláez
 

library jwt

A JWT implementation

image

emarref/jwt

A JWT implementation

  • Wednesday, September 13, 2017
  • by emarref
  • Repository
  • 11 Watchers
  • 75 Stars
  • 197,819 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 13 Forks
  • 6 Open issues
  • 12 Versions
  • 6 % Grown

The README.md

An implementation of the JSON Web Token (JWT) draft in PHP. See jwt.io for more information on JWT., (*1)

Build Status Scrutinizer Code Quality, (*2)

Features include:, (*3)

  • Token serialization
  • Token deserialization
  • Token verification
    • aud, exp, iss, nbf, sub claims are verified
  • Symmetric Encryption
    • NONE, HS256, HS384, HS512 algorithms supported
  • Asymmetric Encryption
    • RS256, RS384, RS512 algorithms supported
    • ES256, ES384, ES512, PS256, PS384, PS512 algorithms are planned

:warning: Versions of this library up to and including v1.0.2 are susceptible to timing attacks when using Symmetric encryption. See #20 for more information. Please update to >= v1.0.3 as soon as possible to address this vulnerability., (*4)

This library is not susceptible to a common encryption vulnerability., (*5)

Installation

composer require emarref/jwt

Usage

Create an instance of the Emarref\Jwt\Token class, then configure it., (*6)

use Emarref\Jwt\Claim;

$token = new Emarref\Jwt\Token();

// Standard claims are supported
$token->addClaim(new Claim\Audience(['audience_1', 'audience_2']));
$token->addClaim(new Claim\Expiration(new \DateTime('30 minutes')));
$token->addClaim(new Claim\IssuedAt(new \DateTime('now')));
$token->addClaim(new Claim\Issuer('your_issuer'));
$token->addClaim(new Claim\JwtId('your_id'));
$token->addClaim(new Claim\NotBefore(new \DateTime('now')));
$token->addClaim(new Claim\Subject('your_subject'));

// Custom claims are supported
$token->addClaim(new Claim\PublicClaim('claim_name', 'claim_value'));
$token->addClaim(new Claim\PrivateClaim('claim_name', 'claim_value'));

To use a token, create a JWT instance., (*7)

$jwt = new Emarref\Jwt\Jwt();

To retrieve the encoded token for transfer, call the serialize() method., (*8)

$algorithm = new Emarref\Jwt\Algorithm\None();
$encryption = Emarref\Jwt\Encryption\Factory::create($algorithm);
$serializedToken = $jwt->serialize($token, $encryption);

The $serializedToken variable now contains the unencrypted base64 encoded string representation of your token. To encrypt a token, pass an instance of Emarref\Jwt\Encryption\EncryptionInterface to the serialize() method as the second argument., (*9)

$algorithm = new Emarref\Jwt\Algorithm\Hs256('verysecret');
$encryption = Emarref\Jwt\Encryption\Factory::create($algorithm);
$serializedToken = $jwt->serialize($token, $encryption);

An example of using Rs256 encryption with a key pair can be found in the wiki - Using RS256 Encryption., (*10)

To use a serialized token, first deserialize it into a Emarref\Jwt\Token object using a Jwt instance., (*11)

$token = $jwt->deserialize($serializedToken);

To verify a token's claims, first set up the context that should be used to verify the token against. Encryption is the only required verification., (*12)

$context = new Emarref\Jwt\Verification\Context($encryption);
$context->setAudience('audience_1');
$context->setIssuer('your_issuer');

Then use the verify() method on a Jwt instance., (*13)

try {
    $jwt->verify($token, $context);
} catch (Emarref\Jwt\Exception\VerificationException $e) {
    echo $e->getMessage();
}

Testing

This library uses PHPUnit for unit testing. Make sure you've run composer install then call:, (*14)

./bin/phpunit ./test

Further Reading

The Versions

13/09 2017

dev-master

9999999-dev

A JWT implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

26/10 2016

dev-feature-json-slashes

dev-feature-json-slashes

A JWT implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

05/09 2016

1.0.3

1.0.3.0

A JWT implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

05/09 2016

dev-feature-timing-attack

dev-feature-timing-attack

A JWT implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

17/08 2016

1.0.2

1.0.2.0

A JWT implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

08/08 2016

dev-feature-exception-codes

dev-feature-exception-codes

A JWT implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

10/07 2016

1.0.1

1.0.1.0

A JWT implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

10/07 2016

dev-travis-ci

dev-travis-ci

A JWT implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

02/09 2015

1.0.0

1.0.0.0

A JWT implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

08/04 2015

0.1.1

0.1.1.0

A JWT implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

09/11 2014

0.1.0

0.1.0.0

A JWT implementation

  Sources   Download

MIT

The Development Requires

04/11 2014

0.0.1

0.0.1.0

A JWT implementation

  Sources   Download

MIT

The Requires

  • ext-openssl *

 

The Development Requires