All Projects → ADmad → Cakephp Jwt Auth

ADmad / Cakephp Jwt Auth

Licence: mit
A CakePHP plugin for authenticating using JSON Web Tokens

Projects that are alternatives of or similar to Cakephp Jwt Auth

Aclmanager
Plugin to manage Acl for CakePHP 2.x
Stars: ✭ 59 (-61.44%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Hybridauth
CakePHP plugin for HybridAuth
Stars: ✭ 81 (-47.06%)
Mutual labels:  cakephp, cakephp-plugin
Public Cakephp Rest Api
CakePHP 3 plugin for building REST API services
Stars: ✭ 71 (-53.59%)
Mutual labels:  cakephp, jwt
Slug
Slugging for CakePHP
Stars: ✭ 32 (-79.08%)
Mutual labels:  cakephp, cakephp-plugin
Search
CakePHP: Easy model searching
Stars: ✭ 153 (+0%)
Mutual labels:  cakephp, cakephp-plugin
Flypie
Flysystem plugin for CakePHP
Stars: ✭ 35 (-77.12%)
Mutual labels:  cakephp, cakephp-plugin
Footprint
CakePHP plugin to allow passing currently logged in user to model layer.
Stars: ✭ 81 (-47.06%)
Mutual labels:  cakephp, cakephp-plugin
Cakepdf
CakePHP plugin for creating and/or rendering PDFs, supporting several popular PDF engines.
Stars: ✭ 360 (+135.29%)
Mutual labels:  cakephp, cakephp-plugin
Migrations
CakePHP database migrations plugin
Stars: ✭ 114 (-25.49%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Tinyauth
CakePHP TinyAuth plugin for an easy and fast user authentication and authorization. Single or multi role. DB or config file based.
Stars: ✭ 114 (-25.49%)
Mutual labels:  cakephp, cakephp-plugin
Debug kit
Debug Toolbar for CakePHP applications.
Stars: ✭ 858 (+460.78%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Ide Helper
IDE Helper plugin for CakePHP
Stars: ✭ 138 (-9.8%)
Mutual labels:  cakephp, cakephp-plugin
Users
Users Plugin for CakePHP
Stars: ✭ 488 (+218.95%)
Mutual labels:  cakephp, cakephp-plugin
Authorization
PSR7 Middleware for authorization
Stars: ✭ 50 (-67.32%)
Mutual labels:  cakephp, cakephp-plugin
Asset compress
An asset compression plugin for CakePHP. Provides file concatenation and a flexible filter system for preprocessing and minification.
Stars: ✭ 370 (+141.83%)
Mutual labels:  cakephp, cakephp-plugin
Webservice
Bringing the power of the CakePHP ORM to your favourite webservices
Stars: ✭ 79 (-48.37%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Tools
A CakePHP Tools plugin containing lots of useful helpers, behaviors, components, shells, ...
Stars: ✭ 325 (+112.42%)
Mutual labels:  cakephp, cakephp-plugin
Crud
Production-grade rapid controller development with built in love for API and Search
Stars: ✭ 339 (+121.57%)
Mutual labels:  cakephp, cakephp-plugin
Acl
Plugin for managing ACL in CakePHP applications.
Stars: ✭ 113 (-26.14%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Proffer
An upload plugin for CakePHP 3
Stars: ✭ 121 (-20.92%)
Mutual labels:  cakephp, cakephp-plugin

CakePHP JWT Authenticate plugin

Build Status Coverage Status Total Downloads License

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

Installation

composer require admad/cakephp-jwt-auth

Usage

Load the plugin using Cake's console:

./bin/cake plugin load ADmad/JwtAuth

Configuration:

Setup AuthComponent:

    // 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 false to prevent getting a MissingRouteException.
            'loginAction' => false
        ]);
    }

Working

The authentication class checks for the token in two locations:

  • HTTP_AUTHORIZATION environment variable:

    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.

  • The query string variable specified using parameter config:

    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.

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.

For e.g. for apache you could use the following:

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

or

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

Token Generation

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

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

Ideally you should also specify the token expiry time using exp claim.

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.

Further reading

For an end to end usage example check out this blog post by Bravo Kernel.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].