datto/json-rpc-auth

JSON-RPC 库的认证扩展

4.0.0 2015-12-15 20:03 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:36:47 UTC


README

这是一个为 php-json-rpc 库提供的认证和授权扩展。它允许在请求到达端点之前对 JSON-RPC 请求进行授权。

示例

首先编写一个认证 Handler

namespace Datto\JsonRpc\Auth;

use Datto\JsonRpc;

class BasicAuthHandler implements Handler
{
    public function canHandle($method, $arguments)
    {
        return isset($_SERVER['PHP_AUTH_USER']);
    }

    public function authenticate($method, $arguments)
    {
        // Don't do this in production. Using '===' is vulnerable to timing attacks!
        return $_SERVER['PHP_AUTH_USER'] === 'phil' && $_SERVER['PHP_AUTH_PW'] === 'superpass!';
    }
}

一旦有了这个,就可以这样使用。此示例使用 Simple\Evaluator(见 php-json-rpc-simple)作为底层映射机制

$authenticator = new Authenticator(array(
    new BasicAuthHandler(),
    // ...
));

$server = new Server(new Auth\Evaluator(new Simple\Evaluator(), $authenticator));
echo $server->reply('...');

要求

  • PHP >= 5.3

安装

"require": {
  "datto/json-rpc-auth": "~4.0"
}

许可证

此软件包在以下开源许可证下发布: LGPL-3.0

作者

由 Chad Kosie 和 Philipp C. Heckel 编写。