thamtech / yii2-jsonrpc-jwsauth
JWS Token 在 JSON RPC 2.0 上的身份验证
Requires
- php: >=5.4.0
- cranetm/yii2-json-rpc-2.0: dev-master
- namshi/jose: ^6.0
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-08-29 03:47:39 UTC
README
一个通过 JSON RPC 2.0 处理签名访问令牌身份验证的扩展。
此库通过 yii2-json-rpc-2.0 与你的控制器接口,以提供 JSON RPC 2.0 通信,并通过 namshi/jose 生成签名 JWS 令牌。
有关许可信息,请查看 LICENSE 文件。
安装
通过 composer 安装此扩展是首选方式。
运行以下命令:
php composer.phar require --prefer-dist thamtech/yii2-jsonrpc-jwsauth
或添加以下内容到你的 composer.json
文件的 require
部分:
"thamtech/yii2-jsonrpc-jwsauth": "*"
集成
-
使用 OpenSSL 生成密钥对,并将密钥存储在 public.pem 和 private.pem 中。
-
在你的站点配置中添加 JwsManager 应用组件
return [ 'components' => [ 'jwsManager' => [ 'class' => 'thamtech\jwsauth\components\JwsManager', 'pubkey' => '@app/config/keys/jwsauth/public.pem', 'pvtkey' => '@app/config/keys/jwsauth/private.pem', // The settings below are optional. Defaults will be used if not set here. //'encoder' => 'Namshi\JOSE\Base64\Base64UrlSafeEncoder', //'refreshExp' => '24 hours', //'exp' => '1 hour', //'alg' => 'RS256', //'jwsClass' => 'Namshi\JOSE\SimpleJWS', ], ] ]
-
在你的应用程序中创建一个
UserController
class UserController extends \thamtech\jwsauth\controllers\UserController { // parent class provides actionAuthenticate($username, $passwrd) // and actionRefreshToken() // You may add your own additional methods to provide additional user // management services such as registration, password changes, etc. }
-
更新你的
User
模型,用\thamtech\jwsauth\models\IdentityInterface
替换\yii\web\IdentityInterface
,并使用SimpleUserTrait
class User extends \yii\base\Object implements \thamtech\jwsauth\models\IdentityInterface { use SimpleUserTrait; public $id; public $username; // You must still implement all methods required by \yii\web\IdentityInterface // since \thamtech\jwsauth\models\IdentityInterface extends // \yii\web\IdentityInterface }
-
在你的任何想要通过 jwsauth 进行身份验证的 \JsonRpc2\Controller 上添加 JsonRpcAuth 过滤器
public function behaviors() { return [ 'authenticator' => [ 'class' => \thamtech\jwsauth\filters\auth\JsonRpcAuth::className(), 'except' => ['public-method-1', 'public-method-2'], ], ]; }
客户端使用
-
向认证方法发送 JSON RPC 请求,传递用户名和密码。
http://yoursite/user
数据
{ "jsonrpc": "2.0", "id": 1, "method": "authenticate", "params": { "username": "YOUR-USERNAME", "password": "YOUR-PASSWORD" } }
成功响应将类似于以下内容
{"jsonrpc":"2.0","id":1,"result":{"token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJpZCI6MT-TRIMMED_FOR_BREVITY"}}
-
使用上一步骤提供的令牌,向需要认证的任何控制器/方法发送 JSON RPC 请求
http://yoursite/protected-controller
数据
{ "jsonrpc": "2.0", "id": 2, "auth": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJpZCI6MT-TRIMMED_FOR_BREVITY", "method": "access-sensitive-data", "params": {"id": 27} }
令牌过期和刷新
当令牌过期(默认为 1 小时后),你可以刷新令牌而无需用户重新使用用户名和密码进行身份验证。这允许刷新令牌(默认为 24 小时)。
如果你有一个有效的令牌,并发送一个认证请求,但收到如下结果
{ "jsonrpc": "2.0", "id": 3, "error": { "code": -32652, "data": null, "message": "Invalid or expired token" } }
那么你的下一步是尝试刷新令牌
http://yoursite/user
数据
{ "jsonrpc": "2.0", "id": 4, "auth": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJpZCI6MT-TRIMMED_FOR_BREVITY", "method": "refresh-token" }
响应将包含一个新令牌,你可以继续正常使用
{"jsonrpc":"2.0","id":4,"result":{"token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJpZCI6MT-TRIMMED_FOR_BREVITY"}}
或指示令牌无法刷新
{ "jsonrpc": "2.0", "id": 3, "error": { "code": -32652, "data": null, "message": "expired; user must reauthenticate" } }
如果令牌无法刷新,那么你需要
-
要求用户使用他们的用户名和密码重新登录
-
使用上述客户端使用部分中的步骤 1 中的 "authenticate" 方法获取新的认证令牌。
-
使用新令牌继续发送认证请求。
高级使用
-
你不需要在 User 身份中使用
SimpleUserTrait
。它只是为大多数用例提供便利。你可以自由地直接在你的User
身份类中实现自己的getAuthKey()
和findIdentityByAccessToken()
方法,以更好地满足你的应用程序需求。 -
而不是将
UserController
实例化为子类,你可以在控制器映射中直接引用\thamtech\jwsauth\controllers\UserController
[ 'controllerMap' => [ // declares "login" controller using a class name 'login' => 'thamtech\jwsauth\controllers\UserController', ], ]
参见
-
cranetm/yii2-json-rpc-2.0 - 将你的控制器转换为 JSON RPC 2.0 API 的 Yii 2 扩展。
-
namshi/jose - JWS (JSON Web Signature) 规范的 PHP 实现。
-
JSON Web Signature (JWS) - JWS 规范