dev-master
9999999-devPlugin for Token Authentication
MIT
The Requires
- php >=5.3.0
- ext-mcrypt *
The Development Requires
by Marco Francaviglia
token
Plugin for Token Authentication
Note : This application is in development, (*1)
This is the plugin for make an authentication done with Tokens., (*2)
You can install the plugin by manually download, or by composer, (*3)
composer require falco442/cakephp-token-plugin
Put into the table you use for authentication model ('users') the fields 'token' (varchar(255)) and 'token_created' (datetime)., (*4)
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)
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
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)
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)
cake-root/app/Config/routes.php
by adding Router::parseExtensions('json','xml');
(or with the extensions you desires)cake-root/app/Controller/AppController.php
add the RequestHandler
component; it will parse the extension of the format (json, xml, ...)Router::mapResources()
, to be put in cake-root/app/Config/routes.php
Plugin for Token Authentication
MIT
token