2017 © Pedro Peláez
 

cakephp-plugin cakephp-jwt-auth

cake php jwt auth with password

image

sonnt/cakephp-jwt-auth

cake php jwt auth with password

  • Friday, May 12, 2017
  • by sonnt0411
  • Repository
  • 1 Watchers
  • 0 Stars
  • 18 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

CakePHP JWT Authenticate plugin

Build Status Coverage Total Downloads License, (*1)

Plugin containing AuthComponent's authenticate class for authenticating using JSON Web Tokens. You can read about JSON Web Token specification in detail here., (*2)

Requirements

  • CakePHP 3.1+

Installation

composer require sonnt/cakephp-jwt-auth

Usage

In your app's config/bootstrap.php add:, (*3)

// In config/bootstrap.php
Plugin::load(‘Sonnt/JwtAuth');

or using cake's console:, (*4)

./bin/cake plugin load ADmad/JwtAuth

Configuration:

Setup AuthComponent:, (*5)

    // In your controller, for e.g. src/Api/AppController.php
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('Auth', [
            'storage' => 'Memory',
            'authenticate' => [
                ‘Sonnt/JwtAuth.Jwt' => [
                    'userModel' => 'Users',
                    'fields' => [
                        'username' => 'id'
                    ],

                    'parameter' => 'token',

                    // Boolean indicating whether the "sub" claim of JWT payload
                    // should be used to query the Users model and get user info.
                    // If set to `false` JWT's payload is directly returned.
                    'queryDatasource' => true,
                ]
            ],

            'unauthorizedRedirect' => false,
            'checkAuthIn' => 'Controller.initialize',

            // If you don't have a login action in your application set
            // 'loginAction' to false to prevent getting a MissingRouteException.
            'loginAction' => false
        ]);
    }

Working

The authentication class checks for the token in two locations:, (*6)

  • HTTP_AUTHORIZATION environment variable:, (*7)

    It first checks if token is passed using Authorization request header. The value should be of form Bearer <token>. The Authorization header name and token prefix Bearer can be customzied using options header and prefix respectively., (*8)

    Note: Some servers don't populate $_SERVER['HTTP_AUTHORIZATION'] when Authorization header is set. So it's upto you to ensure that either $_SERVER['HTTP_AUTHORIZATION'] or $_ENV['HTTP_AUTHORIZATION'] is set., (*9)

    For e.g. for apache you could use the following:, (*10)

    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
    
  • The query string variable specified using parameter config:, (*11)

    Next it checks if the token is present in query string. The default variable name is token and can be customzied by using the parameter config shown above., (*12)

Token Generation

You can use \Firebase\JWT\JWT::encode() of the firebase/php-jwt lib, which this plugin depends on, to generate tokens., (*13)

The payload should have the "sub" (subject) claim whos value is used to query the Users model and find record matching the "id" field., (*14)

You can set the queryDatasource option to false to directly return the token's payload as user info without querying datasource for matching user record., (*15)

Further reading

For an end to end usage example check out this blog post by Bravo Kernel., (*16)

The Versions

12/05 2017

dev-master

9999999-dev

cake php jwt auth with password

  Sources   Download

MIT

The Requires

 

The Development Requires

by nguyen thanh son

12/05 2017

1.0

1.0.0.0

cake php jwt auth with password

  Sources   Download

MIT

The Requires

 

The Development Requires

by nguyen thanh son