2017 © Pedro Peláez
 

cakephp-plugin cakephp-token-plugin

Plugin for Token Authentication

image

falco442/cakephp-token-plugin

Plugin for Token Authentication

  • Tuesday, October 4, 2016
  • by falco442
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Note : This application is in development, (*1)


Cakephp-TokenPlugin

This is the plugin for make an authentication done with Tokens., (*2)

Requirements

  • CakePHP 2.x

Installation

Getting plugin

You can install the plugin by manually download, or by composer, (*3)

composer require falco442/cakephp-token-plugin

Preparing tables

Put into the table you use for authentication model ('users') the fields 'token' (varchar(255)) and 'token_created' (datetime)., (*4)

Loading plugin

Load the plugin by calling, (*5)

CakePlugin::loadAll();

or, (*6)

CakePlugin::load('TokenAuth');

and put the Authentication object in your AppController.php:, (*7)

public $components = [
    '...',
    'RequestHandler',                   // suggested if you want REST
    'Auth'=>[
        'authenticate'=>[
            'TokenAuth.Token'
        ],
        'unauthorizedRedirect'=>false   // suggested if you want REST
    ]
];

Keep in mind that you can customize the Authentication object with the same parameters you would have used with FormAuthenticate, like userModel and fields, (*8)

Use

In Controller

You can set up the login action for your controller; for example, the action login() in UsersController.php:, (*9)

public function login(){
    $user = $this->Auth->identify($this->request,$this->response);
    $this->set(compact('user'));
    $this->set('_serialize',['user']);
}

Since the token authentication is done mainly for API applications, all you need is to retrieve the $user object that contains the new token that TokenAuth automatically generates. This token will be used to do all the calls to the actions that you don't want to be publicly accessible., (*10)

If you want an action to be public, simply use, (*11)

$this->Auth->allow(array('action-name'));

in the beforeFilter() method in respective controller., (*12)

The non-public routes that a client will call shall be of the form, (*13)

GET /uri.json?token=token-received

Reset token

You can reset token by calling the shell, (*14)

cd cake-root ./Console/cake TokenAuth.token refresh

Note: * the reset token task will take '-15 days' as base token life, but you can customize the shell * the shell take the model User as base, but you can set any model you like, (*15)

Type in console, (*16)

cd cake-root ./Console/cake TokenAuth.token refresh --help

to get some help, (*17)

Useful info

Since we use (mainly) token authentication for api web applications, it is useful to set REST in CakePHP (see this page)., (*18)

This is done with simple steps:, (*19)

  • modify the cake-root/app/Config/routes.php by adding Router::parseExtensions('json','xml'); (or with the extensions you desires)
  • in cake-root/app/Controller/AppController.php add the RequestHandler component; it will parse the extension of the format (json, xml, ...)
  • if you want REST you can map the resources (as this page says) with the method Router::mapResources(), to be put in cake-root/app/Config/routes.php

The Versions

04/10 2016

dev-master

9999999-dev

Plugin for Token Authentication

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-mcrypt *

 

The Development Requires

by Marco Francaviglia

token