2017 © Pedro Peláez
 

cakephp-plugin cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

image

admad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  • Monday, April 30, 2018
  • by ADmad
  • Repository
  • 14 Watchers
  • 114 Stars
  • 120,259 Installations
  • PHP
  • 10 Dependents
  • 2 Suggesters
  • 41 Forks
  • 0 Open issues
  • 12 Versions
  • 10 % Grown

The README.md

CakePHP JWT Authenticate plugin

Build Status Coverage Status 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)

Installation

composer require admad/cakephp-jwt-auth

Usage

Load the plugin using Cake's console:, (*3)

./bin/cake plugin load ADmad/JwtAuth

Configuration:

Setup AuthComponent:, (*4)

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

        $this->loadComponent('Auth', [
            'storage' => 'Memory',
            'authenticate' => [
                'ADmad/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 empty string to prevent getting a MissingRouteException.
            'loginAction' => '',
        ]);
    }

Working

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

  • HTTP_AUTHORIZATION environment variable:, (*6)

    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 customized using options header and prefix respectively., (*7)

  • The query string variable specified using parameter config:, (*8)

    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., (*9)

Known Issue

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

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

RewriteEngine On RewriteCond %{HTTP:Authorization} ^(.*) RewriteRule .* - [e=HTTP_AUTHORIZATION:%1], (*12)

or, (*13)

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1, (*14)

Token Generation

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

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

Ideally you should also specify the token expiry time using exp claim., (*17)

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., (*18)

Further reading

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

The Versions

30/04 2018

dev-master

9999999-dev http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication cakephp jwt authenticate

20/04 2018

2.3.2

2.3.2.0 http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication cakephp jwt authenticate

19/04 2018

2.3.1

2.3.1.0 http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication cakephp jwt authenticate

19/04 2018

dev-issue-64

dev-issue-64 http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication cakephp jwt authenticate

18/04 2018

2.3.0

2.3.0.0 http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication cakephp jwt authenticate

01/08 2017

2.2.0

2.2.0.0 http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication cakephp jwt authenticate

31/05 2017

2.1.0

2.1.0.0 http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication cakephp jwt authenticate

07/01 2017

2.0.3

2.0.3.0 http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

authentication cakephp jwt authenticate

17/11 2016

2.0.2

2.0.2.0 http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

authentication cakephp jwt authenticate

02/04 2016

2.0.1

2.0.1.0 http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

authentication cakephp jwt authenticate

18/02 2016

2.0.0

2.0.0.0 http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

authentication cakephp jwt authenticate

16/11 2015

1.0.0

1.0.0.0 http://github.com/ADmad/cakephp-jwt-auth

CakePHP plugin for authenticating using JSON Web Tokens

  Sources   Download

MIT

The Requires

 

authentication cakephp jwt authenticate