kongka / cakephp-3-jwt-auth
CakePHP plugin for authenticating using JSON Web Tokens fork from admad/cakephp-jwt-auth
Installs: 27
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 44
Open Issues: 0
Type:cakephp-plugin
pkg:composer/kongka/cakephp-3-jwt-auth
Requires
- cakephp/cakephp: ^3.5
- firebase/php-jwt: ^5.0
Requires (Dev)
- cakephp/chronos: ^1.1
- phpunit/phpunit: ^5.7.14|^6.0
README
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 kongka/cakephp-3-jwt-auth
Usage
In your app's config/bootstrap.php add:
// In config/bootstrap.php Plugin::load('kongka/JwtAuth');
or using cake's console:
./bin/cake plugin load kongka/JwtAuth
Configuration:
Setup AuthComponent:
// In your controller, for e.g. src/Api/AppController.php public function initialize() { parent::initialize(); $this->loadComponent('Auth', [ 'storage' => 'Memory', 'authenticate' => [ 'kongka/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_AUTHORIZATIONenvironment variable:It first checks if token is passed using
Authorizationrequest header. The value should be of formBearer <token>. TheAuthorizationheader name and token prefixBearercan be customized using optionsheaderandprefixrespectively. -
The query string variable specified using
parameterconfig:Next it checks if the token is present in query string. The default variable name is
tokenand can be customzied by using theparameterconfig 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 should have the "sub" (subject) claim whose value is used to query the Users model and find record matching the "id" field.
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.