/yii2-jwt

JWT Integration for Yii 2

Primary LanguagePHPApache License 2.0Apache-2.0

Latest Stable Version Total Downloads License Build Status

JWT Integration For Yii 2

This extension provides the JWT integration for Yii 2 framework.

This is fork of sizeg/yii2-jwt package

Installation

Add the package to your composer.json:

{
    "require": {
        "bizley/jwt": "^2.0"
    }
}

and run composer update or alternatively run composer require bizley/jwt:^2.0

Basic usage

Add jwt component to your configuration file:

[
    'components' => [
        'jwt' => [
            'class' => \bizley\jwt\Jwt::class,
            'key' => ... // Secret key string or path to the public key file
        ],
    ],
],

Now you have got access to JWT library through Yii::$app->jwt and some helper methods like getBuilder(), getParser(), and getValidationData(). You can validate your JSON Web Token by using validateToken() method and check its signature with verifyToken().

REST authentication

Configure the authenticator behavior in controller.

class ExampleController extends Controller
{
    public function behaviors()
    {
        $behaviors = parent::behaviors();
        
        $behaviors['authenticator'] = [
            'class' => \bizley\jwt\JwtHttpBearerAuth::class,
        ];

        return $behaviors;
    }
}

For other configuration options refer to the Yii 2 Guide.

JWT Basic Usage

Please refer to the lcobucci/jwt Documentation.

JSON Web Tokens