2017 © Pedro Peláez
 

library jwt

PHP7 library for JSON Web Tokens (JWT).

image

artisangang/jwt

PHP7 library for JSON Web Tokens (JWT).

  • Thursday, July 5, 2018
  • by artisangang
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

PHP-JWT

PHP7 library for JSON Web Tokens (JWT)., (*1)

[Standard]https://en.wikipedia.org/wiki/JSON_Web_Token#Standard_fields, (*2)

Installation

composer require artisangang/jwt

Requires PHP 7., (*3)

Usage

<?php


// create token

$token = Token::make([
        'key' => 'secret',
        'issuer' => 'artisangang',
        'expiry' => strtotime('+1 hour'),
        'issuedAt' => time(),
        'algorithm' => 'HS256'
    ])->get();


try {
    Token::validate($token, 'secret');
} catch (\Exception $e) {
    //InvalidArgumentException|UnexpectedValueException
    //InvalidSignatureException|BeforeValidException|TokenExpiredException
}

/**
 * or
 * Token::check($token, 'secret')
 * this will return true or false only
 */

//  decode token
// (new Token)->decode('token', 'key')

// token string to token object
// Token::break('token', 'key')

// jwt claims maping with Token Class
/**
 * [
 *    'iss' => 'issuer',
 *    'sub' => 'subject',
 *    'aud' => 'audience',
 *    'exp' => 'expiry',
 *    'nbf' => 'notBefore',
 *    'iat' => 'issuedAt',
 *    'jti' => 'identify',
 *    'typ' => 'type',
 *    'alg' => 'algorithm'
 *]
 */

Using methods, (*4)


$token = new Token; $token->setKey('secret); $token->setIssuer('who issued this token'); $token->setSubject('subject of token'); $token->setAudience('recipients'); // of in case of multiple audience //$token->setAudience('recipient1', 'recipient2', 'recipient3'); // this will work with unix timestamp $token->setExpiry(time() + 60); // this token cannot be used before $token->setNotBefore(time() + 10); // token issued at unix time stamp // Note: token cannot be used before issued at time $token->setIssuedAt(time()); $token->setIdentity('this must be unique'); $token->setType('jwt'); // suported algorithm: HS256,HS512,HS384 // for oppen ssl : RS256,RS384,RS512 $token->setAlgorithm('HS256'); // add custom claims to token $token->setClaim('user_id', 1); $token->setClaim('email', 'user@example.com'); // generate token based on claims $tokenString = $token->get();

Validating a token, (*5)


// use one from below methods try { // this will return array of claims $token = Token::validate('token string', 'your key'); // you may validate custom claims here } catch(\Exception $e) { //InvalidArgumentException -> some required argument is missing //UnexpectedValueException -> argument or segment value is malformed //InvalidSignatureException -> token signature not matched , Token is invalid //BeforeValidException -> token is used before issued at or not before time //TokenExpiredException -> token is expired } // or by using check, this will return bool if (!Token::check('token string', 'your key')) { // token is not valid }

For more information explorer JWT\Token.php., (*6)

Use openssl_pkcs12_read,openssl_get_privatekey to read key., (*7)

The Versions

05/07 2018

dev-master

9999999-dev

PHP7 library for JSON Web Tokens (JWT).

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires

by Harcharan Singh

api json jwt token

05/07 2018

v0.2

0.2.0.0

PHP7 library for JSON Web Tokens (JWT).

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires

by Harcharan Singh

api json jwt token

04/07 2018

v0.1

0.1.0.0

PHP7 library for JSON Web Tokens (JWT).

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires

by Harcharan Singh

api json jwt token