emog / phalcon-jwt-auth
此包已被废弃,不再维护。没有建议的替代包。
此包的最新版本(1.0.0)没有提供许可证信息。
为Phalcon Micro提供简单JWT中间件,用于处理无状态身份验证
1.0.0
2017-08-30 23:37 UTC
Requires
- php: >=5.6
- ext-phalcon: ^3.0
- firebase/php-jwt: ^4.0
This package is auto-updated.
Last update: 2022-08-30 12:23:07 UTC
README
为Phalcon Micro提供简单JWT中间件,用于处理无状态身份验证。
安装
$ composer require emog/phalcon-jwt-auth
或在您的composer.json中
{
"require": {
"emog/phalcon-jwt-auth" : "dev-master"
}
}
然后运行
$ composer update
用法
配置 - 加载配置服务
在config.ini中或任何配置文件中
[jwtAuth] ; JWT Secret Key secretKey = 923753F2317FC1EE5B52DF23951B ; JWT default Payload ;; expiry time in minutes payload[exp] = 1440 payload[iss] = phalcon-jwt-auth ; Micro Applications do not have a controller or dispatcher ; so to know the resource being called we have to check the actual URL. ; If you want to disable the middleware on certain routes or resource: ;; index ignoreUri[] = / ;; regex pattern with http methods ignoreUri[] = regex:/application/ ignoreUri[] = regex:/users/:POST,PUT ;; literal strings ignoreUri[] = /auth/user:POST,PUT ignoreUri[] = /auth/application
在bootstrap或index文件中
use Phalcon\Mvc\Micro; use Phalcon\Config\Adapter\Ini as ConfigIni; use Phalcon\Di\FactoryDefault; use EmoG\Phalcon\Auth\Middleware\Micro as AuthMicro; // set default services $di = new FactoryDefault(); /** * IMPORTANT: * You must set "config" service that will load the configuration file. */ $config = new ConfigIni( APP_PATH . "app/config/config.ini"); $di->set( "config", function () use($config) { return $config; } ); $app = new Micro($di); // AUTH MICRO $auth = new AuthMicro($app); $app->handle();
配置 - 不想使用配置文件?那么传递配置代替
在bootstrap或index文件中
use Phalcon\Mvc\Micro; use Phalcon\Config\Adapter\Ini as ConfigIni; use Phalcon\Di\FactoryDefault; use EmoG\Phalcon\Auth\Middleware\Micro as AuthMicro; // set default services $di = new FactoryDefault(); $app = new Micro($di); // SETUP THE CONFIG $authConfig = [ 'secretKey' => '923753F2317FC1EE5B52DF23951B1', 'payload' => [ 'exp' => 1440, 'iss' => 'phalcon-jwt-auth' ], 'ignoreUri' : [ '/', 'regex:/application/', 'regex:/users/:POST,PUT', '/auth/user:POST,PUT', '/auth/application' ] ]; // AUTH MICRO $auth = new AuthMicro($app, $authConfig); $app->handle();
身份验证
要通过http进行认证请求,您需要设置以下授权头
Authorization: Bearer {yourtokenhere}
或将令牌作为查询字符串传递
?token={yourtokenhere}
回调
默认情况下,如果身份验证失败,中间件将停止路由的执行并立即返回401未授权的响应。如果您想添加自己的处理程序
$auth->onUnauthorized(function($authMicro, $app) { $response = $app["response"]; $response->setStatusCode(401, 'Unauthorized'); $response->setContentType("application/json"); // to get the error messages $response->setContent(json_encode([$authMicro->getMessages()[0]])); $response->send(); // return false to stop the execution return false; });
如果您想在身份验证上进行额外检查,例如根据令牌签发日期有意使令牌过期,您可以这样操作
$auth->onCheck(function($auth) { // to get the payload $data = $auth->data(); if($data['iat'] <= strtotime('-1 day')) ) { // return false to invalidate the authentication return false; } });
Auth服务
您可以通过调用“auth”服务来访问中间件。
print_r( $app['auth']->data() ); print_r( $app->getDI()->get('auth')->data('email') ); // in your contoller print_r( $this->auth->data() );
如果您想更改服务名称
AuthMicro::$diName = 'jwtAuth';
创建令牌
在您的控制器或路由处理程序中
$payload = [ 'sub' => $user->id, 'email' => $user->email, 'username' => $user->username, 'role' => 'admin', 'iat' => time(), ]; $token = $this->auth->make($payload);
访问认证用户/数据
在您的控制器或路由处理程序中
echo $this->auth->id(); // will look for sub or id payload echo $this->auth->data(); // return all payload echo $this->auth->data('email');
扩展
如果您想添加自己的中间件或进行一些实验
EmoG\Phalcon\Auth\Auth.php and its adapters - does all the authentication EmoG\Phalcon\Auth\TokenGetter\TokenGetter.php and its adapters - does the parsing or getting of token
JWT
Phalcon JWT Auth使用Firebase JWT库。要了解更多关于它以及JSON Web Tokens的一般信息,请访问:https://github.com/firebase/php-jwt https://jwt.net.cn/introduction/
如果您使用的是php 7,可以使用兼容的Dmkit版本https://github.com/dmkit/phalcon-jwt-auth