定制 admad/cakephp-jwt-auth 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

admad/cakephp-jwt-auth

最新稳定版本:3.0.0

Composer 安装命令:

composer require admad/cakephp-jwt-auth

包简介

CakePHP plugin for authenticating using JSON Web Tokens

README 文档

README

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

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.

统计信息

  • 总下载量: 661.64k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 163
  • 点击次数: 1
  • 依赖项目数: 14
  • 推荐数: 1

GitHub 信息

  • Stars: 160
  • Watchers: 18
  • Forks: 45
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-10-05