Download the PHP package raphaelbsr/yii2-jwt-tools without Composer
On this page you can find all versions of the php package raphaelbsr/yii2-jwt-tools. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download raphaelbsr/yii2-jwt-tools
More information about raphaelbsr/yii2-jwt-tools
Files in raphaelbsr/yii2-jwt-tools
Package yii2-jwt-tools
Short Description An easy way to configure JWT authentication and validation on Yii Framework 2 Projects
License MIT
Informations about the package yii2-jwt-tools
JWT Tools to Yii Framework 2
JWT Tools is a toolbox that will help you to configure authentication with JWT token. Not only authentication but also signature validation, the famous secret key.
My biggest motivation to do this was because I didn't see a easy way to setup a simple JWT Validation with some helper functions. I always needed copy and past whole the code to a new project.
Follow the steps below to install and setup in your project.
Installation
The preferred way to install this extension is through composer.
To install, either run:
or add
to the require
section of your composer.json
file.
Usage
Configuration File
Let's guarantee somes application settings are correct. Open your config/web.php
and setup such as:
Controller
In your controller class, register the HttpBearerAuth behaviors in behaviors()
method, such as below:
NOTE: in this examples I used
Yii::$app->params['jwt']['secret']
to store my JWT Secret Key, but, I like a lot of the .env files and this information could be stored there
The JWTSignatureBehavior
will validate the JWT token sent by Authorization
HTTP Header. If there are some problem with your token this one it will throw one of Exceptions below:
-
UnauthorizedHttpException with message
Your request was made without an authorization token.
if HTTP Header doesn't exist or token is empty or null. -
UnauthorizedHttpException with message
Authentication token is expired.
if token is out of due. - UnauthorizedHttpException with message
The token signature is invalid.
if the token signature is invalid.
If for some reason you need to change the HTTP Header name (to be honest I can't see this scenario) you can change this one setting up the headerName
property, such as below:
In your login action you need to create a JWT Token to send your response. It's very easy create a token, see below:
Model Identity Class
At this point we know that the token is valid and we can decode this one to authenticate user.
I'm using here app/models/User
as my User Identity, so, let's implement the findIdentityByAccessToken()
method of the IdentityInterface interface:
If all ok, at this point you're able to authenticate with a valid JWT Token.
Demos
Generating a token
You can use the JWTTools methods to make specific things in your project. See some examples below:
This code will be return something like:
NOTE: the
->getPayload()
returns an instance of the JWTPayload.
Generating Token with an Active Record
You can insert the active record attributes in your payload using withModel()
method, like this:
This code will be return something like:
The sub
property is automatically override to $model->getPrimaryKey()
value, following the RFC7519 instructions.
Changing JWT Properties
You can change the JWT Properties (such as iss
, aud
etc) adding an array in second method parameter, as below:
Authors
- Kilderson Sena - Initial work - Yii Academy
See also the list of contributors who participated in this project.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Licence
This package is released under the MIT License. See the bundled LICENSE for details.