2017 © Pedro Peláez
 

library micro-jwt

Yii2 micro service component for auth

image

ngochip/micro-jwt

Yii2 micro service component for auth

  • Sunday, November 26, 2017
  • by ngochip
  • Repository
  • 1 Watchers
  • 0 Stars
  • 23 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

micro-jwt

jwt for Yii2, (*1)

This extension provides the JWT integration for the Yii framework 2.0 (requires PHP 5.5+). It includes basic HTTP authentication support., (*2)

Installation

Package is available on Packagist, you can install it using Composer., (*3)

composer require ngochip/micro-jwt

Dependencies

Basic usage

  1. Create RSA key:
openssl genrsa -des3 -out private.pem 2048

Enter pass phrase for private.pem: [YOUR_PASSPARSE]
Verifying - Enter pass phrase for private.pem:[YOUR_PASSPARSE]

Export the RSA Public Key to a File, (*4)

openssl rsa -in private.pem -outform PEM -pubout -out public.pem

Exports the Private Key to PEM File, (*5)

openssl rsa -in private.pem -out private_unencrypted.pem -outform PEM

2.Add jwt component to your configuration file,, (*6)

In Authorization server:, (*7)

'jwt' => [
      'class' => 'ngochip\jwt\Jwt',
      'privateKey'  => __DIR__.'/../certificate/private.pem', //private key for sign (only setup in authorization server)
      'publicKey'   => __DIR__.'/../certificate/public.pem', //public key for verify in client.
      'passparse'   => '1234', //pass parse private key
      'ttl'       => 60 * 60, //time to live for token
      'ttl_refresh'   => 60 * 90, //time to live for refreshToken
      'redis_config'  => [
        'host'  => '127.0.0.1', // blacklist server address (redis server)
        'port'    => 6379, //redis port
        'database'  => 10,
        'password'  => NULL //password for AUTH redis server
      ]
  ],

In Client (other server), (*8)

'jwt' => [
      'class' => 'ngochip\jwt\Jwt',
      'publicKey'   => __DIR__.'/../certificate/public.pem', //public key for verify in client.
      'ttl'       => 60 * 60, //time to live for token
      'ttl_refresh'   => 60 * 90, //time to live for refreshToken
      'issuer'        => 'http://auth.domain.com/api/', //Auth Server Address.
      'redis_config'  => [
        'host'  => '127.0.0.1', // blacklist server address (redis server)
        'port'    => 6379, //redis port
        'database'  => 10,
        'password'  => NULL //password for AUTH redis server
      ]
  ],

Creating (in Authorization server)

Just use the builder to create a new JWT/JWS tokens:, (*9)

$userInfo = [
  'id' => 1,
  'username' => 'admin',
  'email' => 'admin@domain.com',
  'roles' => ['create_post','delete_user']
];
$token = Yii::$app->jwt->getToken($userInfo); //create token
Yii::$app->jwt->setToken($token); //assign Token
$newToken = Yii::$app->jwt->refreshToken(); //refresh token when expried

Use in other server (public key only)

Yii::$app->jwt->getTokenFromHeader(); //get token from Header and set to Object;
Yii::$app->jwt->verify(); //verify token, return bool;
Yii::$app->jwt->getInfo(); //get all info in tokenKey (not verify, only get from token). should be call after verified.
Yii::$app->jwt->getInfo('exp'); //extract claim from token, will return expiry time;
Yii::$app->jwt->getHeader(); //get all header in token.

Verifying

We can easily validate if the token is valid (using the previous token as example):, (*10)

Yii::$app->jwt->getTokenFromHeader(); //get token from Header Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9....
Yii::$app->jwt->verify(); //verify token, return true if verify success;

The Versions

26/11 2017

dev-master

9999999-dev

Yii2 micro service component for auth

  Sources   Download

MIT

The Requires

 

jwt yii2 ngochip

11/11 2017

1.0.2

1.0.2.0

Yii2 micro service component for auth

  Sources   Download

MIT

The Requires

 

jwt yii2 ngochip

11/11 2017

1.0.1

1.0.1.0

Yii2 micro service component for auth

  Sources   Download

MIT

The Requires

 

jwt yii2 ngochip

10/11 2017

v1.0

1.0.0.0

Yii2 micro service component for auth

  Sources   Download

MIT

The Requires

 

jwt yii2 ngochip