2017 © Pedro Peláez
 

library pjwt

PHP implementation of JSON Web Token (JWT). It provides a simple way to create, sign and verify JWT.

image

iachilles/pjwt

PHP implementation of JSON Web Token (JWT). It provides a simple way to create, sign and verify JWT.

  • Tuesday, November 25, 2014
  • by iAchilles
  • Repository
  • 1 Watchers
  • 3 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 7 % Grown

The README.md

Build Status AGPL v3, (*1)

pJWT

PHP implementation of JSON Web Token (JWT). It provides a simple way to create, sign and verify JWT., (*2)

The following features are supported: - Built-in validation for the JWT claims (iat, nbf, exp, jti). - Symmetric and asymmetric algorithms for protecting integrity:, (*3)

Symmetric Asymmetric
HS256 RS256
HS384 RS384
HS512 RS512

Requirements

PHP 5.4.0 or above., (*4)

Installation

Use composer to install pJWT:, (*5)

composer require iachilles/pjwt

Code examples

  1. Creating JWT, (*6)

    • by using symmetric algorithm HS256:
    $claims = ['iat' => time(), 'nbf' => time(), 'exp' => strtotime('+1 day'), 'iss' => 'domain.com', 'uid' => 1];
    $headers = ['alg' => 'HS256', 'typ' => 'JWT'];
    $jws = new Jws($headers, $claims);
    $jws->privateKey = 'YoUr_SeCrEt';
    $jws->sign(); //Returns URL-safe string representation of the digitally signed JWT. This encoded JWT can be sent to a user.
    
  • by using asymmetric algorithm RS256:, (*7)

    $claims = ['iat' => time(), 'nbf' => time(), 'exp' => strtotime('+1 day'), 'iss' => 'domain.com', 'uid' => 1];
    $headers = ['alg' => 'RS256', 'typ' => 'JWT'];
    $jws = new Jws($headers, $claims);
    $jws->privateKey = 'file:///path/to/private/key.pem'; //Path to the PEM encoded private key.
    $jws->sign(); //Returns URL-safe string representation of the digitally signed JWT. This encoded JWT can be sent to a user.
    

    If the private key is encrypted with a password, you can use the following format:, (*8)

    $jws->privateKey = ['file:///path/to/private/key.pem', 'pAsSwOrd'];
    
  • with protection from replay attacks. In order to protect from replay attacks, you can set 'jti' claim to TRUE during creation JWT., (*9)

    $claims = ['jti' => true, 'iat' => time(), 'nbf' => time(), 'exp' => strtotime('+1 day')];
    $headers = ['alg' => 'RS256', 'typ' => 'JWT'];
    $jws = new Jws($headers, $claims);
    
  1. Decoding and verifying JWT, (*10)

    $encodedJwt = 'abcdef.ghijklm.nopqrstuvw';
    $jws = Jws::parse($encodedJwt);
    $jws->getPayload()->issuedAt; //Access to the registered JWT claims
    $jws->getPayload()->getCustomClaim('user_id'); //Access to the custom claims.
    $jws->getHeader()->getAlgorithm(); //Access to the JOSE header parameters.
    

    Verifying signature, (*11)

    $encodedJwt = 'abcdef.ghijklm.nopqrstuvw';
    $jws = Jws::parse($encodedJwt);
    //For symmetric algorithm:
    $jws->privateKey = 'YoUr_SeCrEt';
    //For asymmetric algorithm:
    $jws->certificate = 'file:///path/to/certificate.pem'; //Path to the PEM encoded X.509 certificate.
    $jws->verify(); //TRUE if the signature is valid.
    

    If the signature is valid, you have to validate the JWT claims., (*12)

    $jws->getPayload()->verify(); //Returns TRUE if the JWT is valid, otherwise it returns a string that contains an error message.
    

    To validate "jti" value you need to create two anonymous functions, and pass them as arguments to the verify method., (*13)

    $setJti = function($jti)
    {
        //Writes "jti" value into storage. (E.g. Redis Db)
    };
    //This function must return TRUE if the given value exists in storage, false otherwise.
    $getJti = function($jti)
    {
       //...
    };
    $jws->getPayload()->verify($setJti, $getJti);
    

The Versions

25/11 2014

dev-master

9999999-dev https://github.com/iAchilles/pjwt

PHP implementation of JSON Web Token (JWT). It provides a simple way to create, sign and verify JWT.

  Sources   Download

AGPL License 3.0

The Requires

  • php >=5.4.0

 

by Igor Manturov Jr.

jwt token jws json web token

23/11 2014

1.0.0

1.0.0.0 https://github.com/iAchilles/pjwt

PHP implementation of JSON Web Token (JWT). It provides a simple way to create, sign and verify JWT.

  Sources   Download

AGPL License 3.0

The Requires

  • php >=5.4.0

 

by Igor Manturov Jr.

jwt token jws json web token