Giter VIP home page Giter VIP logo

cakephp-jwt-auth's Introduction

CakePHP JWT Authenticate plugin

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

Requirements

  • CakePHP 3.0+

Installation

composer require admad/cakephp-jwt-auth:1.0.x-dev

Usage

In your app's config/bootstrap.php add:

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

or 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()
    {
        parent::initialize();
        
        $this->loadComponent('Auth', [
            'authenticate', [
                'ADmad/JwtAuth.Jwt' => [
                    'parameter' => '_token',
                    'userModel' => 'Users',
                    'scope' => ['Users.active' => 1],
                    'fields' => [
                        'id' => 'id'
                    ]
                ]
            ],
            'unauthorizedRedirect' => false,
            // Config below is available since CakePHP 3.1.
            // It makes user info available in controller's beforeFilter() which is not possible in CakePHP 3.0.
            'checkAuthIn' => 'Controller.initialize',
        ]);
    }

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 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.

The payload of the token should either have key id or record. If id key exists it's value will be used to query against the primary key field of users table.

If record key exists it's value will be returned as user record. No check will be done against the database.

Additional Info

AuthComponent performs it's authentication routine for stateless auth after your controller's beforeFilter() has run. So trying to get user info using $this->Auth->user() in beforeFilter() will always return null.

As of CakPHP 3.1 though you can set a new config option checkAuthIn to Controller.initialize which makes AuthComponent do the authentication routine before controller's beforeFilter() is called.

Further reading

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

cakephp-jwt-auth's People

Contributors

admad avatar ceeram avatar curtisgibby avatar

Watchers

Johnson Antunes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.