Download the PHP package dersonsena/yii2-jwt-tools without Composer

On this page you can find all versions of the php package dersonsena/yii2-jwt-tools. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package yii2-jwt-tools

JWT Tools to Yii Framework 2

GitHub GitHub repo size Packagist Stars Packagist PHP Version Support (specify version) Packagist Downloads

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:

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

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.


All versions of yii2-jwt-tools with dependencies

PHP Build Version
Package Version
Requires php Version >=8
firebase/php-jwt Version ^5.2
yiisoft/yii2 Version ^2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package dersonsena/yii2-jwt-tools contains the following files

Loading the files please wait ....