2017 © Pedro Peláez
 

library jwt

A JWT (JSON Web Token) Encoding & Decoding library

image

psecio/jwt

A JWT (JSON Web Token) Encoding & Decoding library

  • Wednesday, May 13, 2015
  • by enygma
  • Repository
  • 7 Watchers
  • 104 Stars
  • 169,102 Installations
  • PHP
  • 4 Dependents
  • 1 Suggesters
  • 10 Forks
  • 11 Open issues
  • 9 Versions
  • 5 % Grown

The README.md

JWT (JSON Web Token) Creation and Decoding Library

Build Status, (*1)

This library allows for the creation and decoding of JWT (JSON Web Tokens)., (*2)

Installation

This tool can be installed via Composer:, (*3)

{
    "require": {
        "psecio/jwt": "1.*"
    }
}

Example Usage

In the example below, the JWT object is created and a Header instance is assigned (required). The JWt object is then assigned several claims: issuer, audience, issued at and not before to define data and how it could be processed. The encode method is then called with the key and a resulting JWT-formatted string is returned., (*4)

NOTE: The JWT token will be generated in the order the claims are provided. No sorting is done in the background., (*5)

The decode method can then be called on the data along with the key to return an object matching the state of the jwt object., (*6)

issuer('http://example.org')
    ->audience('http://example.com')
    ->issuedAt(1356999524)
    ->notBefore(1357000000)
    ->expireTime(time()+3600)
    ->jwtId('id123456')
    ->type('https://example.com/register');

$result = $jwt->encode();
echo 'ENCODED: '.print_r($result)."\n\n";
echo 'DECODED: '.var_export($jwt->decode($result), true);

?>

Encryption via OpenSSL

The JWT Library also supports encryption of the resulting JWT-formatted string. Here's an example of it in use:, (*7)

issuer('http://example.org')
    ->audience('http://example.com')
    ->issuedAt(1356999524)
    ->notBefore(1357000000)
    ->expireTime(time()+3600)
    ->jwtId('id123456')
    ->type('https://example.com/register');

$result = $jwt->encrypt('AES-256-CBC', '1234567812345678', $encryptKey);

echo 'ENCRYPTED: '.var_export($result, true)."\n";
echo "DECRYPTED: ".var_export($jwt->decrypt($result, 'AES-256-CBC', '1234567812345678', $encryptKey), true)."\n";

?>

Custom Claim values

You can also add your own custom claim values to the JWT payload using the custom method. The first paramater is the value and the second is the claim "type" (key):, (*8)

custom('foobar', 'custom-claim');

// Or, you can add more than one at the same time with an array
$jwt->custom(array(
    'custom-claim' => 'foorbar',
    'key1' => 'value1'
));

$result = $jwt->encode();
echo 'ENCODED: '.print_r($result)."\n\n";
echo 'DECODED: '.var_export($jwt->decode($result), true);
?>

You can use any of the OpenSSL cypher methods provided by the openssl_get_cipher_methods on your system., (*9)

Supported Claim Types

  • Audience (aud)
  • Expire Time (exp)
  • Issued At (iat)
  • Issuer (iss)
  • JwtId (jit)
  • Not Before (nbf)
  • Subject (sub)
  • Private

Hashing types

By default this JWT tool uses HMAC hashing (HS256) to generate the signature for the request. There are other options for this that will use the OpenSSL functionality to let you use public and private keys for these methods:, (*10)

  • HS256
  • HS384
  • HS512
  • ES256
  • ES384
  • ES512
  • RS256
  • RS384
  • RS512

You cannot use a simple text string for the key like you can with HMAC hashing, so you must provide a valid key instance for the library to use. Here's an example using a .pem private key file and the RS256 hashing:, (*11)

setAlgorithm('RS256');

// or you can define the hash algorithm on the init too:
$header = new \Psecio\Jwt\Header($key, 'RS256');
?>

An exception (\Psecio\Jwt\Exception\InvalidKeyException) will be thrown if the key is invalid and cannot be used in signing the request. If there is an error during the actual signing of the message, you will be thrown a \Psecio\Jwt\Exception\SignatureErrorException., (*12)

Documentation for JSON Web Tokens

The Versions

13/05 2015

dev-master

9999999-dev https://github.com/psecio/jwt.git

A JWT (JSON Web Token) Encoding & Decoding library

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json jwt token web

13/05 2015

1.7

1.7.0.0 https://github.com/psecio/jwt.git

A JWT (JSON Web Token) Encoding & Decoding library

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json jwt token web

11/02 2015

1.6

1.6.0.0 https://github.com/psecio/jwt.git

A JWT (JSON Web Token) Encoding & Decoding library

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json jwt token web

10/02 2015

1.5

1.5.0.0 https://github.com/psecio/jwt.git

A JWT (JSON Web Token) Encoding & Decoding library

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json jwt token web

14/11 2014

1.4

1.4.0.0 https://github.com/psecio/jwt.git

A JWT (JSON Web Token) Encoding & Decoding library

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

json jwt token web

30/07 2014

1.3

1.3.0.0 https://github.com/psecio/jwt.git

A JWT (JSON Web Token) Encoding & Decoding library

  Sources   Download

MIT

The Requires

  • php >=5.3.1

 

The Development Requires

json jwt token web

20/07 2014

1.2

1.2.0.0 https://github.com/psecio/jwt.git

A JWT (JSON Web Token) Encoding & Decoding library

  Sources   Download

MIT

The Requires

  • php >=5.3.1

 

The Development Requires

json jwt token web

20/07 2014

1.1

1.1.0.0 https://github.com/psecio/jwt.git

A JWT (JSON Web Token) Encoding & Decoding library

  Sources   Download

MIT

The Requires

  • php >=5.3.1

 

The Development Requires

json jwt token web

14/07 2014

1.0

1.0.0.0 https://github.com/psecio/jwt.git

A JWT (JSON Web Token) Encoding & Decoding library

  Sources   Download

MIT

The Requires

  • php >=5.3.1

 

json jwt token web